14940번: 쉬운 최단거리
https://www.acmicpc.net/problem/14940
# 코드
import java.util.*;
import java.io.*;
public class Main {
private final static int[] DX = { 1, 0, -1, 0 };
private final static int[] DY = { 0, -1, 0, 1 };
private static int[][] map, distance;
private static int m, n;
private static boolean[][] isVisited;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringBuilder builder = new StringBuilder();
boolean isStartChecked = false;
String[] size = reader.readLine().split(" ");
n = Integer.parseInt(size[0]);
m = Integer.parseInt(size[1]);
int startX = -1, startY = -1;
map = new int[n][m];
distance = new int[n][m];
isVisited = new boolean[n][m];
for (int i = 0; i < n; i++) {
map[i] = Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
if (!isStartChecked)
for (int j = 0; j < m; j++)
if (map[i][j] == 2) {
isStartChecked = true;
startX = i;
startY = j;
break;
}
}
bfs(startX, startY);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
if (!isVisited[i][j] && map[i][j] == 1)
builder.append(-1 + " ");
else
builder.append(distance[i][j] + " ");
builder.append("\n");
}
System.out.print(builder.toString());
}
private static void bfs(int x, int y) {
Queue<Point> queue = new LinkedList<>();
queue.add(new Point(x, y));
isVisited[x][y] = true;
while (!queue.isEmpty()) {
Point current = queue.poll();
for (int i = 0; i < 4; i++) {
int nextX = current.x + DX[i];
int nextY = current.y + DY[i];
if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) continue;
if (map[nextX][nextY] == 0) continue;
if (isVisited[nextX][nextY]) continue;
queue.add(new Point(nextX, nextY));
distance[nextX][nextY] = distance[current.x][current.y] + 1;
isVisited[nextX][nextY] = true;
}
}
}
}
class Point {
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
'코딩테스트 > 자바 문제풀이' 카테고리의 다른 글
[CLASS 3: 자료구조] 백준 5430 AC (0) | 2024.11.22 |
---|---|
[CLASS 3: 분할정복] 백준 1074 Z (0) | 2024.11.21 |
[CLASS 3: 최단경로] 백준 11403 경로 찾기 (0) | 2024.11.19 |
[CLASS 3: 브루트포스] 백준 6064 카잉 달력 (1) | 2024.11.18 |
[CLASS 3: 문자열] 백준 5525 IOIOI (0) | 2024.11.17 |