ZOJ Problem Set - 3944(interesting people counting))

来源:互联网 发布:mysql开启binlog日志 编辑:程序博客网 时间:2024/06/01 10:04

【链接】:click here~~
【题意】:
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

【题意】people counting,统计照片中的人数个数。
【思路】统计将可能构成人体的每一部分的周围的其他器官,赋值为一个点,然后暴力即可,注意代码不要出错,之前手残将s[i][j]=’.’写成s[i][j]=’\’WA无数遍!!!
【代码】:

/*ZOJ Problem Set - 3944*/#include <iostream>#include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn =1e2+10;inline LL read(){    int  c=0,f=1;    char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}    return c*f;}char s[maxn][maxn];int n,m;int check(int x,int y){    if(x>=0&&x<n && y>=0&&y<m) return true;    return false;}int ok1(int sx,int sy) ///'O' 头下面五个可能的部位{    if(s[sx+1][sy-1]=='/' &&check(sx+1,sy-1)) s[sx+1][sy-1]='.';    if(s[sx+1][sy]=='|' &&check(sx+1,sy)) s[sx+1][sy]='.';    if(s[sx+1][sy+1]=='\\' &&check(sx+1,sy+1)) s[sx+1][sy+1]='.';    if(s[sx+2][sy-1]=='(' &&check(sx+2,sy-1)) s[sx+2][sy-1]='.';    if(s[sx+2][sy+1]==')' &&check(sx+2,sy+1)) s[sx+2][sy+1]='.';}int ok2(int sx,int sy)/// '|'{    if(s[sx][sy-1]=='/' &&check(sx,sy-1)) s[sx][sy-1]='.';    if(s[sx][sy+1]=='\\' &&check(sx,sy+1)) s[sx][sy+1]='.';    if(s[sx+1][sy-1]=='(' &&check(sx+1,sy-1)) s[sx+1][sy-1]='.';    if(s[sx+1][sy+1]==')' &&check(sx+1,sy+1)) s[sx+1][sy+1]='.';}int ok3(int sx,int sy)/// '/'{    if(s[sx][sy+1]=='|'&& check(sx,sy+1)) s[sx][sy+1]='.';    if(s[sx][sy+2]=='\\' &&check(sx,sy+2)) s[sx][sy+2]='.';    if(s[sx+1][sy]=='(' &&check(sx+1,sy)) s[sx+1][sy]='.';    if(s[sx+1][sy+2]==')' &&check(sx+1,sy+2)) s[sx+1][sy+2]='.';}int ok4(int sx,int sy) ///'\\'{    if(s[sx+1][sy-2]=='(' &&check(sx+1,sy-2)) s[sx+1][sy-2]='.';    if(s[sx+1][sy]==')' &&check(sx+1,sy)) s[sx+1][sy]='.';}int ok5(int sx,int sy)///'('{    if(s[sx][sy+2]==')' &&check(sx,sy+2)) s[sx][sy+2]='.';}int main(){     //freopen("1.txt","r",stdin);    int t;t=read();    while(t--)    {       n=read();m=read();        for(int i=0; i<n; ++i)        {            for(int j=0; j<m; ++j)            {                cin>>s[i][j];            }        }        int sx,sy,dx,dy;        int cnt=0;        for(int i=0; i<n; ++i)        {            for(int j=0; j<m; ++j)            {                if(s[i][j]=='O')                {                    cnt++;                    ok1(i,j);                }                else if(s[i][j]=='|')                {                    cnt++;                    ok2(i,j);                }                else if(s[i][j]=='/')                {                    cnt++;                    ok3(i,j);                }                else if(s[i][j]=='\\')                {                    cnt++;                    ok4(i,j);                }                else if(s[i][j]=='(')                {                    cnt++;                    ok5(i,j);                }                else if(s[i][j]==')')cnt++;            }        }        printf("%d\n",cnt);    }    return 0;}
0 0