From e5a14cbe744941d0d596c15f3eb13c7fd04fe3c8 Mon Sep 17 00:00:00 2001 From: soopeach Date: Sun, 23 Oct 2022 16:59:36 +0900 Subject: [PATCH 1/6] =?UTF-8?q?Feat:=20=ED=86=B1=EB=8B=88=EB=B0=94?= =?UTF-8?q?=ED=80=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...61\353\213\210\353\260\224\355\200\264.kt" | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 "src/4week/hyunsoo/\355\206\261\353\213\210\353\260\224\355\200\264.kt" diff --git "a/src/4week/hyunsoo/\355\206\261\353\213\210\353\260\224\355\200\264.kt" "b/src/4week/hyunsoo/\355\206\261\353\213\210\353\260\224\355\200\264.kt" new file mode 100644 index 0000000..8b9e1f1 --- /dev/null +++ "b/src/4week/hyunsoo/\355\206\261\353\213\210\353\260\224\355\200\264.kt" @@ -0,0 +1,188 @@ +package `4week`.hyunsoo + +/** + * <문제> + * [톱니바퀴](https://www.acmicpc.net/problem/14891) + * + * 톱니바퀴는 8개의 톱니를 가지고 있고, 4개가 주어짐 + * - 회전에 대한 정보가 주어지면 + * - 1 ~ 4번의 톱니 상태를 모두 확인하기 + * - 지정된 톱니부터 하나씩 차례로 회전 시키기 + * + * + * + * + * 입/출력 + * - 1 ~ 4째 줄에 톱니바퀴의 상태가 주어짐 + * - 상태는 8개의 정수로 주어지고 12시방향부터 시계방향임. + * - N극은 0, S극은 1 + * - 5째 줄에는 회전 횟수가 주어짐. + * - 그 후 K개의 줄에는 회전시킨 방법이 주어짐. + * - 첫 번째 정수는 회전시킨 톱니바퀴의 번호, 두 번째 정수는 방향 + * - 방향이 1이면 시계 방향, -1이면 반시계 방향 + */ + +class `전현수_톱니바퀴`{ + data class RotateInfo(val gearNum: Int, val dir: Int) + + class Gear(stateData: String) { + + private val state = IntArray(8) + + init { + stateData.forEachIndexed { index, charNum -> + state[index] = charNum.digitToInt() + } + } + + // 시계 방향으로 회전 + private fun rotateClockWise() { + val tempState = state.copyOf(state.size) + state[0] = state[state.lastIndex] + tempState.dropLast(1).forEachIndexed { index, num -> + state[index + 1] = num + } + } + + // 반시계 방향으로 회전 + private fun rotateCounterClockWise() { + val tempState = state.copyOf(state.size) + state[state.lastIndex] = state[0] + tempState.drop(1).forEachIndexed { index, num -> + state[index] = num + } + } + + fun rotate(dir: Int) { + if (dir == 1) rotateClockWise() + else rotateCounterClockWise() + } + + private fun getLeftPole() = state[6] + + private fun getRightPole() = state[2] + + fun getPole() = listOf( + getLeftPole(), + getRightPole() + ) + + fun getTopState() = state.first() + + override fun toString(): String { + return this.state.joinToString("") + } + } + + + val gearList = Array(4) { + Gear(readln()) + } + + val affectedInfo = Array(4) { + mutableListOf() + } + + fun solution() { + + val gear1 = gearList.first() + val gear2 = gearList.second() + val gear3 = gearList.third() + val gear4 = gearList.fourth() + + val rotateCnt = readln().toInt() + + val rotateInfoList = Array(rotateCnt) { + val (gearNum, dir) = readln().split(" ").map { it.toInt() } + RotateInfo(gearNum, dir) + } + + rotateInfoList.forEach { rotateInfo -> + + val (gearNum, dir) = rotateInfo + + val (firstLeft, firstRight) = gear1.getPole() + val (secondLeft, secondRight) = gear2.getPole() + val (thirdLeft, thirdRight) = gear3.getPole() + val (fourthLeft, fourthRight) = gear4.getPole() + + // 회전에 영향을 받을 기어 판정 + if (firstRight != secondLeft) { + affectedInfo[0].add(1) + affectedInfo[1].add(0) + } + if (secondRight != thirdLeft) { + affectedInfo[1].add(2) + affectedInfo[2].add(1) + } + if (thirdRight != fourthLeft) { + affectedInfo[2].add(3) + affectedInfo[3].add(2) + } + + // 지정된 기어 회전 + rotateGear(gearNum - 1, dir) + + affectedInfo.clear() + } + + var score = 0 + repeat(4) { cnt -> + score += gearList[cnt].getTopState() * twoToPowOfExponent(cnt) + } + + println(score) + + } + + fun rotateGear(gearNum: Int, dir: Int) { + val targetGear = gearList[gearNum] + targetGear.rotate(dir) + val changedDir = dir.dirChange() + val relatedGearList = mutableListOf() + + affectedInfo[gearNum].forEach { relatedGearNum -> + relatedGearList.add(relatedGearNum) + } + + relatedGearList.forEach { relatedGearNum -> + affectedInfo[gearNum].remove(relatedGearNum) + affectedInfo[relatedGearNum].remove(gearNum) + } + + relatedGearList.forEach { + rotateGear(it, changedDir) + } + } + + // 2차원 배열 안의 리스트들을 초기화 + fun Array>.clear() { + this.forEach { + it.clear() + } + } + + // 2의 거듭제곱 + fun twoToPowOfExponent(exponent: Int): Int { + var powered = 1 + if (exponent == 0) return 1 + repeat(exponent) { + powered *= 2 + } + return powered + } + + // 방향 바꿔주기 + fun Int.dirChange() = if (this == -1) 1 else -1 + + fun Array.second(): T = this[1] + fun Array.third(): T = this[2] + fun Array.fourth(): T = this[3] +} + +fun main(){ + val myClass = 전현수_톱니바퀴() + myClass.solution() +} + + From abe70a39f2684cb1185f85756e8b36045d99bf23 Mon Sep 17 00:00:00 2001 From: soopeach Date: Sun, 23 Oct 2022 17:01:27 +0900 Subject: [PATCH 2/6] =?UTF-8?q?Feat:=20=EA=B7=BC=EC=86=90=EC=8B=A4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\352\267\274\354\206\220\354\213\244.kt" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 "src/4week/hyunsoo/\352\267\274\354\206\220\354\213\244.kt" diff --git "a/src/4week/hyunsoo/\352\267\274\354\206\220\354\213\244.kt" "b/src/4week/hyunsoo/\352\267\274\354\206\220\354\213\244.kt" new file mode 100644 index 0000000..b0453a9 --- /dev/null +++ "b/src/4week/hyunsoo/\352\267\274\354\206\220\354\213\244.kt" @@ -0,0 +1,62 @@ +package `4week`.hyunsoo + +class 전현수_근손실{ + /** + * <문제> + * [근손실](https://www.acmicpc.net/problem/18429) + * + * 하루가 지나면 중량이 K만큼 감소 + * N개의 서로 다른 운동 키트를 가지고 있음. + * 하루에 1개씩 키트를 사용. 사용하면 중량이 즉시 증가 + * 운동 키트를 사용하면서 항상 중량이 500이상이 되도록 해야함. + * + * 아이디어 + * - 완탐해서 모든 조합을 구하고 + * - 각 조합들로 운동할 경우 500이상이 되는지를 판정하자! + * + */ + + val exerciseKitList = mutableListOf() + var possibleCaseCnt = 0 + val exerciseCase = mutableListOf() + val visited = BooleanArray(8) { false } + + fun solution() { + + val (exerciseCnt, downWeight) = readln().split(" ").map { it.toInt() } + val exerciseData = readln().split(" ").map { it.toInt() } + exerciseKitList.addAll(exerciseData) + + checkAllCases(0, exerciseCnt, downWeight) + + println(possibleCaseCnt) + } + + fun checkAllCases(cnt: Int, depth: Int, downWeight: Int) { + + if (cnt == depth) { + + var curWeight = 500 + exerciseCase.forEach { upWeight -> + curWeight += upWeight - downWeight + if (curWeight < 500) return + } + possibleCaseCnt++ + return + } + for (index in 0 until exerciseKitList.size) { + if (visited[index]) continue + + visited[index] = true + exerciseCase.add(exerciseKitList[index]) + checkAllCases(cnt + 1, depth, downWeight) + visited[index] = false + exerciseCase.removeAt(exerciseCase.lastIndex) + } + } +} + +fun main(){ + val myClass = 전현수_근손실() + myClass.solution() +} \ No newline at end of file From 52a1e1b450cc68a4c89289456a64b5ed2ddfdfcf Mon Sep 17 00:00:00 2001 From: soopeach Date: Sun, 23 Oct 2022 17:03:03 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Feat:=20=ED=8A=B8=EB=A6=AC=EC=9D=98=20?= =?UTF-8?q?=EB=B6=80=EB=AA=A8=20=EC=B0=BE=EA=B8=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\353\252\250 \354\260\276\352\270\260.kt" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "src/4week/hyunsoo/\355\212\270\353\246\254\354\235\230 \353\266\200\353\252\250 \354\260\276\352\270\260.kt" diff --git "a/src/4week/hyunsoo/\355\212\270\353\246\254\354\235\230 \353\266\200\353\252\250 \354\260\276\352\270\260.kt" "b/src/4week/hyunsoo/\355\212\270\353\246\254\354\235\230 \353\266\200\353\252\250 \354\260\276\352\270\260.kt" new file mode 100644 index 0000000..93d1b5a --- /dev/null +++ "b/src/4week/hyunsoo/\355\212\270\353\246\254\354\235\230 \353\266\200\353\252\250 \354\260\276\352\270\260.kt" @@ -0,0 +1,60 @@ +package `4week`.hyunsoo + +import java.util.* + +/** + * <문제> + * [트리의 부모 찾기](https://www.acmicpc.net/problem/11725) + * + * 루트없는 트리가 주어짐. + * 트리의 루트는 1. 각 노드의 부모를 구하라. + */ + +class 전현수_트리의_부모_찾기{ + + val rootQueue: Queue = ArrayDeque() + val nodeCnt = readln().toInt() + val treeArray = Array(nodeCnt + 1) { mutableListOf() } + val parentNodeData = IntArray(nodeCnt + 1) + + // 루트는 1 + val initParent = 1 + + fun solution() { + + + repeat(nodeCnt - 1) { + val (a, b) = readln().split(" ").map { it.toInt() } + treeArray[a].add(b) + treeArray[b].add(a) + } + findParentNode() + parentNodeData.drop(2).forEach { + println(it) + } + + } + + fun findParentNode() { + + rootQueue.add(initParent) + + while (rootQueue.isNotEmpty()) { + val curParent = rootQueue.poll() + // 부모와 연결되어있는 것들 자식으로 판정 + treeArray[curParent].forEach { child -> + parentNodeData[child] = curParent + // 방문 판정 -> 자식 노드에서 부모 노드 제거 + treeArray[child].remove(curParent) + rootQueue.add(child) + } + } + + } + +} + +fun main(){ + val myClass = 전현수_트리의_부모_찾기() + myClass.solution() +} \ No newline at end of file From 960965705c56dfb0d017b9a70397cbb669f2d279 Mon Sep 17 00:00:00 2001 From: soopeach Date: Sun, 23 Oct 2022 17:03:53 +0900 Subject: [PATCH 4/6] =?UTF-8?q?Feat:=20=ED=9A=8C=EC=9D=98=EC=8B=A4=20?= =?UTF-8?q?=EB=B0=B0=EC=A0=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\213\244 \353\260\260\354\240\225.kt" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "src/4week/hyunsoo/\355\232\214\354\235\230\354\213\244 \353\260\260\354\240\225.kt" diff --git "a/src/4week/hyunsoo/\355\232\214\354\235\230\354\213\244 \353\260\260\354\240\225.kt" "b/src/4week/hyunsoo/\355\232\214\354\235\230\354\213\244 \353\260\260\354\240\225.kt" new file mode 100644 index 0000000..37fcdeb --- /dev/null +++ "b/src/4week/hyunsoo/\355\232\214\354\235\230\354\213\244 \353\260\260\354\240\225.kt" @@ -0,0 +1,52 @@ +package `4week`.hyunsoo + +/** + * <문제> + * [회의실 배정](https://www.acmicpc.net/problem/1931) + * + * 한 개의 회의실이 있는데 이를 사용하고자 하는 N개의 회으ㅔㅇ 대하여 회의실 사용표를 만드려고 함. + * 각 회의 I에 대해 시작시간과 끝나는 시간이 주어져있고, 각 회의가 겹치지 않게 하면서 회의실을 사용할 수 있는 회의의 최대 개수 찾기 + * + * 아이디어 + * - 회의의 수가 100,000개라 완탐은 불가능 + */ + +class 전현수_회의실_배정{ + + data class MeetingData(val start: Int, val end: Int) + + fun solution() { + + val meetingCnt = readln().toInt() + val meetingList = mutableListOf() + var canMeetingCnt = 0 + var endTime = 0 + + repeat(meetingCnt) { + val (start, end) = readln().split(" ").map { it.toInt() } + meetingList.add(MeetingData(start, end)) + } + + // 정렬 + meetingList.sortWith(compareBy { it.end }.thenBy { it.start }) + + // 정렬된 첫 미팅은 반드시 해야지! + endTime = meetingList.first().end + canMeetingCnt++ + + meetingList.drop(1).forEach { meetingData -> + if (endTime <= meetingData.start) { + endTime = meetingData.end + canMeetingCnt++ + } + } + + println(canMeetingCnt) + } + +} + +fun main(){ + val myClass = 전현수_회의실_배정() + myClass.solution() +} \ No newline at end of file From 4912b9bf84d2d85ba60f08984312b30bb24b3348 Mon Sep 17 00:00:00 2001 From: soopeach Date: Mon, 24 Oct 2022 04:00:33 +0900 Subject: [PATCH 5/6] =?UTF-8?q?Feat:=20=EC=A7=80=EB=A6=84=EA=B8=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\247\200\353\246\204\352\270\270.kt" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" diff --git "a/src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" "b/src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" new file mode 100644 index 0000000..f94105a --- /dev/null +++ "b/src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" @@ -0,0 +1,50 @@ +package `4week`.hyunsoo + +import java.lang.Math.min + +/** + * <문제> + * [지름길](https://www.acmicpc.net/problem/1446) + * + * 매일 아침, 세준이는 학교에 가기 위해 차를 타고 D 킬로미터 길이의 고속도로를 지남 + * 어느 날 지름길이 존재하는 것을 알게 되었고, 지름길은 일방통행임 + * 세준이가 운전해야 하는 거리의 최솟값을 구해라 + * + * 입/출력 + * - 첫째 줄 + * - 지름길의 개수 N(12 이하), 고속도로의 길이 D(10,000 이하) + * - N개의 줄 + * - 지름길의 시작 위치, 도착 위치, 지름길의 길이가 주어짐.(10,000 이하) + * + */ + +data class ShortCutData(val start: Int, val distance: Int) + +fun main() { + + val (shortCutCnt, highWayLength) = readln().split(" ").map { it.toInt() } + val dp = Array(10001) { i -> i } + val shortCutGraph = Array(10001) { + mutableListOf() + } + + repeat(shortCutCnt) { + val (start, end, shortCutDistance) = readln().split(" ").map { it.toInt() } + // 도착지점이 고속도로의 길이를 넘거나 지름길이 도로보다 길지 않은 경우에만 지름길 생성 + if ((end > highWayLength).not() && + (end - start <= shortCutDistance).not() + ) shortCutGraph[end].add(ShortCutData(start, shortCutDistance)) + + } + + for (end in 1..highWayLength) { + dp[end] = dp[end - 1] + 1 + if (shortCutGraph[end].isNotEmpty()) { + shortCutGraph[end].forEach { shortCutData -> + dp[end] = min(dp[end], dp[shortCutData.start] + shortCutData.distance) + } + } + } + + println(dp[highWayLength]) +} \ No newline at end of file From c2d907c0a129c5c9a575fcf9a6e1b5bac79522e6 Mon Sep 17 00:00:00 2001 From: soopeach Date: Mon, 24 Oct 2022 04:04:30 +0900 Subject: [PATCH 6/6] =?UTF-8?q?Refactor:=20=EC=A7=80=EB=A6=84=EA=B8=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=A9=EB=8F=8C=20=EC=95=88=EB=82=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\247\200\353\246\204\352\270\270.kt" | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git "a/src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" "b/src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" index f94105a..71b36bb 100644 --- "a/src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" +++ "b/src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" @@ -18,33 +18,42 @@ import java.lang.Math.min * */ -data class ShortCutData(val start: Int, val distance: Int) +class 전현수_지름길{ -fun main() { + data class ShortCutData(val start: Int, val distance: Int) - val (shortCutCnt, highWayLength) = readln().split(" ").map { it.toInt() } - val dp = Array(10001) { i -> i } - val shortCutGraph = Array(10001) { - mutableListOf() - } + fun solution() { - repeat(shortCutCnt) { - val (start, end, shortCutDistance) = readln().split(" ").map { it.toInt() } - // 도착지점이 고속도로의 길이를 넘거나 지름길이 도로보다 길지 않은 경우에만 지름길 생성 - if ((end > highWayLength).not() && - (end - start <= shortCutDistance).not() - ) shortCutGraph[end].add(ShortCutData(start, shortCutDistance)) + val (shortCutCnt, highWayLength) = readln().split(" ").map { it.toInt() } + val dp = Array(10001) { i -> i } + val shortCutGraph = Array(10001) { + mutableListOf() + } - } + repeat(shortCutCnt) { + val (start, end, shortCutDistance) = readln().split(" ").map { it.toInt() } + // 도착지점이 고속도로의 길이를 넘거나 지름길이 도로보다 길지 않은 경우에만 지름길 생성 + if ((end > highWayLength).not() && + (end - start <= shortCutDistance).not() + ) shortCutGraph[end].add(ShortCutData(start, shortCutDistance)) - for (end in 1..highWayLength) { - dp[end] = dp[end - 1] + 1 - if (shortCutGraph[end].isNotEmpty()) { - shortCutGraph[end].forEach { shortCutData -> - dp[end] = min(dp[end], dp[shortCutData.start] + shortCutData.distance) + } + + for (end in 1..highWayLength) { + dp[end] = dp[end - 1] + 1 + if (shortCutGraph[end].isNotEmpty()) { + shortCutGraph[end].forEach { shortCutData -> + dp[end] = min(dp[end], dp[shortCutData.start] + shortCutData.distance) + } } } + + println(dp[highWayLength]) } - println(dp[highWayLength]) +} + +fun main(){ + val myClass = 전현수_지름길() + myClass.solution() } \ No newline at end of file