File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# [ level 2] 게임 맵 최단거리 - 1844
22
3- [ 문제 링크] ( https://school.programmers.co.kr/learn/courses/30/lessons/1844 )
3+ [ 문제 링크] ( https://school.programmers.co.kr/learn/courses/30/lessons/1844?language=java )
44
55### 성능 요약
66
7- 메모리: 9.48 MB, 시간: 10.45 ms
7+ 메모리: 57.9 MB, 시간: 3.46 ms
88
99### 구분
1010
1616
1717### 제출 일자
1818
19- 2025년 12월 31일 13:49:06
19+ 2026년 05월 08일 19:53:54
2020
2121### 문제 설명
2222
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public int solution (int [][] maps ) {
5+ int n = maps .length ;
6+ int m = maps [0 ].length ;
7+ boolean [][] visited = new boolean [n ][m ];
8+
9+ Queue <int []> q = new LinkedList <>();
10+ q .add (new int [] {0 , 0 , 1 });
11+ visited [0 ][0 ] = true ;
12+
13+ int [] dx = {-1 , 1 , 0 , 0 };
14+ int [] dy = {0 , 0 , 1 , -1 };
15+
16+ while (!q .isEmpty ()){
17+ int [] curr = q .poll ();
18+ int x = curr [0 ];
19+ int y = curr [1 ];
20+ int dist = curr [2 ];
21+
22+ if (x == n -1 && y == m -1 ) return dist ;
23+
24+ for (int i = 0 ; i < 4 ; i ++){
25+ int nx = x + dx [i ];
26+ int ny = y + dy [i ];
27+
28+ if (nx >= 0 && nx < n && ny >= 0 && ny < m && maps [nx ][ny ] == 1 && !visited [nx ][ny ]){
29+ q .add (new int [] {nx , ny , dist +1 });
30+ visited [nx ][ny ] = true ;
31+ }
32+ }
33+ }
34+ return -1 ;
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments