Skip to content

Commit

Permalink
2024-03-28 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed Mar 28, 2024
1 parent f8bc67d commit acccdac
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SeongHoonC/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
| 14์ฐจ์‹œ | 2024.03.06 | dfs | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/64064"></a> |https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/50 |
| 15์ฐจ์‹œ | 2024.03.09 | ๋ถ„ํ• ์ •๋ณต | <a href="https://www.acmicpc.net/problem/1629"></a> |https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/55 |
| 16์ฐจ์‹œ | 2024.03.14 | ๋ˆ„์ ํ•ฉ | <a href="https://www.acmicpc.net/problem/11659"></a> |https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/60 |
| 17์ฐจ์‹œ | 2024.03.17 | ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ | <a href="https://www.acmicpc.net/problem/1504"></a> |https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/62 |
| 18์ฐจ์‹œ | 2024.03.28 | ๊ตฌํ˜„ | <a href="https://www.acmicpc.net/problem/16234"></a> |https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/70 |
---
90 changes: 90 additions & 0 deletions SeongHoonC/๊ตฌํ˜„/์ธ๊ตฌ์ด๋™.kt
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
}

0 comments on commit acccdac

Please sign in to comment.