From fa03c78cc72deceef05bbf64e4e644e209310c18 Mon Sep 17 00:00:00 2001 From: yubin Date: Tue, 23 Jan 2024 00:08:11 +0900 Subject: [PATCH] =?UTF-8?q?solve(BOJ):=20G5=5F10026=5F=EC=A0=81=EB=A1=9D?= =?UTF-8?q?=EC=83=89=EC=95=BD=5Fkt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\353\241\235\354\203\211\354\225\275.kt" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 "src/boj/G5_10026_\354\240\201\353\241\235\354\203\211\354\225\275.kt" diff --git "a/src/boj/G5_10026_\354\240\201\353\241\235\354\203\211\354\225\275.kt" "b/src/boj/G5_10026_\354\240\201\353\241\235\354\203\211\354\225\275.kt" new file mode 100644 index 0000000..9413027 --- /dev/null +++ "b/src/boj/G5_10026_\354\240\201\353\241\235\354\203\211\354\225\275.kt" @@ -0,0 +1,71 @@ +package boj + +import java.util.* + +class BOJ10026() { + var N = 0 + lateinit var map: Array + + val offsets = arrayOf(-1 to 0, 0 to -1, 1 to 0, 0 to 1) + fun solve() { + N = readln().toInt() + map = Array(N) { "" } + for (i in 0 until N) { + map[i] = readln() + } + + val sectionCount = findSectionCount() + + val colorWeakSectionCount = findColorWeakSectionCount() + + StringBuilder().apply { + append(sectionCount) + append(" ") + append(colorWeakSectionCount) + println(this) + } + } + + fun findSectionCount(): Int { + val stack = Stack>() + val isVisited = Array(N) { BooleanArray(N) } + var count = 0 + + for (i in 0 until N) { + for (j in 0 until N) { + if (isVisited[i][j]) continue + count++ + stack.add(i to j) + isVisited[i][j] = true + while (stack.isNotEmpty()) { + val (x, y) = stack.pop() + offsets.forEach { (offsetX, offsetY) -> + val nX = x + offsetX + val nY = y + offsetY + if (nX in 0 until N && nY in 0 until N && !isVisited[nX][nY] && map[i][j] == map[nX][nY]) { + isVisited[nX][nY] = true + stack.add(nX to nY) + } + } + } + + } + } + return count + } + + fun findColorWeakSectionCount(): Int { + + for (i in 0 until N) { + map[i] = map[i].replace('G', 'R') + } + + return findSectionCount() + } + + +} + +fun main() { + BOJ10026().solve() +} \ No newline at end of file