-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblack.java
62 lines (51 loc) · 1.35 KB
/
black.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
/*
Given N * M field of O's and X's, where O=white, X=black
Return the number of black shapes. A black shape consists of one or more adjacent X's (diagonals not included)
Example:
OOOXOOO
OOXXOXO
OXOOOXO
answer is 3 shapes are :
(i) X
X X
(ii)
X
(iii)
X
X
Note that we are looking for connected shapes here.
For example,
XXX
XXX
XXX
is just one single connected black shape.
*/
public int black(ArrayList<String> a) {
int len=0;
if(a.isEmpty() || a==null)
return 0;
visited = new int[a.size()][a.get(0).length()];
for( int i =0; i<a.size(); i++)
{
for( int j =0; j<a.get(i).length() ;j ++)
visited[i][j] =0;
}
for( int i =0; i<a.size(); i++)
for( int j =0; j<a.get(i).length(); j++)
{
if(visited[i][j]!=1 && a.get(i).charAt(j)=='X') {
blackHelper(a, i, j);
len++;
}
}
return len;
}
void blackHelper(ArrayList<String> a,int i,int j) {
if(i>a.size() || i<0|| j>a.get(i).length() || j<0 || visited[i][j]==1 || a.get(i).charAt(j)=='O')
return;
visited[i][j] =1;
blackHelper(a,i-1,j);
blackHelper(a,i+1,j);
blackHelper(a,i,j-1);
blackHelper(a,i,1);
}