- [백준] 쉬운 최단거리2025년 02월 09일 18시 29분 26초에 업로드 된 글입니다.작성자: do_hyuk
https://www.acmicpc.net/problem/14940
이 문제는 정석적인 BFS 문제라고 볼 수 있다.
중요한 포인트는 BFS 시작점을 (0,0)으로 두지 말고 목표 좌표로 설정해야하는 것이다.
전체코드
import java.io.*; import java.util.*; public class Main { private static int N,M; private static boolean[][] visited; private static int[][] map, result; private static int[] dirX = {1,0,-1,0}; private static int[] dirY = {0,1,0,-1}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()," "); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); visited = new boolean[N][M]; map = new int[N][M]; result = new int[N][M]; int x=0, y = 0; for (int i = 0; i < N; i++) { st = new StringTokenizer(br.readLine()," "); for (int j = 0; j < M; j++) { visited[i][j] = false; map[i][j] = Integer.parseInt(st.nextToken()); if(map[i][j] == 2){ x = i; y = j; } else if(map[i][j] == 0){ visited[i][j] = true; } } } bfs(x,y); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (!visited[i][j]) { result[i][j] = -1; } System.out.print(result[i][j] + " "); } System.out.println(); } } private static void bfs(int x, int y) { Queue<int[]> queue = new LinkedList<>(); queue.add(new int[]{x,y}); visited[x][y] = true; while(!queue.isEmpty()){ int[] temp = queue.poll(); for (int i = 0; i < 4; i++) { int newX = temp[0] + dirX[i]; int newY = temp[1] + dirY[i]; if (newX >= 0 && newX < N && newY >= 0 && newY < M) { if (!visited[newX][newY] && map[newX][newY] == 1) { visited[newX][newY] = true; result[newX][newY] = result[temp[0]][temp[1]] + 1; queue.add(new int[]{newX,newY}); } } } } } }
'코딩문제' 카테고리의 다른 글
[소프티어] 나무조경 (1) 2025.02.06 [소프티어] 택배 마스터 광우 (1) 2025.02.05 [소프티어] 출퇴근길 (1) 2025.02.03 [소프티어] 징검다리 (0) 2025.01.24 [소프티어] 성적평균 (1) 2025.01.23 댓글