11import java .util .*;
2-
32class Solution {
4- int [] dx = {-1 , 1 , 0 , 0 }; // 상하좌우 이동을 위한 배열
5- int [] dy = {0 , 0 , -1 , 1 };
6- int n , m ; // 보드의 크기
7-
3+ int [] dx = {-1 ,1 ,0 ,0 };
4+ int [] dy = {0 ,0 ,-1 ,1 };
5+ int row ;
6+ int col ;
7+ boolean [][] visited ;
8+
89 public int solution (String [] board ) {
9- n = board .length ;
10- m = board [0 ].length ();
11- int [] start = new int [ 2 ];
12- int [] goal = new int [ 2 ];
13-
14- // 시작 위치와 목표 위치 찾기
15- for (int i = 0 ; i < n ; i ++) {
16- for ( int j = 0 ; j < m ; j ++) {
17- if ( board [ i ]. charAt ( j ) == 'R' ) {
18- start [ 0 ] = i ;
19- start [ 1 ] = j ;
20- } else if ( board [ i ]. charAt ( j ) == 'G' ) {
21- goal [ 0 ] = i ;
22- goal [ 1 ] = j ;
10+ row = board .length ;
11+ col = board [0 ].length ();
12+ visited = new boolean [ row ][ col ];
13+
14+ for ( int i = 0 ; i < row ; i ++){
15+ String str = board [ i ];
16+ for (int j = 0 ; j < col ; j ++){
17+ char ch = str . charAt ( j );
18+ int [] start = new int [ 2 ];
19+ start = new int []{ i , j } ;
20+ if ( ch == 'R' ){
21+ // bfs 로 최단거리 계산
22+ int cnt = bfs ( board , start ) ;
23+ return cnt ;
2324 }
2425 }
2526 }
26-
27- return bfs ( board , start , goal ) ;
27+
28+ return - 1 ;
2829 }
29-
30- private int bfs (String [] board , int [] start , int [] goal ) {
30+
31+ int bfs (String [] board , int [] start ) {
3132 Queue <int []> queue = new LinkedList <>();
32- boolean [][] visited = new boolean [n ][m ];
33- queue .offer (new int []{start [0 ], start [1 ], 0 }); // x, y, 이동 횟수
3433 visited [start [0 ]][start [1 ]] = true ;
35-
36- while (!queue .isEmpty ()) {
34+
35+ // [x좌표,y좌표,이동 횟수]
36+ queue .offer (new int []{start [0 ],start [1 ],0 });
37+
38+ while (!queue .isEmpty ()){
3739 int [] current = queue .poll ();
38- int x = current [0 ], y = current [1 ], moves = current [2 ];
39-
40- if (x == goal [0 ] && y == goal [1 ]) {
41- return moves ;
40+ int currentX = current [0 ];
41+ int currentY = current [1 ];
42+ int cnt = current [2 ];
43+
44+ // System.out.println("현재 위치: " + currentX+","+currentY);
45+
46+ // 도착
47+ if (board [currentX ].charAt (currentY ) == 'G' ) {
48+ return cnt ;
4249 }
43-
44- for (int i = 0 ; i < 4 ; i ++) {
45- int nx = x , ny = y ;
46- // 해당 방향으로 끝까지 이동
47- while (nx >= 0 && nx < n && ny >= 0 && ny < m && board [nx ].charAt (ny ) != 'D' ) {
48- nx += dx [i ];
49- ny += dy [i ];
50+
51+ // 모든 방향 탐색
52+ for (int i =0 ;i <4 ;i ++){
53+ int nextX = currentX +dx [i ];
54+ int nextY = currentY +dy [i ];
55+
56+ // 격자 범위 이내 & 끝까지 이동
57+ while (nextX >= 0 && nextX <row && nextY >= 0 && nextY <col
58+ && board [nextX ].charAt (nextY ) != 'D' ) {
59+ nextX += dx [i ];
60+ nextY += dy [i ];
5061 }
51- // 벽이나 장애물 직전 위치로 이동
52- nx -= dx [i ];
53- ny -= dy [i ];
54-
55- if (!visited [nx ][ny ]) {
56- visited [nx ][ny ] = true ;
57- queue .offer (new int []{nx , ny , moves + 1 });
62+ // 직전 위치
63+ nextX -= dx [i ];
64+ nextY -= dy [i ];
65+ if (!visited [nextX ][nextY ]) { // 처음 와본 정지구역
66+ queue .offer (new int []{nextX ,nextY , cnt +1 }); // 위치 저장
67+ visited [nextX ][nextY ] = true ; // 방문 처리
5868 }
5969 }
6070 }
61-
62- return -1 ; // 목표에 도달할 수 없는 경우
71+ return -1 ;
6372 }
6473}
0 commit comments