From bd9e1d701342ec93aa12e56036f6badeb0aec6ce Mon Sep 17 00:00:00 2001 From: bngsh Date: Sun, 26 May 2024 20:15:29 +0900 Subject: [PATCH] solve: 64week --- ...353\266\204 \354\210\230\354\227\264 4.kt" | 35 +++++++++++ ...4\353\246\254\353\247\211 \352\270\270.kt" | 44 ++++++++++++++ ...1 \353\213\254\355\214\275\354\235\264.kt" | 41 +++++++++++++ .../week64/\354\271\230\354\246\210.kt" | 60 +++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 "src/main/kotlin/byeonghee/week64/\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.kt" create mode 100644 "src/main/kotlin/byeonghee/week64/\353\202\264\353\246\254\353\247\211 \352\270\270.kt" create mode 100644 "src/main/kotlin/byeonghee/week64/\354\202\274\352\260\201 \353\213\254\355\214\275\354\235\264.kt" create mode 100644 "src/main/kotlin/byeonghee/week64/\354\271\230\354\246\210.kt" diff --git "a/src/main/kotlin/byeonghee/week64/\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.kt" "b/src/main/kotlin/byeonghee/week64/\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.kt" new file mode 100644 index 00000000..ec0cdb81 --- /dev/null +++ "b/src/main/kotlin/byeonghee/week64/\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.kt" @@ -0,0 +1,35 @@ +package byeonghee.week64 + +class `소병희_가장 긴 증가하는 부분 수열 4` { + companion object { + fun solve() = with(System.`in`.bufferedReader()) { + val n = readLine().toInt() + val arr = IntArray(n) + val dp = Array(n) { mutableListOf() } + var answer = 0 + + readLine().split(" ").forEachIndexed { i, v -> arr[i] = v.toInt() } + + dp[0].add(arr[0]) + for(i in 1 until n) { + var idx = i + for(j in i - 1 downTo 0) { + if (dp[j].last() < arr[i] && dp[j].size > dp[idx].size) { + idx = j + } + } + if (idx != i) dp[i].addAll(dp[idx]) + dp[i].add(arr[i]) + + if (dp[i].size > dp[answer].size) answer = i + } + + println(dp[answer].size) + println(dp[answer].joinToString(" ")) + } + } +} + +fun main() { + `소병희_가장 긴 증가하는 부분 수열 4`.solve() +} \ No newline at end of file diff --git "a/src/main/kotlin/byeonghee/week64/\353\202\264\353\246\254\353\247\211 \352\270\270.kt" "b/src/main/kotlin/byeonghee/week64/\353\202\264\353\246\254\353\247\211 \352\270\270.kt" new file mode 100644 index 00000000..09ee2680 --- /dev/null +++ "b/src/main/kotlin/byeonghee/week64/\353\202\264\353\246\254\353\247\211 \352\270\270.kt" @@ -0,0 +1,44 @@ +package byeonghee.week64 + +class 소병희_내리막길 { + companion object { + val dr = intArrayOf(0, 1, 0, -1) + val dc = intArrayOf(1, 0, -1, 0) + + fun solve() = with(System.`in`.bufferedReader()) { + val (m, n) = readLine().split(" ").map { it.toInt() } + val map = Array(m) { IntArray(n) } + val visited = Array(m) { IntArray(n) { -1 } } + + repeat(m) { i -> + readLine().split(" ").forEachIndexed { j, v -> + map[i][j] = v.toInt() + } + } + + visited[m-1][n-1] = 1 + + fun dfs(r: Int, c: Int): Int { + if (visited[r][c] > -1) return visited[r][c] + + visited[r][c] = 0 + for(d in 0 until 4) { + val nr = r + dr[d] + val nc = c + dc[d] + if (nr !in 0 until m || nc !in 0 until n) continue + if (map[nr][nc] >= map[r][c]) continue + + visited[r][c] += dfs(nr, nc) + } + + return visited[r][c] + } + + println(dfs(0, 0)) + } + } +} + +fun main() { + 소병희_내리막길.solve() +} \ No newline at end of file diff --git "a/src/main/kotlin/byeonghee/week64/\354\202\274\352\260\201 \353\213\254\355\214\275\354\235\264.kt" "b/src/main/kotlin/byeonghee/week64/\354\202\274\352\260\201 \353\213\254\355\214\275\354\235\264.kt" new file mode 100644 index 00000000..fba99dba --- /dev/null +++ "b/src/main/kotlin/byeonghee/week64/\354\202\274\352\260\201 \353\213\254\355\214\275\354\235\264.kt" @@ -0,0 +1,41 @@ +package byeonghee.week64 + +class `소병희_삼각 달팽이` { + fun solution(n: Int): IntArray { + var answer: IntArray = intArrayOf() + + val triangle = Array(n) { ArrayDeque() } + var side = n + var round = 0 + var sl = 0 + var count = 1 + + while(side > 0) { + for(i in sl until sl + side) { + triangle[i].add(round, count++) + } + + repeat(side - 1) { i -> + triangle[sl + side - 1].add(round + i + 1, count++) + } + + for(i in sl + side-2 downTo sl + 1) { + triangle[i].add(triangle[i].size - round, count++) + } + + round++ + side -= 3 + sl += 2 + } + + return triangle.flatMap { it.toList() }.toIntArray() + } +} + +fun main() { + val sol = `소병희_삼각 달팽이`() + println(sol.solution(4).joinToString(" ")) + println(sol.solution(5).joinToString(" ")) + println(sol.solution(6).joinToString(" ")) + println(sol.solution(7).joinToString(" ")) +} \ No newline at end of file diff --git "a/src/main/kotlin/byeonghee/week64/\354\271\230\354\246\210.kt" "b/src/main/kotlin/byeonghee/week64/\354\271\230\354\246\210.kt" new file mode 100644 index 00000000..5a74dacc --- /dev/null +++ "b/src/main/kotlin/byeonghee/week64/\354\271\230\354\246\210.kt" @@ -0,0 +1,60 @@ +package byeonghee.week64 + +class 소병희_치즈 { + companion object { + val dr = intArrayOf(0, 1, 0, -1) + val dc = intArrayOf(1, 0, -1, 0) + + fun solve() = with(System.`in`.bufferedReader()) { + val (n, m) = readLine().split(" ").map { it.toInt() } + val paper = Array(n) { IntArray(m) } + var airContact = Array(n) { IntArray(m) } + val q = ArrayDeque() + var time = 0 + var cheese = 0 + + repeat(n) { i -> + readLine().split(" ").forEachIndexed { j, v -> + paper[i][j] = v.toInt().also { if (it == 1) cheese++ } + } + } + + fun checkAir() { + airContact = Array(n) { IntArray(m) } + airContact[0][0]++ + q.add(intArrayOf(0, 0)) + + while(q.isNotEmpty()) { + val (r, c) = q.removeFirst() + for(d in 0 until 4) { + val nr = r + dr[d] + val nc = c + dc[d] + if (nr !in 0 until n || nc !in 0 until m) continue + if (paper[nr][nc] == 0 && airContact[nr][nc] > 0) continue + + airContact[nr][nc]++ + if (paper[nr][nc] == 1) continue + q.add(intArrayOf(nr, nc)) + } + } + } + + while(cheese > 0) { + checkAir() + for(i in 0 until n) for(j in 0 until m) { + if (paper[i][j] == 1 && airContact[i][j] >= 2) { + paper[i][j] = 0 + cheese-- + } + } + time++ + } + + println(time) + } + } +} + +fun main() { + 소병희_치즈.solve() +} \ No newline at end of file