In a BG (dinner gathering) for ZJU ICPC team, the coaches wanted to count the number of people present at the BG. They did that by having the waitress take a photo for them. Everyone was in the photo and no one was completely blocked. Each person in the photo has the same posture. After some preprocessing, the photo was converted into a H×W character matrix, with the background represented by ".". Thus a person in this photo is represented by the diagram in the following three lines:
.O.
/|\
(.)
Given the character matrix, the coaches want you to count the number of people in the photo. Note that if someone is partly blocked in the photo, only part of the above diagram will be presented in the character matrix.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first contains two integers H, W (1 ≤ H, W ≤ 100) - as described above, followed by H lines, showing the matrix representation of the photo.
Output
For each test case, there should be a single line, containing an integer indicating the number of people from the photo.
Sample Input
2
3 3
.O.
/|\
(.)
3 4
OOO(
/|\\
()))
Sample Output
1
4
<strong><span style="font-size:24px;">#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char people[3][3]={'.','O','.','/','|','\\','(','.',')'};//储存人的姿势
char mp[105][105];
bool vis[105][105];
int n,m;
int judge(int x,int y){
if(x<0||x>=n||y<0||y>=m||vis[x][y])return 0;
return 1;
}
void search_p(int i,int j){
vis[i][j]=1;
if(mp[i][j]=='O'){//如果访问到了一个没有访问过的头,就根据相对位置访问该人的其他位置。
if(judge(i+1,j-1)&&mp[i+1][j-1]=='/')vis[i+1][j-1]=1;
if(judge(i+1,j)&&mp[i+1][j]=='|')vis[i+1][j]=1;
if(judge(i+1,j+1)&&mp[i+1][j+1]=='\\')vis[i+1][j+1]=1;
if(judge(i+2,j-1)&&mp[i+2][j-1]=='(')vis[i+2][j-1]=1;
if(judge(i+2,j+1)&&mp[i+2][j+1]==')')vis[i+2][j+1]=1;
}
if(mp[i][j]=='/'){
if(judge(i,j+1)&&mp[i][j+1]=='|')vis[i][j+1]=1;
if(judge(i,j+2)&&mp[i][j+2]=='\\')vis[i][j+2]=1;
if(judge(i-1,j+1)&&mp[i-1][j+1]=='O')vis[i-1][j+1]=1;
if(judge(i+1,j)&&mp[i+1][j]=='(')vis[i+1][j]=1;
if(judge(i+1,j+2)&&mp[i+1][j+2]==')')vis[i+1][j+2]=1;
}
if(mp[i][j]=='|'){
if(judge(i-1,j)&&mp[i-1][j]=='O')vis[i-1][j]=1;
if(judge(i,j+1)&&mp[i][j+1]=='\\')vis[i][j+1]=1;
if(judge(i,j-1)&&mp[i][j-1]=='/')vis[i][j-1]=1;
if(judge(i+1,j-1)&&mp[i+1][j-1]=='(')vis[i+1][j-1]=1;
if(judge(i+1,j+1)&&mp[i+1][j+1]==')')vis[i+1][j+1]=1;
}
if(mp[i][j]=='\\'){
if(judge(i,j-1)&&mp[i][j-1]=='|')vis[i][j-1]=1;
if(judge(i,j-2)&&mp[i][j-2]=='/')vis[i][j-2]=1;
if(judge(i-1,j-1)&&mp[i-1][j-1]=='O')vis[i-1][j-1]=1;
if(judge(i+1,j)&&mp[i+1][j]==')')vis[i+1][j]=1;
if(judge(i+1,j-2)&&mp[i+1][j-2]=='(')vis[i+1][j-2] =1;
}
if(mp[i][j]=='('){
if(judge(i-1,j+1)&&mp[i-1][j+1]=='|')vis[i-1][j+1]=1;
if(judge(i,j+2)&&mp[i][j+2]==')')vis[i][j+2]=1;
if(judge(i-1,j)&&mp[i-1][j]=='/')vis[i-1][j]=1;
if(judge(i-1,j+2)&&mp[i+1][j]=='\\')vis[i-1][j+2]=1;
if(judge(i-2,j+1)&&mp[i-2][j+1]==')')vis[i-2][j+1]=1;
}
if(mp[i][j]==')'){
if(judge(i-1,j-1)&&mp[i-1][j+1]=='|')vis[i-1][j-1]=1;
if(judge(i,j-2)&&mp[i][j+2]=='(')vis[i][j-2]=1;
if(judge(i-1,j)&&mp[i-1][j]=='\\')vis[i-1][j]=1;
if(judge(i-1,j-2)&&mp[i+1][j]=='/')vis[i-1][j-2]=1;
if(judge(i-2,j-1)&&mp[i-2][j-1]==')')vis[i-2][j-1]=1;
}
}
int main(){
int cases,i,j,cnt;
cin>>cases;
while(cases--){
memset(vis,0,sizeof(vis));
cnt=0;
cin>>n>>m;
for(i=0;i<n;i++)
scanf("%s",mp[i]);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(!vis[i][j]&&mp[i][j]!='.'){//如果找到一个没有访问过的人的身体部位,计数器加1
cnt++;
search_p(i,j);//寻找该人身体其他部位并且标记出来
}
}
}
cout<<cnt<<endl;
}
return 0;
}</span><span style="font-size:18px;">
</span></strong>