-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectedCellsInAGrid.java
More file actions
66 lines (54 loc) · 1.82 KB
/
Copy pathConnectedCellsInAGrid.java
File metadata and controls
66 lines (54 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author rn-sshawish
*/
public class ConnectedCellsInAGrid {
static int n;
static int m;
public static void main(String[] args) throws Exception {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(input.readLine());
m = Integer.parseInt(input.readLine());
String[][] board = new String[n][];
for (int i = 0; i < n; i++) {
board[i] = input.readLine().split(" ");
}
System.out.println(floodFill(board));
}
public static int floodFill(String[][] board) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!board[i][j] .equals("1") ) {
continue;
}
board[i][j] = "0";
max = Math.max(visit(i, j, board) + 1, max);
}
}
return max;
}
public static int visit(int x, int y, String[][] board) {
int[] xValue = {1, 1, 0, -1, -1, 0, -1, 1};
int[] yValue = {1, 0, 1, -1, 0, -1, 1, -1};
int sum = 0;
for (int k = 0; k < xValue.length; k++) {
if (check(x + xValue[k], y + yValue[k]) && !board[x + xValue[k]][y + yValue[k]].equals("0")) {
sum++;
board[x + xValue[k]][y + yValue[k]] = "0";
sum+=visit(x + xValue[k], y + yValue[k], board);
}
}
return sum;
}
public static boolean check(int x, int y) {
return x > -1 && x < n && y > -1 && y < m;
}
}