@@ -3,57 +3,50 @@ class Solution {
33 static int [] dx = {-1 ,1 ,0 ,0 };
44 static int [] dy = {0 ,0 ,-1 ,1 };
55 static char [][] arr ;
6- static int X , Y ;
6+ static int n , m ;
77
88 public int solution (String [] board ) {
9- X = board .length ; // 행
10- Y = board [0 ].length (); // 열
11-
12- arr = new char [X ][ Y ];
9+ m = board .length ; // 행
10+ n = board [0 ].length (); // 열
11+
12+ arr = new char [m ][ n ];
1313
1414 int startX =0 , startY =0 ;
15- for (int i =0 ; i <X ; i ++){
16- for (int j =0 ; j <Y ; j ++){
15+ for (int i =0 ; i <m ; i ++){
16+ for (int j =0 ; j <n ; j ++ ){
1717 arr [i ][j ]=board [i ].charAt (j );
18- if (arr [i ][j ]=='R' ){ // 출발점 찾기
19- startX =j ;
20- startY =i ;
18+ if (arr [i ][j ]=='R' ){
19+ startX =j ; startY =i ;
2120 }
2221 }
2322 }
24- return bfs (startY ,startX ); // 행, 열 순서로 넘기기
25- }
23+ return bfs (startY , startX , arr );
2624
27- static int bfs (int sy , int sx ){
28- boolean [][] visited = new boolean [X ][Y ];
29- visited [sy ][sx ]=true ;
25+ }
26+ static int bfs (int y , int x , char [][] arr ){
27+ boolean [][] visited = new boolean [m ][n ];
28+ visited [y ][x ] = true ;
3029 Queue <int []> q = new LinkedList <>();
31- q .offer (new int []{sy , sx ,0 });
32-
30+ q .offer (new int []{y , x ,0 });
31+
3332 while (!q .isEmpty ()){
34- int [] pre = q .poll ();
35- int preY = pre [0 ];
36- int preX = pre [1 ];
37- int cnt = pre [2 ];
38-
39- if (arr [preY ][preX ]=='G' ) return cnt ;
40-
41- for (int d =0 ; d <4 ; d ++){
42- int nowY = preY ;
43- int nowX = preX ;
44-
45- // 미끄러지기
33+ int []pre = q .poll ();
34+ int py = pre [0 ], px =pre [1 ], cnt =pre [2 ];
35+
36+ if (arr [py ][px ] == 'G' ) return cnt ;;
37+
38+ for (int i =0 ; i <4 ; i ++){
39+ int nowX = px , nowY =py ;
4640 while (true ){
47- int nY = nowY + dx [d ];
48- int nX = nowX + dy [d ];
49- if ( nY < 0 || nY >= X || nX < 0 || nX >= Y ) break ;
50- if ( arr [ nY ][ nX ]== 'D' ) break ;
51- nowY = nY ;
52- nowX = nX ;
41+ int nextX = nowX + dx [i ];
42+ int nextY = nowY + dy [i ];
43+
44+ if ( nextX < 0 || nextX >= n || nextY < 0 || nextY >= m ) break ;
45+ if ( arr [ nextY ][ nextX ] == 'D' ) break ;
46+ nowX = nextX ; nowY = nextY ;
5347 }
54-
5548 if (!visited [nowY ][nowX ]){
56- visited [nowY ][nowX ] = true ;
49+ visited [nowY ][nowX ]= true ;
5750 q .offer (new int []{nowY ,nowX ,cnt +1 });
5851 }
5952 }
0 commit comments