ZOJ - 3944 People Counting (模拟)

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
问题链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3944
问题简述: 根据他给出的判断条件,判断输入的照片中有几个人(一些人可能只在照片中露出几个部位)
问题分析: 刚开始以为只要找到头,身子,左脚右脚左手右手中最多的那个,就是人数,结果一直WA,后来用他给的人的条件,从第一个格子开始判断才给过
AC通过的C++语言程序如下:

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <climits>
#include <iomanip>
#include <queue>
#include<vector>
#define ll long long
using namespace std;

char k[1000][1000];

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    while(n--)
    {
        int a,b,ans=0;
        cin>>a>>b;
        for(int i=0;i<a;i++)
        {
            for(int j=0;j<b;j++)
            {
                cin>>k[i][j];
            }
        }
        for(int i=-2;i<a;i++)
        {
            for(int j=-2;j<b;j++)
            {
                if((i>=0&&i<a&&j+1>=0&&j+1<b&&k[i][j+1]=='O')||(i+1>=0&&i+1<a&&j>=0&&j<b&&k[i+1][j]=='/')) ans++;
                else if((i+1>=0&&i+1<a&&j+1>=0&&j+1<b&&k[i+1][j+1]=='|')||(i+1>=0&&i+1<a&&j+2>=0&j+2<b&&k[i+1][j+2]=='\\'))ans++;
                else if((i+2>=0&&i+2<a&&j>=0&&j<b&&k[i+2][j]=='(')||(i+2>=0&&i+2<a&&j+2>=0&&j+2<b&&k[i+2][j+2]==')')) ans++;
            }
        }
        cout<<ans<<endl;
        memset(k,0,sizeof(k));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44012745/article/details/89452180