Skip to content

[Week5] 소병희: 미로 만들기, 쿼드트리, 아기 상어, 흙길 보수하기 #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/5week/byeonghee/미로 만들기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package `5week`.byeonghee

import java.io.*

class `소병희_미로 만들기` {

companion object {
const val R = 1
const val L = -1
}

val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.`out`))

fun Int.turn(dir: Int) : Int {
return (this + 4 + dir) % 4
}

fun Char.dir() : Int {
return if (this == 'R') R else L
}

data class Pos(var r: Int, var c: Int) {
operator fun plus(pos: Pos) : Pos {
return Pos(r + pos.r, c + pos.c)
}
}

val directions = listOf(
Pos(1, 0),
Pos(0, -1),
Pos(-1, 0),
Pos(0, 1)
)

var minPos = Pos(50, 50)
var maxPos = Pos(50, 50)
var curDir = 0
var curPos = Pos(50, 50)
val maze = Array(101) { CharArray(101){'#'} }

fun solution() {
val n = br.readLine().toInt()
br.readLine().toCharArray().forEach {
when(it) {
'F' -> {
curPos += directions[curDir]
with(curPos) {
maze[r][c] = '.'

maxPos = Pos(Integer.max(maxPos.r, r), Integer.max(maxPos.c, c))
minPos = Pos(Integer.min(minPos.r, r), Integer.min(minPos.c, c))
}
}
else -> curDir = curDir.turn(it.dir())
}
}
maze[50][50] = '.'

for(r in minPos.r..maxPos.r) {
for(c in minPos.c..maxPos.c) {
bw.append(maze[r][c])
}
bw.newLine()
}
bw.flush()
}
}

fun main() {
`소병희_미로 만들기`().solution()
}
2 changes: 2 additions & 0 deletions src/5week/byeonghee/수 게임.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package `5week`.byeonghee

98 changes: 98 additions & 0 deletions src/5week/byeonghee/아기 상어.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package `5week`.byeonghee

import java.io.*
import java.util.PriorityQueue
import kotlin.system.exitProcess

class `소병희_아기 상어` {
companion object {
data class Pos(val r: Int, val c: Int) {
operator fun plus(pos: Pos) : Pos {
return Pos(r + pos.r, c + pos.c)
}

fun inSpace(x: Int) : Boolean {
return r in 0 until x && c in 0 until x
}
}

val mv = listOf(
Pos(-1, 0),
Pos(0, -1),
Pos(0, 1),
Pos(1, 0)
)

val br = BufferedReader(InputStreamReader(System.`in`))

var n = 0

lateinit var space: Array<IntArray>
lateinit var visited: Array<BooleanArray>

val pq = PriorityQueue(Comparator<Pair<Pos, Int>> { a, b ->
if (a.second == b.second) {
if (a.first.r == b.first.r) a.first.c - b.first.c
else a.first.r - b.first.r
}
else a.second - b.second
})

var shark = Pos(0, 0)
var size = 2
var eat = 0
var mom = true
var answer = 0

fun solve() {
n = br.readLine().toInt()
space = Array(n) { r ->
br.readLine().split(" ").mapIndexed{ c, it ->
it.toInt().also { if (it == 9) shark = Pos(r, c) }
}.toIntArray()
}
space[shark.r][shark.c] = 0

while(true) {
pq.clear()
pq.add(Pair(shark, 0))
visited = Array(n) { BooleanArray(n) }
mom = true

while(pq.isNotEmpty()) {
val (pos, dist) = pq.poll()
if (pos.inSpace(n).not()) continue
if (visited[pos.r][pos.c]) continue
visited[pos.r][pos.c] = true

if (space[pos.r][pos.c] > size) continue

if (space[pos.r][pos.c] in listOf(0, size) ) {
for (d in mv) {
pq.add(Pair(pos + d, dist + 1))
}
}
else {
answer += dist
if ((++eat) == size) {
eat = 0
size++
}
space[pos.r][pos.c] = 0
shark = pos.copy()
mom = false
break
}
}
if (mom) {
println(answer)
exitProcess((0))
}
}
}
}
}

