-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8bc67d
commit acccdac
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
import java.util.* | ||
import kotlin.collections.ArrayDeque | ||
import kotlin.math.* | ||
|
||
val dx = listOf(1, -1, 0, 0) | ||
val dy = listOf(0, 0, 1, -1) | ||
fun main() { | ||
val br = BufferedReader(InputStreamReader(System.`in`)) | ||
val (n, l, r) = br.readLine().split(" ").map { it.toInt() } | ||
val grounds = Array(n) { Array(n) { 0 } } | ||
|
||
for (i in 0 until n) { | ||
val line = br.readLine().split(" ").map { it.toInt() } | ||
for (j in 0 until n) { | ||
grounds[i][j] = line[j] | ||
} | ||
} | ||
var count = 0 | ||
// ์ธ๊ตฌ๊ฐ ๋์ด์ ์ด๋ํ ์ ์์ ๋๊น์ง ๋ฐ๋ณต | ||
while (moving(grounds, n, l, r)) { | ||
count++ | ||
} | ||
println(count) | ||
} | ||
|
||
// L ๋ช ์ด์ R ๋ช ์ดํ๋ฉด ์ธ๊ตฌ ์ด๋ํ๊ธฐ | ||
private fun moving(grounds: Array<Array<Int>>, n: Int, l: Int, r: Int): Boolean { | ||
val visited = Array(n) { Array(n) { false } } | ||
|
||
val unities = mutableListOf<List<Pair<Int, Int>>>() | ||
for (i in 0 until n) { | ||
for (j in 0 until n) { | ||
val unity = bfs(grounds, n, visited, i to j, l, r) ?: continue | ||
unities.add(unity) | ||
} | ||
} | ||
if (unities.isEmpty()) return false | ||
|
||
unities.forEach { goAverage(grounds, it) } | ||
|
||
return true | ||
} | ||
|
||
// ์ฐํฉ๋ค์ ํ๊ท ๊ฐ์ผ๋ก ๋ณ๊ฒฝ | ||
private fun goAverage(grounds: Array<Array<Int>>, unity: List<Pair<Int, Int>>) { | ||
val average = unity.sumOf { grounds[it.first][it.second] } / unity.size | ||
unity.forEach { | ||
grounds[it.first][it.second] = average | ||
} | ||
} | ||
|
||
// bfs ๋ก ์ฐํฉ ๋ค ์ฐพ๊ธฐ | ||
private fun bfs( | ||
grounds: Array<Array<Int>>, | ||
n: Int, | ||
visited: Array<Array<Boolean>>, | ||
start: Pair<Int, Int>, | ||
l: Int, | ||
r: Int, | ||
): List<Pair<Int, Int>>? { | ||
if (visited[start.first][start.second]) return null | ||
val deque = ArrayDeque<Pair<Int, Int>>() | ||
val unity = mutableListOf<Pair<Int, Int>>() | ||
deque.add(start) | ||
unity.add(start) | ||
visited[start.first][start.second] = true | ||
while (deque.isNotEmpty()) { | ||
val now = deque.removeFirst() | ||
for (i in 0..3) { | ||
val nextX = now.first + dx[i] | ||
val nextY = now.second + dy[i] | ||
if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= n) { | ||
continue | ||
} | ||
if (visited[nextX][nextY]) { | ||
continue | ||
} | ||
if (abs(grounds[nextX][nextY] - grounds[now.first][now.second]) !in l..r) { | ||
continue | ||
} | ||
visited[nextX][nextY] = true | ||
deque.add(nextX to nextY) | ||
unity.add(nextX to nextY) | ||
} | ||
} | ||
if (unity.size == 1) return null | ||
return unity | ||
} |