240525 쉬운 최단거리

Jongleee·2024년 5월 25일
0

TIL

목록 보기
582/737
private static final int[] DX = {1, 0, -1, 0};
private static final int[] DY = {0, 1, 0, -1};

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String[] input = br.readLine().split(" ");
	int n = Integer.parseInt(input[0]);
	int m = Integer.parseInt(input[1]);
	
	int[][] distance = new int[n][m];
	boolean[][] blocked = new boolean[n][m];
	int[] queueY = new int[n * m];
	int[] queueX = new int[n * m];
	int front = 0;
	int back = 0;
	
	StringBuilder sb = new StringBuilder((n * m) * 2);

	for (int i = 0; i < n; i++) {
		String line = br.readLine();
		for (int j = 0; j < m; j++) {
			switch (line.charAt(j * 2)) {
				case '0':
					blocked[i][j] = true;
					break;
				case '2':
					distance[i][j] = 1;
					queueY[back] = i;
					queueX[back++] = j;
					break;
				default:
					break;
			}
		}
	}

	while (front < back) {
		int y = queueY[front];
		int x = queueX[front++];
		
		for (int d = 0; d < 4; d++) {
			int nextY = y + DY[d];
			int nextX = x + DX[d];
			
			if (nextY < 0 || nextX < 0 || nextY >= n || nextX >= m) continue;
			if (blocked[nextY][nextX] || distance[nextY][nextX] > 0) continue;
			
			distance[nextY][nextX] = distance[y][x] + 1;
			queueY[back] = nextY;
			queueX[back++] = nextX;
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (distance[i][j] > 0) {
				sb.append(distance[i][j] - 1).append(' ');
			} else if (blocked[i][j]) {
				sb.append("0 ");
			} else {
				sb.append("-1 ");
			}
		}
		sb.append('\n');
	}

	System.out.print(sb);
}

출처:https://www.acmicpc.net/problem/14940

0개의 댓글