ZOJ People Counting

第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题。

ZOJ  3944 http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3944

 

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

/
刚读完题的我是无比懵逼的。。。
冷静下来,发现有几点对解题还是有用的。
1 每个人都至少有一部分漏出来。
2 大家的站姿相同,也就是说,每个人躯体的各个部分的相对位置是确定的。如果你找到了一只手,那么在确定的地方可能会出现这个人的另一只手。。。

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <stdlib.h>
# define MAXN 110
typedef long long int LL;
using namespace std;


char in[MAXN][MAXN];
int t, p, w;
int head[5][2] = {{1,-1}, {1,0}, {1,1}, {2,-1}, {2,1}};
char headwing[6] = {"/|\\()"};
int leftarm[5][2] = {{-1,1}, {0,1}, {0,2}, {1,0}, {1,2}};
char leftarmwing[6] = {"O|\\()"};
int mid[5][2] = {{-1,0}, {0,-1}, {0,1}, {1,-1}, {1,1}};
char midwing[6] = {"O/\\()"};
int rightarm[5][2] = {{-1,-1}, {0,-2}, {0,-1}, {1,-2}, {1,0}};
char rightarmwing[6] = {"O/|()"};
int leftleg[5][2] = {{-2,1}, {-1,0}, {-1,1}, {-1,2}, {0,2}};
char leftlegwing[6] = {"O/|\\)"};
int rightleg[5][2] = {{-2,-1}, {-1,-2}, {-1,-1}, {-1,0}, {0,-2}};
char rightlegwing[6] = {"O/|\\("};


void check(int i, int j){
    
     int k = 0, x, y;
     if(in[i][j] == 'O'){
         
         for(k=0; k<5; ++k){
                x = i + head[k][0];
            y = j + head[k][1];    
            
    
             if(x < 0 || x>=p || y<0 || y>=w){
                 continue;
            }else{
                if(in[x][y] == headwing[k]){
                    in[x][y] = '.';
                }
            }
        }
         
     }else if(in[i][j] == '/'){
         
         for(k=0; k<5; ++k){
                x = i + leftarm[k][0];
            y = j + leftarm[k][1];    
             if(x < 0 || x>=p || y<0 || y>=w){
                 continue;
            }else{
                if(in[x][y] == leftarmwing[k]){
                    in[x][y] = '.';
                }
            }
        }
         
     }else if(in[i][j] == '|'){
         
         
         for(k=0; k<5; ++k){
                x = i + mid[k][0];
            y = j + mid[k][1];    
             if(x < 0 || x>=p || y<0 || y>=w){
                 continue;
            }else{
                if(in[x][y] == midwing[k]){
                    in[x][y] = '.';
                }
            }
        }
         
     }else if(in[i][j] == '\\'){
         
         for(k=0; k<5; ++k){
                x = i + rightarm[k][0];
            y = j + rightarm[k][1];    
             if(x < 0 || x>=p || y<0 || y>=w){
                 continue;
            }else{
                if(in[x][y] == rightarmwing[k]){
                    in[x][y] = '.';
                }
            }
        }
         
         
     }else if(in[i][j] == '('){
         
         for(k=0; k<5; ++k){
                x = i + leftleg[k][0];
            y = j + leftleg[k][1];    
             if(x < 0 || x>=p || y<0 || y>=w){
                 continue;
            }else{
                if(in[x][y] == leftlegwing[k]){
                    in[x][y] = '.';
                }
            }
        }
         
     }else if(in[i][j] == ')'){
         
         for(k=0; k<5; ++k){
                x = i + rightleg[k][0];
            y = j + rightleg[k][1];    
             if(x < 0 || x>=p || y<0 || y>=w){
                 continue;
            }else{
                if(in[x][y] == rightlegwing[k]){
                    in[x][y] = '.';
                }
            }
        }
         
     }    
    
    
    return ;
}
int main(){
    
    //freopen("in.txt", "r", stdin);
    scanf("%d", &t);
    
    while( t-- ){
        scanf("%d%d", &p, &w);
        getchar();
        
        memset(in, 0, sizeof(in));    
        for(int i=0; i<p; ++i){
            for(int j=0; j<w; ++j){
                scanf("%c", &in[i][j]);
            }
            getchar();
        }
    
        
        int ans = 0;
        for(int i=0; i<p; ++i){
            for(int j=0; j<w; ++j){
                
                if(in[i][j] == '.'){
                    continue;
                }else{
                
                    ans ++;
                    check(i,j);
                    in[i][j] = '.';
                }
                
            }
        }
        
        cout << ans << endl;
    }

    return 0;
}

// 代码写的这么长,真的很抱歉 :(。



转载于:https://www.cnblogs.com/zzusunjs/p/5539859.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值