File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55### 성능 요약
66
7- 메모리: 34088 KB, 시간: 76 ms
7+ 메모리: 12524 KB, 시간: 164 ms
88
99### 분류
1010
11- 너비 우선 탐색 , 그래프 이론, 그래프 탐색
11+ 그래프 이론 , 그래프 탐색, 너비 우선 탐색, 격자 그래프
1212
1313### 제출 일자
1414
15- 2024년 4월 9일 14: 12:38
15+ 2026년 2월 11일 12:47:26
1616
1717### 문제 설명
1818
Original file line number Diff line number Diff line change 1+ /* /dev/stdin */
2+ let fs = require ( "fs" ) ;
3+ let input = fs
4+ . readFileSync ( "/dev/stdin" )
5+ . toString ( )
6+ . split ( "\n" )
7+ . map ( ( x ) => x . replaceAll ( "\r" , "" ) ) ;
8+
9+ const [ N , M ] = input [ 0 ] . split ( " " ) . map ( Number ) ;
10+
11+ const maze = input . slice ( 1 ) . map ( ( x ) => x . split ( "" ) . map ( Number ) ) ;
12+ const visited = Array . from ( { length : N } , ( ) => Array ( M ) . fill ( false ) ) ;
13+ const queue = [ ] ;
14+ queue . push ( [ 0 , 0 , 1 ] ) ;
15+ visited [ 0 ] [ 0 ] = true ;
16+
17+ while ( queue . length > 0 ) {
18+ const [ x , y , dist ] = queue . shift ( ) ;
19+ if ( x === N - 1 && y === M - 1 ) {
20+ console . log ( dist ) ;
21+ break ;
22+ }
23+ //상
24+ if ( x - 1 >= 0 && ! visited [ x - 1 ] [ y ] && maze [ x - 1 ] [ y ] === 1 ) {
25+ visited [ x - 1 ] [ y ] = true ;
26+ queue . push ( [ x - 1 , y , dist + 1 ] ) ;
27+ }
28+
29+ //우
30+ if ( y + 1 < M && ! visited [ x ] [ y + 1 ] && maze [ x ] [ y + 1 ] === 1 ) {
31+ visited [ x ] [ y + 1 ] = true ;
32+ queue . push ( [ x , y + 1 , dist + 1 ] ) ;
33+ }
34+
35+ //하
36+ if ( x + 1 < N && ! visited [ x + 1 ] [ y ] && maze [ x + 1 ] [ y ] === 1 ) {
37+ visited [ x + 1 ] [ y ] = true ;
38+ queue . push ( [ x + 1 , y , dist + 1 ] ) ;
39+ }
40+ //좌
41+ if ( y - 1 >= 0 && ! visited [ x ] [ y - 1 ] && maze [ x ] [ y - 1 ] === 1 ) {
42+ visited [ x ] [ y - 1 ] = true ;
43+ queue . push ( [ x , y - 1 , dist + 1 ] ) ;
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments