File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ // 오늘 너무 피곤하다.. 날먹 dfs...rrr
2+
3+ var numIslands = function ( grid ) {
4+ const dir = [
5+ [ 1 , 0 ] ,
6+ [ - 1 , 0 ] ,
7+ [ 0 , 1 ] ,
8+ [ 0 , - 1 ] ,
9+ ] ;
10+
11+ const bfs = ( y , x ) => {
12+ const q = [ [ y , x ] ] ;
13+ while ( q . length ) {
14+ const [ curRow , curCol ] = q . shift ( ) ;
15+ for ( let i = 0 ; i < dir . length ; i ++ ) {
16+ const ny = curRow + dir [ i ] [ 0 ] ;
17+ const nx = curCol + dir [ i ] [ 1 ] ;
18+ if (
19+ nx >= 0 &&
20+ nx < grid [ 0 ] . length &&
21+ ny >= 0 &&
22+ ny < grid . length &&
23+ grid [ ny ] [ nx ] === "1"
24+ ) {
25+ grid [ ny ] [ nx ] = "0" ;
26+ q . push ( [ ny , nx ] ) ;
27+ }
28+ }
29+ }
30+ } ;
31+ let count = 0 ;
32+ for ( let i = 0 ; i < grid . length ; i ++ ) {
33+ for ( let j = 0 ; j < grid [ 0 ] . length ; j ++ ) {
34+ if ( grid [ i ] [ j ] === "1" ) {
35+ bfs ( i , j ) ;
36+ count ++ ;
37+ }
38+ }
39+ }
40+ return count ;
41+ } ;
You can’t perform that action at this time.
0 commit comments