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.
InputThere are multiple test cases. The first line of input contains an integer Tindicating 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.
<h4< dd="">OutputFor each test case, there should be a single line, containing an integer indicating the number of people from the photo.
<h4< dd="">Sample Input2 3 3 .O. /|\ (.) 3 4 OOO( /|\\ ()))<h4< dd="">Sample Output
1
4
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
char maps[110][110];
int n,m;
void gaibian(int x,int y)
{
if(maps[x][y]=='O'){
if(x+1<=n&&maps[x+1][y]=='|') maps[x+1][y] = '1';
if(x+1<=n&&y-1>=1&&maps[x+1][y-1]=='/') maps[x+1][y-1] = '1';
if(x+1<=n&&y+1<=m&&maps[x+1][y+1]=='\\') maps[x+1][y+1] = '1';
if(x+2<=n&&y-1>=1&&maps[x+2][y-1]=='(') maps[x+2][y-1] = '1';
if(x+2<=n&&y+1<=m&&maps[x+2][y+1]==')') maps[x+2][y+1] = '1';
}
if(maps[x][y]=='/'){
if(y+1<=m&&maps[x][y+1]=='|') maps[x][y+1] = '1';
if(y+2<=m&&maps[x][y+2]=='\\') maps[x][y+2] = '1';
if(x+1<=n&&maps[x+1][y] == '(') maps[x+1][y] = '1';
if(x+1<=n&&y+2<=m&&maps[x+1][y+2] == ')') maps[x+1][y+2] = '1';
}
if(maps[x][y]=='|'){
if(y+1<=m&&maps[x][y+1]=='\\') maps[x][y+1]='1';
if(y-1>=1&&x+1<=n&&maps[x+1][y-1]=='(') maps[x+1][y-1]='1';
if(y+1<=m&&x+1<=n&&maps[x+1][y+1]==')') maps[x+1][y+1] = '1';
}
if(maps[x][y]=='\\'){
if(y-2>=1&&x+1<=n&&maps[x+1][y-2]=='(') maps[x+1][y-2]='1';
if(x+1<=n&&maps[x+1][y]==')') maps[x+1][y] = '1';
}
if(maps[x][y]=='('){
if(y+2<=m&&maps[x][y+2]==')') maps[x][y+2] = '1';
}
}
int main()
{
int t,num,ans,i,j;
cin>>t;
while(t--){
ans=0;
cin>>n>>m;
getchar();
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%c",&maps[i][j]);
}
getchar();
}
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(maps[i][j]!='1'&&maps[i][j]!='.'){
ans++;
gaibian(i,j);
}
}
}
cout<<ans<<endl;
}
return 0;
}
本文介绍了一种通过分析字符矩阵来计数照片中人数的方法。该方法将照片转换为字符矩阵,通过特定图案识别每个人的位置,从而实现自动计数。文章提供了一个具体的示例,并附带了用于计数的C++代码。
935

被折叠的 条评论
为什么被折叠?



