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


Author: Lu, Yi

Source: The 13th Zhejiang Provincial Collegiate Programming Contest

统计小人个数;  泪流满面!!

#include <cstdio>
#include <cstring>
#define N 110
char G[N][N]; int v[N][N];
int n , m;
using namespace std ;
void check()
{
        for(int i=0; i< n; i++)
                for( int j=0; j<m; j++)
                {
                    printf("%c", G[i][j]);
                    if(j== m-1) printf("\n");
                }
}
bool judge(int a, int b)
{
    if(a>= 0&& a< n && b>= 0 && b < m)
        return true;
    else
        return false;
}
int main()
{
    int t; scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        for(int i=0; i <n; i++)
        {
            scanf ("%s", G[i]);
        }
        int sum= 0;
        for(int i=0; i< n; i++)
            for(int j=0; j< m; j++)
            {

                if(G[i][j]==‘.‘) continue;
                if(G[i][j]==‘O‘)
                {
                    sum += 1;
                    G[i][j]= ‘.‘;
                    if(judge( i+1, j) && G[i+1][j]==‘|‘) G[i+1][j]=‘.‘;
                    if(judge( i+1, j-1) &&G[i+1][j-1]==‘/‘) G[i+1][j-1]= ‘.‘;
                    if(judge( i+1, j+1) &&G[i+1][j+1]==‘\\‘) G[i+1][j+1]= ‘.‘;
                    if(judge( i+2, j-1) &&G[i+2][j-1]== ‘(‘) G[i+2][j-1]= ‘.‘;
                    if(judge( i+2, j+1) &&G[i+2][j+1]== ‘)‘) G[i+2][j+1]= ‘.‘;
                }

                if(G[i][j]== ‘/‘)
                {
                    sum += 1;
                    G[i][j]= ‘.‘;
                    if(judge( i, j+1) &&G[i][j+1]== ‘|‘) G[i][j+1]= ‘.‘;
                    if(judge( i, j+2) &&G[i][j+2]== ‘\\‘) G[i][j+2]= ‘.‘;
                    if(judge( i+1, j) &&G[i+1][j]==‘(‘) G[i+1][j]= ‘.‘;
                    if(judge( i+1, j+2) &&G[i+1][j+2]== ‘)‘) G[i+1][j+2]= ‘.‘;
                }

                if(G[i][j]== ‘|‘)
                {
                    sum += 1;
                    G[i][j]= ‘.‘;
                    if(judge( i, j+1) &&G[i][j+1]== ‘\\‘) G[i][j+1] =‘.‘;
                    if(judge( i+1, j-1) &&G[i+1][j-1]== ‘(‘) G[i+1][j-1]= ‘.‘;
                    if(judge( i+1, j+1) &&G[i+1][j+1]== ‘)‘) G[i+1][j+1]= ‘.‘;
                }

                if(G[i][j]==‘\\‘)
                {
                    sum += 1;
                    G[i][j]= ‘.‘;
                    if(judge( i+1, j) &&G[i+1][j]== ‘)‘) G[i+1][j]= ‘.‘;
                    if(judge( i+1, j-2) &&G[i+1][j-2]==‘(‘) G[i+1][j-2]= ‘.‘;
                }
                if(G[i][j]== ‘(‘)
                {
                    sum += 1;
                    G[i][j]= ‘.‘;
                    if(judge( i,j+2) &&G[i][j+2]== ‘)‘) G[i][j+2]= ‘.‘;
                }
                if(G[i][j]== ‘)‘)
                {
                    sum += 1;
                    G[i][j]=‘.‘;
                }
            }
        printf("%d\n", sum);
    }

    return 0;
}
时间: 2024-10-27 02:07:18

Zoj3944 - People Counting的相关文章

ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)

ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|(.) 占3*3格子,句号“.”为背景.没有两个人完全重合.有的人被挡住了一部分.问照片上有几个人. 题解: 先弄个常量把3*3人形存起来,然后6个部位依次找,比如现在找头,找到一个头,就把这个人删掉(找这个人的各个部位,如果在该部位位置的不是这个人的身体,就不删),删成句号,疯狂找就行了. 代码: 1

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

Counting Divisors HDU - 6069

Counting Divisors HDU - 6069 题意:给定区间[a,b]和k,求xk有多少因子(x属于[a,b]),求和. 题解:http://blog.csdn.net/zlh_hhhh/article/details/76680641 a.b最大可达到1e12,但是b-a<1e6. 一开始愚蠢的一个一个分解然后去求有多少因子然后求和,范围那么大裸裸的超时啊! 可以枚举素数,对每一个素数,把区间内所有可以分解的进行分解. 最后再求和. 1 #include <bits/stdc++

LightOJ - 1148 Mad Counting(坑)

Mad Counting Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive a

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

Project Euler 85 :Counting rectangles 数长方形

Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles: Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the

HDU3664 Permutation Counting

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 754 Problem Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-val