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 ) ;
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 relation = input . slice ( 1 ) ;
13+ const graph = Array . from ( { length : N + 1 } ) . map ( ( _ ) =>
14+ Array . from ( { length : 0 } )
15+ ) ;
16+
17+ for ( const [ f1 , f2 ] of relation ) {
18+ graph [ f1 ] . push ( f2 ) ;
19+ graph [ f2 ] . push ( f1 ) ;
20+ }
21+
22+ function dfs ( start , target ) { }
23+
24+ for ( let i = 1 ; i <= N ; i ++ ) {
25+ const temp = [ ] ;
26+ for ( let j = 1 ; j <= N ; j ++ ) {
27+ if ( graph [ i ] . includes ( j ) ) temp . push ( 1 ) ;
28+ else dfs ( i , j ) ;
29+ }
30+ }
31+ }
32+
33+ console . log ( solution ( input ) ) ;
You can’t perform that action at this time.
0 commit comments