fun main() {
`소병희_아기 상어`.solve()
}
27 changes: 27 additions & 0 deletions src/5week/byeonghee/오픈채팅방.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package `5week`.byeonghee

class 소병희_오픈채팅방 {
companion object {
var answer = mutableListOf<String>()
val uidMap = hashMapOf<String, String>()

fun solution(record: Array<String>): Array<String> {
val log = record.map{ it.split(" ") }

log.forEach {
when(it[0]) {
"Enter", "Change" -> uidMap.put(it[1], it[2])
}
}

log.forEach {
when(it[0]) {
"Enter" -> answer.add("${uidMap[it[1]]!!}님이 들어왔습니다.")
"Leave" -> answer.add("${uidMap[it[1]]!!}님이 나갔습니다.")
}
}

return answer.toTypedArray()
}
}
}
67 changes: 67 additions & 0 deletions src/5week/byeonghee/쿼드트리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package `5week`.byeonghee

/**
* 1. array + slice -> list + subList : -20ms
* 2. if (all 1) else if (all 0) else -> when(all 1) : -10ms
* 3. subList * 2 + all -> for(r)for(c)if(1) : -40ms
* 4. list+ for -> array + for : -20ms
*/

import java.io.*

class `소병희_쿼드트리` {
companion object {
data class Pos(val r: Int, val c: Int) {
operator fun times(int: Int) : Pos {
return Pos(r * int, c * int)
}

operator fun plus(pos: Pos) : Pos {
return Pos(r + pos.r, c + pos.c)
}
}

val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.`out`))

val quad = listOf(
Pos(0, 0),
Pos(0, 1),
Pos(1, 0),
Pos(1, 1)
)

lateinit var screen : Array<CharArray>

fun solve() {
val n = br.readLine().toInt()
screen = Array(n) { br.readLine().toCharArray() }

dfsQuadTree(Pos(0, 0), n)
bw.flush()
}

fun dfsQuadTree(p: Pos, size: Int) {
var ones = 0
for(r in p.r until p.r + size) for(c in p.c until p.c + size) {
if (screen[r][c] == '1') ones++
}

when(ones) {
size * size -> bw.append('1')
0 -> bw.append('0')
else -> {
bw.append('(')
for(mv in quad) {
dfsQuadTree(p + (mv * (size / 2)), size / 2)
}
bw.append(')')
}
}
}
}
}

fun main() {
`소병희_쿼드트리`.solve()
}
52 changes: 52 additions & 0 deletions src/5week/byeonghee/흙길 보수하기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package `5week`.byeonghee

/** 입력이 [s, e)로 주어짐에 주의해야 했다 **/

import java.io.*
import java.util.PriorityQueue

class `소병희_흙길 보수하기` {

data class Water(val s: Int, val e: Int)

val br = BufferedReader(InputStreamReader(System.`in`))
val pq = PriorityQueue(Comparator<Water>{ a, b -> a.s - b.s })
var n = 0
var l = 0

lateinit var cur : Water
var mergeCnt = 0
var prevEnd = 0
var answer = 0

fun solution() {
br.readLine().split(" ").map{ it.toInt() }.let {
n = it[0]
l = it[1]
}

repeat(n) {
br.readLine().split(" ").map{ it.toInt() }.sorted().let {
pq.add(Water(it[0], it[1] - 1))
}
}

while(pq.isNotEmpty()) {
cur = pq.poll()
prevEnd = Integer.max(prevEnd, cur.s)
mergeCnt = getCount(prevEnd, cur.e)
answer += mergeCnt
prevEnd += l * mergeCnt
}

println(answer)
}

fun getCount(s: Int, e: Int) : Int {
return (e - s + l ) / l
}
}

fun main() {
`소병희_흙길 보수하기`().solution()
}