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 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..71b36bb --- /dev/null +++ "b/src/4week/hyunsoo/\354\247\200\353\246\204\352\270\270.kt" @@ -0,0 +1,59 @@ +package `4week`.hyunsoo + +import java.lang.Math.min + +/** + * <문제> + * [지름길](https://www.acmicpc.net/problem/1446) + * + * 매일 아침, 세준이는 학교에 가기 위해 차를 타고 D 킬로미터 길이의 고속도로를 지남 + * 어느 날 지름길이 존재하는 것을 알게 되었고, 지름길은 일방통행임 + * 세준이가 운전해야 하는 거리의 최솟값을 구해라 + * + * 입/출력 + * - 첫째 줄 + * - 지름길의 개수 N(12 이하), 고속도로의 길이 D(10,000 이하) + * - N개의 줄 + * - 지름길의 시작 위치, 도착 위치, 지름길의 길이가 주어짐.(10,000 이하) + * + */ + +class 전현수_지름길{ + + data class ShortCutData(val start: Int, val distance: Int) + + fun solution() { + + 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]) + } + +} + +fun main(){ + val myClass = 전현수_지름길() + myClass.solution() +} \ No newline at end of file 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() +} + + 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 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