Skip to content

Commit a43f504

Browse files
committed
hackerrank submission
1 parent 04c64ee commit a43f504

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
public static boolean[][] visited;
9+
public static void main(String[] args) {
10+
Scanner scan = new Scanner(System.in);
11+
int M = scan.nextInt();
12+
int N = scan.nextInt();
13+
int[][] grid = new int[M][N];
14+
visited = new boolean[M][N];
15+
for(int i = 0; i < M; i++) {
16+
for(int j = 0; j < N; j++) {
17+
grid[i][j] = scan.nextInt();
18+
visited[i][j] = false;
19+
}
20+
}
21+
int max = 0;
22+
for(int i = 0; i < M; i++) {
23+
for(int j = 0; j < N; j++) {
24+
if(!visited[i][j]) max = Math.max(max, findZoneHelper(grid, i, j, 0, M, N));
25+
}
26+
}
27+
System.out.println(max);
28+
29+
}
30+
31+
public static int findZoneHelper(int[][] grid, int i, int j, int count, int M, int N) {
32+
if(i < 0 || j < 0 || i >= M || j >= N) return 0;
33+
if(visited[i][j]) return 0;
34+
visited[i][j] = true;
35+
if(grid[i][j] == 0) return 0;
36+
else return 1 +
37+
findZoneHelper(grid, i-1, j-1, count, M, N) +
38+
findZoneHelper(grid, i-1, j, count, M, N) +
39+
findZoneHelper(grid, i-1, j+1, count, M, N) +
40+
findZoneHelper(grid, i, j-1, count, M, N) +
41+
findZoneHelper(grid, i, j, count, M, N) +
42+
findZoneHelper(grid, i, j+1, count, M, N) +
43+
findZoneHelper(grid, i+1, j-1, count, M, N) +
44+
findZoneHelper(grid, i+1, j, count, M, N) +
45+
findZoneHelper(grid, i+1, j+1, count, M, N);
46+
}
47+
}

0 commit comments

Comments
 (0)