I - People Counting

I - People Counting

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status Practice ZOJ 3944

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.

扫描二维码关注公众号,回复: 3644730 查看本文章

Sample Input

2
3 3
.O.
/|\
(.)
3 4
OOO(
/|\\
()))

Sample Output

1
4

题目的意思是要我们找出给出照片有多少个人,需要注意的是,照片中可能只给出了人的一部分,我们也需要把这个算进去。

我们知道,一个人只有一个头(别的部分也可以),我们可以把别的部分的位置映射到头的位置。也就是说,我们假设找到了左手的位置,那么我们可以根据左手的位置从而算出头的位置,把所有算出的头的位置放在set里去重,就可以很快速的得到照片中的人。

#include <iostream>
#include <cstdio>
#include <set>
#include <map>
using namespace std;

char photo[105][105];
set<pair<int, int> > ans;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int h, w;
        scanf("%d%d", &h, &w);
        //cout << h << endl;
        for (int i = 0; i < h;i++)
        {
            scanf("%s",photo[i]);
        }
        ans.clear();
        for(size_t i = 0; i < h; i++)
        {
            for(size_t j = 0; j < w; j++)
            {
                switch (photo[i][j])
                {
                    case 'O':
                        ans.insert(make_pair(i, j));
                        break;
                    case '/':
                        ans.insert(make_pair(i - 1, j + 1));
                        break;
                    case '|':
                        ans.insert(make_pair(i - 1, j));
                        break;
                    case '\\':
                        ans.insert(make_pair(i - 1, j - 1));
                        break;
                    case '(':
                        ans.insert(make_pair(i - 2, j+1));
                        break;
                    case ')':
                        ans.insert(make_pair(i - 2, j - 1));
                        break;
                }
            }
        }
        printf("%d\n", ans.size());
    }
    //system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40620465/article/details/82217122