People Counting

Description

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

AC代码:

//AC
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#define mod 998244353;
#define Max 0x3f3f3f3f;
#define Min 0xc0c0c0c0;
using namespace std;
typedef long long ll;
const int maxn=100005;
int n,m;
char str[105][105];
int judge(int x,int y)                  //人物的任意部分符合条件均可(该函数描绘人物图形)
{
    if(x>=1 && x<=n && y+1>=1 && y+1<=m && str[x][y+1]=='O')
    {
        return 1;
    }
    if(x+1>=1 && x+1<=n && y>=1 && y<=m && str[x+1][y]=='/')
    {
        return 1;
    }
    if(x+1>=1 && x+1<=n && y+1>=1 && y+1<=m && str[x+1][y+1]=='|')
    {
        return 1;
    }
    if(x+1>=1 && x+1<=n && y+2>=1 && y+2<=m && str[x+1][y+2]=='\\')
    {
        return 1;
    }
    if(x+2>=1 && x+2<=n && y>=1 && y<=m && str[x+2][y]=='(')
    {
        return 1;
    }
    if(x+2>=1 && x+2<=n && y+2>=1 && y+2<=m && str[x+2][y+2]==')')
    {
        return 1;
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>str[i][j];
            }
        }
        int cnt=0;
        for(int i=-1;i<=n;i++)
        {
            for(int j=-1;j<=m;j++)
            {
                if(judge(i,j))      //统计人物数量
                {
                    cnt++;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/82188720