Skip to content

Commit

Permalink
solve(BOJ): G5_10026_적록색약_kt
Browse files Browse the repository at this point in the history
  • Loading branch information
gogumaC committed Jan 22, 2024
1 parent d62e24b commit fa03c78
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/boj/G5_10026_적록색약.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package boj

import java.util.*

class BOJ10026() {
var N = 0
lateinit var map: Array<String>

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<Pair<Int, Int>>()
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()
}

0 comments on commit fa03c78

Please sign in to comment.