【ZOJ3944】People Counting(思维)

题目链接

People Counting


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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列,所以扩大后可以搜照片中的每一个位置,将每个位置放一个人,看是否有一个部分能够对上,如果可以则ans++。

这里还有个注意点就是‘/’的问题,需要用'//'表示。

【代码】

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,ans=0;
        char s[125][125];
        scanf("%d%d",&n,&m);
        memset(s,'#',sizeof(s));
        for(int i=2;i<n+2;i++)
            scanf("%s",s[i]+2);
        for(int i=0;i<n+4;i++)
        {
            for(int j=0;j<m+4;j++)
            {
                    if (s[i][j+1]=='O' ||s[i+1][j]=='/' ||s[i+1][j+1]=='|' ||
                        s[i+1][j+2]=='\\' ||s[i+2][j]=='('||s[i+2][j+2]==')')
					    ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/82216377
今日推荐