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
题意:给你一张照片,根据题目所定义的人,然后判断该照片中有多少个人;
思路:把给出的照片矩阵向优秀各移动2个单位(考虑有人只被拍到身体的一小部分),然后用3*3的人矩阵去匹配照片,每个被匹配到的位置就变成背景('.'代表背景),输出匹配数就好了。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(2*1e2)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
char s[N][N];
char ren[3][3]={
{'#','O','#'},//'.'是背景不需要匹配,直接初始化为'#'
{'/','|','\\'},
{'(','#',')'}
};
bool update(int x,int y)
{
int cout=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(ren[i][j]==s[x-2+i][y-2+j])
{
s[x-2+i][y-2+j]='.';
cout++;
}
}
}
return cout>0;
}
int main()
{
#ifdef CDZSC_June
freopen("i.txt", "r", stdin);
int _time_jc = clock();
#endif
int t,n,m,max1,sum,p;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int ans=0;
memset(s,0,sizeof(s));
for(int i=2;i<=n+1;i++)
{
scanf("%s",s[i]+2);
}
for(int i=2;i<=n+3;i++)
{
for(int j=2;j<=m+3;j++)
{
if(s[i][j]!='.')
{
if(update(i,j))
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}