From 3bb771e4c6b19a397471f4b94b6a236bda8d72a7 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:45:54 +0900 Subject: [PATCH 1/9] =?UTF-8?q?fix:=20=EA=B0=90=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/heejik/25week/\352\260\220\354\213\234.kt" | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git "a/src/main/kotlin/heejik/25week/\352\260\220\354\213\234.kt" "b/src/main/kotlin/heejik/25week/\352\260\220\354\213\234.kt" index aa8f1c4b..9d92e3cf 100644 --- "a/src/main/kotlin/heejik/25week/\352\260\220\354\213\234.kt" +++ "b/src/main/kotlin/heejik/25week/\352\260\220\354\213\234.kt" @@ -81,20 +81,18 @@ class 감시 { } } } - rec(0, setOf(), start = 0) + rec(0, setOf(), idx = 0) } - private fun rec(cnt: Int, monitoredPos: Set, start: Int) { + private fun rec(cnt: Int, monitoredPos: Set, idx: Int) { if (cnt == cctvPos.size) { answer = min(answer, blindSpotSize - monitoredPos.size) return } - for (i in start until cctvPos.size) { - for (way in CCTV.values()[room[cctvPos[i].x][cctvPos[i].y] - 1].ways) { - rec(cnt + 1, (monitoredPos + monitor(cctvPos[i], way)), start = i + 1) - } + for (way in CCTV.values()[room[cctvPos[idx].x][cctvPos[idx].y] - 1].ways) { + rec(cnt + 1, (monitoredPos + monitor(cctvPos[idx], way)), idx = idx + 1) } } From 796b75f8bf0dea866dd354d830591bd9969168b3 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:46:02 +0900 Subject: [PATCH 2/9] =?UTF-8?q?fix:=20=EB=8F=84=EC=84=9C=EA=B4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../heejik/24week/\353\217\204\354\204\234\352\264\200.kt" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/src/main/kotlin/heejik/24week/\353\217\204\354\204\234\352\264\200.kt" "b/src/main/kotlin/heejik/24week/\353\217\204\354\204\234\352\264\200.kt" index 81b6fe60..af48d18a 100644 --- "a/src/main/kotlin/heejik/24week/\353\217\204\354\204\234\352\264\200.kt" +++ "b/src/main/kotlin/heejik/24week/\353\217\204\354\204\234\352\264\200.kt" @@ -50,7 +50,7 @@ class 도서관 { while (books.isNotEmpty()) { if (books.size >= m) { val holdingBooks = books.subList(0, m) - distance += holdingBooks.maxOf { it.position.absoluteValue } + distance += holdingBooks.first().position books.removeAll(holdingBooks) } else { distance += books.maxOf { it.position.absoluteValue } From c136c97248b95642b93efb6f43779aee305392fa Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:46:19 +0900 Subject: [PATCH 3/9] =?UTF-8?q?fix:=20=EC=84=A0=EC=88=98=EA=B3=BC=EB=AA=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\204\240\354\210\230\352\263\274\353\252\251.kt" | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git "a/src/main/kotlin/heejik/25week/\354\204\240\354\210\230\352\263\274\353\252\251.kt" "b/src/main/kotlin/heejik/25week/\354\204\240\354\210\230\352\263\274\353\252\251.kt" index e85d4cde..b0ca0bb9 100644 --- "a/src/main/kotlin/heejik/25week/\354\204\240\354\210\230\352\263\274\353\252\251.kt" +++ "b/src/main/kotlin/heejik/25week/\354\204\240\354\210\230\352\263\274\353\252\251.kt" @@ -43,7 +43,7 @@ class 선수과목 { } } - semester ++ + semester++ while (learnedSubject.size != 0) { val tmpLearned = mutableListOf() @@ -57,10 +57,8 @@ class 선수과목 { } learnedSubject.clear() tmpLearned.forEach { - if (prerequisiteCount[it] == 0) { - subjectBySemester[it] = semester - learnedSubject.add(it) - } + subjectBySemester[it] = semester + learnedSubject.add(it) } semester++ } From 9ddb9a0f0ee67c459e859a499e881a2066e8c205 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:46:32 +0900 Subject: [PATCH 4/9] =?UTF-8?q?solve:=20=EB=A7=A5=EC=A3=BC=20=EB=A7=88?= =?UTF-8?q?=EC=8B=9C=EB=A9=B4=20=EA=B1=B8=EC=96=B4=EA=B0=80=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\354\226\264\352\260\200\352\270\260.kt" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "src/main/kotlin/heejik/26week/\353\247\245\354\243\274 \353\247\210\354\213\234\353\251\264\354\204\234 \352\261\270\354\226\264\352\260\200\352\270\260.kt" diff --git "a/src/main/kotlin/heejik/26week/\353\247\245\354\243\274 \353\247\210\354\213\234\353\251\264\354\204\234 \352\261\270\354\226\264\352\260\200\352\270\260.kt" "b/src/main/kotlin/heejik/26week/\353\247\245\354\243\274 \353\247\210\354\213\234\353\251\264\354\204\234 \352\261\270\354\226\264\352\260\200\352\270\260.kt" new file mode 100644 index 00000000..21798009 --- /dev/null +++ "b/src/main/kotlin/heejik/26week/\353\247\245\354\243\274 \353\247\210\354\213\234\353\251\264\354\204\234 \352\261\270\354\226\264\352\260\200\352\270\260.kt" @@ -0,0 +1,68 @@ +package heejik.`26week` + +import kotlin.math.abs + +class `맥주 마시면서 걸어가기` { + + private fun String.toPos() : Pos{ + return this.split(' ').map { it.toInt() }.run { + Pos(first(), last()) + } + } + + data class Pos( + val x: Int, + val y: Int + ) { + operator fun minus(other: Pos): Int { + return abs(x - other.x) + abs(y - other.y) + } + } + + fun getTestCaseCount() { + val testCaseCount = readln().toInt() + repeat(testCaseCount) { + canGoFestival().run { + println(if (this) "happy" else "sad") + } + } + } + + fun canGoFestival(): Boolean { + val n = readln().toInt() + val conveniencePoses = mutableListOf() + var homePos = readln().toPos() + + repeat(n) { + conveniencePoses.add(readln().toPos()) + } + var festivalPos = readln().toPos() + + return start(homePos, festivalPos, conveniencePoses) + } + + fun start(homePos: Pos, festivalPos: Pos, conveniencePoses: MutableList): Boolean { + val visited = MutableList(conveniencePoses.size) { false } + val queue = ArrayDeque() + queue.add(homePos) + + while (queue.isNotEmpty()) { + val pos = queue.removeFirst() + if (pos - festivalPos <= 1000) { + return true + } + conveniencePoses.forEachIndexed { index, conveniencePos -> + if (pos - conveniencePos <= 1000 && visited[index].not()) { + queue.add(conveniencePos) + visited[index] = true + } + } + } + + return false + } +} + +fun main() { + `맥주 마시면서 걸어가기`().getTestCaseCount() +} \ No newline at end of file From b15973b26a6a803995de62b79254a6dd4e0bb565 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:46:49 +0900 Subject: [PATCH 5/9] =?UTF-8?q?solve:=20=EC=A0=88=EB=8C=93=EA=B0=92=20?= =?UTF-8?q?=ED=9E=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\210\353\214\223\352\260\222 \355\236\231.kt" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 "src/main/kotlin/heejik/26week/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" diff --git "a/src/main/kotlin/heejik/26week/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" "b/src/main/kotlin/heejik/26week/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" new file mode 100644 index 00000000..aabed3a3 --- /dev/null +++ "b/src/main/kotlin/heejik/26week/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" @@ -0,0 +1,14 @@ +import java.util.PriorityQueue +import kotlin.math.abs + +fun main() { + val pq = PriorityQueue(compareBy { abs(it) }.thenComparator { a, b -> a - b }) + repeat(readln().toInt()) { + val x = readln().toInt() + if (x == 0) { + println(pq.poll() ?: 0) + } else { + pq.add(x) + } + } +} \ No newline at end of file From df2e330f151f070b338dace25fb145a1e878350e Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:47:02 +0900 Subject: [PATCH 6/9] =?UTF-8?q?solve:=20=EC=B9=98=EC=A6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../26week/\354\271\230\354\246\210.kt" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "src/main/kotlin/heejik/26week/\354\271\230\354\246\210.kt" diff --git "a/src/main/kotlin/heejik/26week/\354\271\230\354\246\210.kt" "b/src/main/kotlin/heejik/26week/\354\271\230\354\246\210.kt" new file mode 100644 index 00000000..09abf6b4 --- /dev/null +++ "b/src/main/kotlin/heejik/26week/\354\271\230\354\246\210.kt" @@ -0,0 +1,78 @@ +package heejik.`26week` + +import kotlin.properties.Delegates + +class 치즈 { + + data class Pos( + val x: Int, + val y: Int + ) + + val dx = listOf(1, -1, 0, 0) + val dy = listOf(0, 0, 1, -1) + + var n by Delegates.notNull() + var m by Delegates.notNull() + val board = mutableListOf>() + fun solve() { + readln().split(' ').map { it.toInt() }.run { + n = this[0] + m = this[1] + } + + repeat(n) { + board.add(readln().split(' ').map { it.toInt() }.toMutableList()) + } + + val answers = mutableListOf() + + while (true) { + val answer = bfs(pos = Pos(0, 0)) + if (answer == 0) { + break + } + answers.add(answer) + } + println(answers.size) + println(answers.last()) + } + + fun bfs(pos: Pos): Int { + var cnt = 0 + val queue = ArrayDeque() + queue.add(pos) + while (queue.isNotEmpty()) { + val (x, y) = queue.removeFirst() + for (i in dx.indices) { + val nx = x + dx[i] + val ny = y + dy[i] + if ((nx !in 0 until n) or (ny !in 0 until m)) continue + if (board[nx][ny] == 1) { + board[nx][ny] = -1 + cnt++ + } + if (board[nx][ny] == 0) { + queue.add(Pos(nx, ny)) + } + board[nx][ny] = -1 + } + } + + board.forEachIndexed { x, ints -> + ints.forEachIndexed { y, i -> + if (i == -1) { + board[x][y] = 0 + } + } + } + + + return cnt + } +} + +fun main() { + 치즈().solve() + +} \ No newline at end of file From 443fab348ffa6f4265917ab4d5d6e68832ad0b26 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:47:10 +0900 Subject: [PATCH 7/9] =?UTF-8?q?solve:=20=EC=B9=98=ED=82=A8=20=EB=B0=B0?= =?UTF-8?q?=EB=8B=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\355\202\250 \353\260\260\353\213\254.kt" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "src/main/kotlin/heejik/26week/\354\271\230\355\202\250 \353\260\260\353\213\254.kt" diff --git "a/src/main/kotlin/heejik/26week/\354\271\230\355\202\250 \353\260\260\353\213\254.kt" "b/src/main/kotlin/heejik/26week/\354\271\230\355\202\250 \353\260\260\353\213\254.kt" new file mode 100644 index 00000000..52f9ed12 --- /dev/null +++ "b/src/main/kotlin/heejik/26week/\354\271\230\355\202\250 \353\260\260\353\213\254.kt" @@ -0,0 +1,80 @@ + package heejik.`26week` + + import kotlin.math.absoluteValue + import kotlin.math.min + import kotlin.properties.Delegates + + class `치킨 배달` { + + data class Pos( + val x: Int, + val y: Int + ) { + operator fun minus(other: Pos) = (x - other.x).absoluteValue + (y - other.y).absoluteValue + + } + + var n by Delegates.notNull() + var m by Delegates.notNull() + + var answer = Int.MAX_VALUE + val city = mutableListOf>() + val chickenPoses = mutableListOf() + val homePoses = mutableListOf() + + fun solve() { + setting() + pickChickenStores(start = 0) + + println(answer) + } + + private fun setting() { + readln().split(' ').map { it.toInt() }.run { + n = this[0] + m = this[1] + } + + repeat(n) { + city.add(readln().split(' ').map { it.toInt() }.toMutableList()) + } + + city.forEachIndexed { x, row -> + row.forEachIndexed { y, i -> + if (i == 2) { + chickenPoses.add(Pos(x, y)) + } + if (i == 1) { + homePoses.add(Pos(x, y)) + } + } + } + } + + private fun pickChickenStores(stores: List = listOf(), start: Int) { + if (stores.size == m) { + answer = min(answer, getChickenDistance(stores)) + return + } + for (i in start until chickenPoses.size) { + pickChickenStores(stores.plus(chickenPoses[i]), start = i + 1) + } + } + + private fun getChickenDistance(stores: List): Int { + var sumOfDistance = 0 + homePoses.forEach { homePos -> + var minOfDistanceByHome = Int.MAX_VALUE + stores.forEach { chickenPos -> + minOfDistanceByHome = min(minOfDistanceByHome, homePos - chickenPos) + } + sumOfDistance += minOfDistanceByHome + } + + return sumOfDistance + } + } + + fun main() { + `치킨 배달`().solve() + } \ No newline at end of file From d2fa18f0b9441c7b655f070e0a1b10a77a38f487 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:47:21 +0900 Subject: [PATCH 8/9] =?UTF-8?q?solve:=20=ED=91=9C=ED=98=84=20=EA=B0=80?= =?UTF-8?q?=EB=8A=A5=ED=95=9C=20=EC=9D=B4=EC=A7=84=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\354\247\204\355\212\270\353\246\254.kt" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "src/main/kotlin/heejik/26week/\355\221\234\355\230\204 \352\260\200\353\212\245\355\225\234 \354\235\264\354\247\204\355\212\270\353\246\254.kt" diff --git "a/src/main/kotlin/heejik/26week/\355\221\234\355\230\204 \352\260\200\353\212\245\355\225\234 \354\235\264\354\247\204\355\212\270\353\246\254.kt" "b/src/main/kotlin/heejik/26week/\355\221\234\355\230\204 \352\260\200\353\212\245\355\225\234 \354\235\264\354\247\204\355\212\270\353\246\254.kt" new file mode 100644 index 00000000..c4b45a44 --- /dev/null +++ "b/src/main/kotlin/heejik/26week/\355\221\234\355\230\204 \352\260\200\353\212\245\355\225\234 \354\235\264\354\247\204\355\212\270\353\246\254.kt" @@ -0,0 +1,74 @@ +package heejik.`26week` + +import kotlin.math.ln +import kotlin.math.pow + +class `표현 가능한 이진트리` { + val answers = mutableListOf() + var isAdded = false + fun solution(numbers: LongArray): MutableList { + numbers.forEach { + decimalToBinary(it).setFullBinaryTree().checkBinaryTree() + } + return answers + } + + private fun decimalToBinary(number: Long): StringBuilder { + var quotient = number + val binary = StringBuilder() + while (quotient > 0) { + binary.insert(0, quotient % 2) + quotient /= 2 + } + return binary + } + + private fun StringBuilder.setFullBinaryTree(): StringBuilder { + while ((this.length and this.length + 1) != 0) { + this.insert(0, "0") + } + return this + } + + private fun StringBuilder.checkBinaryTree() { + val treeHeight = getHeightOfTree(this.length) + val rootNode = 2.0.pow(treeHeight - 1).toInt() - 1 + val offset = treeHeight - 2 + + isAdded = false + val preAnswerSize = answers.size + checkBinaryTreeBySubTree(rootNode, offset) + if (preAnswerSize == answers.size) answers.add(1) + + } + + + private fun StringBuilder.checkBinaryTreeBySubTree(rootNode: Int, offset: Int) { + if (offset < 0) return + val distanceOfChildTree = 2.0.pow(offset).toInt() + if (this[rootNode] == '0') { + if ((this[rootNode - distanceOfChildTree] != '0') or (this[rootNode + distanceOfChildTree] != '0')) { + if (isAdded.not()) { + answers.add(0) + isAdded = true + } + return + } + } + + + checkBinaryTreeBySubTree(rootNode - distanceOfChildTree, offset - 1) + checkBinaryTreeBySubTree(rootNode + distanceOfChildTree, offset - 1) + } + + + fun getHeightOfTree(n: Int): Int { + return (ln(n.toDouble()) / ln(2.0)).toInt() + 1 + } +} + + +fun main() { + val numbers = longArrayOf(63,111,95) + `표현 가능한 이진트리`().solution(numbers) +} \ No newline at end of file From 758a979d8253dd02ab2098b4d7f70f395b79a78b Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 10 Mar 2023 23:48:09 +0900 Subject: [PATCH 9/9] =?UTF-8?q?fix:=20=EC=A0=88=EB=8C=93=EA=B0=92=20?= =?UTF-8?q?=ED=9E=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\353\214\223\352\260\222 \355\236\231.kt" | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git "a/src/main/kotlin/heejik/26week/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" "b/src/main/kotlin/heejik/26week/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" index aabed3a3..262ae6f6 100644 --- "a/src/main/kotlin/heejik/26week/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" +++ "b/src/main/kotlin/heejik/26week/\354\240\210\353\214\223\352\260\222 \355\236\231.kt" @@ -1,14 +1,26 @@ +package heejik.`26week` + import java.util.PriorityQueue -import kotlin.math.abs +import kotlin.math.absoluteValue -fun main() { - val pq = PriorityQueue(compareBy { abs(it) }.thenComparator { a, b -> a - b }) - repeat(readln().toInt()) { - val x = readln().toInt() - if (x == 0) { - println(pq.poll() ?: 0) - } else { - pq.add(x) +class `절댓값 힙` { + + val pq = PriorityQueue(compareBy { it.absoluteValue }.thenComparator { a, b -> a - b }) + fun solve() { + val n = readln().toInt() + repeat(n) { + val x = readln().toInt() + if (x == 0) { + pq.poll().run { + println(this ?: 0) + } + } else { + pq.add(x) + } } } +} + +fun main() { + `절댓값 힙`().solve() } \ No newline at end of file