From d6cf6de057f38af66e85d3d2e9db05006cb4ba34 Mon Sep 17 00:00:00 2001 From: vrexpert Date: Sun, 3 Mar 2024 21:55:40 +0900 Subject: [PATCH] 2024-03-03 solved --- ...34\353\213\250\352\261\260\353\246\254.kt" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "SeongHoonC/bfs/\354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254.kt" diff --git "a/SeongHoonC/bfs/\354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254.kt" "b/SeongHoonC/bfs/\354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254.kt" new file mode 100644 index 0000000..2e1d1c6 --- /dev/null +++ "b/SeongHoonC/bfs/\354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254.kt" @@ -0,0 +1,66 @@ +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() } + + var start = 0 to 0 + val graph = Array(n) { Array(m) { 0 } } + for (i in 0 until n) { + val line = br.readLine().split(" ").map { it.toInt() } + for (j in 0 until m) { + if (line[j] == 2) { + start = i to j + graph[i][j] = 0 + continue + } + graph[i][j] = line[j] + } + } + + val q = ArrayDeque>() + val visited = Array(n) { Array(m) { false } } + q.add(start) + + 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 >= n || nextY >= m || nextX < 0 || nextY < 0) { + continue + } + + if (graph[nextX][nextY] == 0) { + continue + } + + if (visited[nextX][nextY]) { + continue + } + + graph[nextX][nextY] = graph[now.first][now.second] + 1 + visited[nextX][nextY] = true + q.add(nextX to nextY) + } + } + + for (i in 0 until n) { + for (j in 0 until m) { + if (visited[i][j] || graph[i][j] == 0) { + continue + } + graph[i][j] = -1 + } + } + + for (i in 0 until n) { + println(graph[i].joinToString(" ")) + } +}