Skip to content

Commit

Permalink
Merge pull request #35 from AlgoLeadMe/9-SeongHoonC
Browse files Browse the repository at this point in the history
9-SeongHoonC
  • Loading branch information
SeongHoonC authored Feb 23, 2024
2 parents 5c6b4eb + 9174f00 commit fc01bf6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions SeongHoonC/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
| 6์ฐจ์‹œ | 2024.02.05 | ํ/์Šคํƒ | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/42587"></a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/20 |
| 7์ฐจ์‹œ | 2024.02.08 | DP | <a href="https://www.acmicpc.net/problem/1932"></a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/23 |
| 8์ฐจ์‹œ | 2024.02.14 | ์ •๋ ฌ | <a href="https://www.acmicpc.net/problem/18870"></a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/26 |
| 9์ฐจ์‹œ | 2024.02.21 | bfs | <a href="https://www.acmicpc.net/problem/21736"></a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/35 |
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.BufferedReader
import java.io.InputStreamReader

val dx = listOf(1, -1, 0, 0)
val dy = listOf(0, 0, 1, -1)

fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val (n, m) = br.readLine().split(" ").map { it.toInt() }

// n * m graph
val graph = Array(n) { Array(m) { "" } }
var start: Pair<Int, Int> = Pair(-1, -1)
for (i in 0 until n) {
val line = br.readLine().toList().map { it.toString() }
for (j in line.indices) {
graph[i][j] = line[j]
if (line[j] == "I") {
start = Pair(i, j)
}
}
}

// n * m visited
val visited = Array(n) { Array(m) { false } }
visited[start.first][start.second] = true

// ํ ์ƒ์„ฑ
val q = ArrayDeque<Pair<Int, Int>>()
q.add(start)

var count = 0

while (q.isNotEmpty()) {
val now = q.removeFirst()
for (i in 0..3) {
val nextX = now.first + dx[i]
val nextY = now.second + dy[i]

// ์บ ํผ์Šค ๋ฐ–์„ ๋‚˜๊ฐ„๋‹ค
if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) {
continue
}
// ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๊ฑฐ๋‚˜ ๋ฒฝ์€ ๋ชป๊ฐ
if (visited[nextX][nextY] || graph[nextX][nextY] == "X") {
continue
}
// ์‚ฌ๋žŒ์ด๋ฉด ์นœ๊ตฌํ•˜๊ธฐ
if (graph[nextX][nextY] == "P") {
count++
}
// ๋ฐฉ๋ฌธ ํ‘œ์‹œ ํ›„ ํ์— ์ถ”๊ฐ€
visited[nextX][nextY] = true
q.add(Pair(nextX, nextY))
}
}
println(if (count > 0) count else "TT")
}

0 comments on commit fc01bf6

Please sign in to comment.