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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章