Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18-SeongHoonC #70

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Comment on lines +46 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν‰κ· κ°’κΉŒμ§€ ν•¨μˆ˜ν™”ν•˜μ…”μ„œ λ§Œλ“œμ…¨κ΅°μš”!


// 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
}
Loading