forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
91 lines (81 loc) · 2.9 KB
/
Solution.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.io.*;
import java.lang.*;
import java.util.*;
class Node {
public int i,j;
public Node(int i, int j) {
this.i = i;
this.j = j;
}
public static boolean isValid(int i, int j, int n, int m) {
return (0 <= i && i < n && 0 <= j && j < m);
}
}
public class Solution {
public static int[][] dfs(int[][] adj, int i, int j, int n, int m) {
Stack<Node> s = new Stack<Node>();
int counter = 0;
s.push(new Node(i ,j));
while (!s.empty()) {
counter++;
Node t = s.pop();
if (adj[t.i][t.j] < counter) { adj[t.i][t.j] = counter; }
if (Node.isValid(t.i-1, t.j-1, n, m) && adj[t.i-1][t.j-1] == 0) {
adj[t.i-1][t.j-1] = counter;
s.push(new Node(t.i-1, t.j-1));
}
if (Node.isValid(t.i-1, t.j, n, m) && adj[t.i-1][t.j] == 0) {
adj[t.i-1][t.j] = counter;
s.push(new Node(t.i-1, t.j));
}
if (Node.isValid(t.i-1, t.j+1, n, m) && adj[t.i-1][t.j+1] == 0) {
adj[t.i-1][t.j+1] = counter;
s.push(new Node(t.i-1, t.j+1)); }
if (Node.isValid(t.i, t.j-1, n, m) && adj[t.i][t.j-1] == 0) {
adj[t.i][t.j-1] = counter;
s.push(new Node(t.i, t.j-1)); }
if (Node.isValid(t.i, t.j+1, n, m) && adj[t.i][t.j+1] == 0) {
adj[t.i][t.j+1] = counter;
s.push(new Node(t.i, t.j+1)); }
if (Node.isValid(t.i+1, t.j-1, n, m) && adj[t.i+1][t.j-1] == 0) {
adj[t.i+1][t.j-1] = counter;
s.push(new Node(t.i+1, t.j-1)); }
if (Node.isValid(t.i+1, t.j, n, m) && adj[t.i+1][t.j] == 0) {
adj[t.i+1][t.j] = counter;
s.push(new Node(t.i+1, t.j)); }
if (Node.isValid(t.i+1, t.j+1, n, m) && adj[t.i+1][t.j+1] == 0) {
adj[t.i+1][t.j+1] = counter;
s.push(new Node(t.i+1, t.j+1)); }
}
adj[i][j] = counter;
return adj;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] adj = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int tmp = sc.nextInt();
if (tmp == 1) {
adj[i][j] = 0;
} else {
adj[i][j] = -1;
}
}
}
int counter = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (adj[i][j] == 0) {
adj = Solution.dfs(adj, i, j, n, m);
if (adj[i][j] > counter) {
counter = adj[i][j];
}
}
}
}
System.out.println(counter);
}
}