-
[BOJ] 14940 쉬운 최단거리ALGORITHM/BOJ 2023. 6. 6. 21:26
https://www.acmicpc.net/problem/14940
2023-06-06
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main14940 {public static int[] dx = {-1, 0, 1, 0};public static int[] dy = {0, -1, 0, 1};public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int[][] arr = new int[n][m];Queue<int[]> q = new LinkedList<>();int[][] ans = new int[n][m];for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {arr[i][j] = sc.nextInt();if(arr[i][j] == 2) q.add(new int[]{i, j});}}int count = 1;while(!q.isEmpty()) {int size = q.size();while(size > 0) {int[] tmp = q.poll();int x = tmp[0];int y = tmp[1];for(int i = 0; i < 4; i++) {int nx = x + dx[i];int ny = y + dy[i];if(nx < 0 || ny < 0 || nx >= n || ny >= m || arr[nx][ny] == 0 || arr[nx][ny] == 2 || ans[nx][ny] != 0) continue;ans[nx][ny] = count;q.add(new int[]{nx, ny});}size--;}count++;}StringBuilder sb = new StringBuilder();for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {if(arr[i][j] == 1 && ans[i][j] == 0) sb.append("-1").append(" ");else sb.append(ans[i][j]).append(" ");}sb.append("\n");}System.out.println(sb.toString());}}cs #문제풀이
상하좌우 / BFS
'ALGORITHM > BOJ' 카테고리의 다른 글
[BOJ] 5212 지구 온난화 (0) 2023.06.18 [BOJ] 18110 solved.ac (0) 2023.06.13 [BOJ] 16948 데스 나이트 (1) 2023.06.04 [BOJ] 1389 케빈 베이컨의 6단계 법칙 (0) 2023.05.21 [백준] 2660 회장뽑기 (0) 2022.08.15