File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const fs = require ( 'fs' ) ;
2+
3+ const input = fs
4+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
5+ . toString ( )
6+ . trim ( )
7+ . split ( '\n' )
8+ . map ( ( el ) => el . split ( ' ' ) . map ( Number ) ) ;
9+
10+ function solution ( input ) {
11+ const [ n , m ] = input [ 0 ] ;
12+ const drawing = input . slice ( 1 ) . map ( ( row ) => [ ...row ] ) ;
13+
14+ const dx = [ - 1 , 1 , 0 , 0 ] ;
15+ const dy = [ 0 , 0 , - 1 , 1 ] ;
16+
17+ let count = 0 ;
18+ let maxSize = 0 ;
19+
20+ function dfs ( x , y ) {
21+ let size = 1 ;
22+ drawing [ x ] [ y ] = 0 ;
23+
24+ for ( let i = 0 ; i < 4 ; i ++ ) {
25+ const nx = x + dx [ i ] ;
26+ const ny = y + dy [ i ] ;
27+
28+ if ( nx >= 0 && nx < n && ny >= 0 && ny < m && drawing [ nx ] [ ny ] === 1 ) {
29+ size += dfs ( nx , ny ) ;
30+ }
31+ }
32+ return size ;
33+ }
34+
35+ for ( let i = 0 ; i < n ; i ++ ) {
36+ for ( let j = 0 ; j < m ; j ++ ) {
37+ if ( drawing [ i ] [ j ] === 1 ) {
38+ count ++ ;
39+ maxSize = Math . max ( maxSize , dfs ( i , j ) ) ;
40+ }
41+ }
42+ }
43+
44+ console . log ( count ) ;
45+ console . log ( maxSize ) ;
46+ }
47+
48+ solution ( input ) ;
You can’t perform that action at this time.
0 commit comments