-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackShapes.cpp
More file actions
57 lines (57 loc) · 1.66 KB
/
Copy pathblackShapes.cpp
File metadata and controls
57 lines (57 loc) · 1.66 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
#include<iostream>
#include<vector>
void bfs(std::vector<std::string> & A, int rowIndex, int colIndex){
A[rowIndex][colIndex] = 'O';
//move left , right, up and down
//moving up
if(rowIndex-1>=0 && A[rowIndex-1][colIndex]=='X'){
bfs(A,rowIndex-1,colIndex);
}
//moving down
if(rowIndex+1<A.size() && A[rowIndex+1][colIndex]=='X'){
bfs(A,rowIndex+1, colIndex);
}
//moving left
if(colIndex-1>=0 && A[rowIndex][colIndex-1]=='X'){
bfs(A,rowIndex,colIndex-1);
}
//moving right
if(colIndex+1 < A[rowIndex].size() && A[rowIndex][colIndex+1]=='X'){
bfs(A,rowIndex,colIndex+1);
}
}
int solve(std::vector<std::string> & A){
//find connected components
//'X' means shape
//'Y' means void
/*std::vector<std::string> visited = A;
for(int rowIndex = 0;rowIndex<A.size();++rowIndex){
for(int colIndex = 0;colIndex<A[rowIndex].size();++colIndex){
if(visited[rowIndex][colIndex]=='X'){
visited[rowIndex][colIndex] = '0';
}
}
}*/
int count = 0;
for(int rowIndex = 0;rowIndex<A.size();++rowIndex){
for(int colIndex = 0;colIndex<A[rowIndex].size();++colIndex){
//if(A[rowIndex][colIndex]=='X' && visited[rowIndex][colIndex]=='0'){
if(A[rowIndex][colIndex]=='X'){
bfs(A,rowIndex,colIndex);
++count;
}
}
}
return count;
}
int main(){
int N;
std::cin>>N;
std::vector<std::string> vec(N);
for(int index = 0;index<N;++index){
std::cin>>vec[index];
}
int output = solve(vec);
std::cout<<output<<std::endl;
return 0;
}