Zoj-5704题 People Counting (带注释) The 13th ZhejiangProvincial Collegiate Programming Contest – I

题目http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5704

 

Zoj-5704 People CountingThe 13th ZhejiangProvincial Collegiate Programming Contest – I

 

In a BG (dinner gathering) for ZJU ICPC team, thecoaches wanted to count the number of people present at the BG. They did thatby having the waitress take a photo for them. Everyone was in the photo and noone was completely blocked. Each person in the photo has the same posture.After some preprocessing, the photo was converted into aH×W charactermatrix, with the background represented by ".". Thus a person in thisphoto is represented by the diagram in the following three lines:

.O.

/|\

(.)

Given the character matrix, the coaches want you tocount the number of people in the photo. Note that if someone is partly blockedin the photo, only part of the above diagram will be presented in the charactermatrix.

Input

There are multiple test cases. The first line of inputcontains an integerT indicating the number of test cases. For each testcase:

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

 

/* 思路是从左上角开始,遇到一个人的部位后,把属于该人的全部部分删除*/
 
# include <stdio.h>
char map1[105][105];
int nline,m;
int main()
{
   int ncase;
   scanf("%d",&ncase);
   while(ncase--)
    {
       scanf("%d%d",&nline,&m);
       for(int i=0;i<nline;i++)
       {
       //for(int j=0;j<m;j++)
       {
       scanf("%s",map1[i]);// 一次读入一行,想一想为什么不一个一个读
       }
       }
       int solve();//函数调用声明
       int k= solve(); //模块化,看起来舒服,
                     //查错方便快捷
       printf("%d\n",k);
    }
   return 0;
}
void clear_head(int i,int j)
{
   if(map1[i][j]=='O')//确定是头
    {map1[i][j]=='.';//干掉头
       if(j-1>=0&&i+1<nline&&map1[i+1][j-1]=='/')  //左手
            map1[i+1][j-1]='.';
       if(i+1<nline&&map1[i+1][j]=='|') //躯干
            map1[i+1][j]='.';
       if(j+1<m&&i+1<nline&&map1[i+1][j+1]==92)//右手
            map1[i+1][j+1]='.';
 
       if(j-1>=0&&i+2<nline&&map1[i+2][j-1]=='(') //左脚
            map1[i+2][j-1]='.';
       if(j+1<m&&i+2<nline&&map1[i+2][j+1]==')') //右脚
            map1[i+2][j+1]='.';
    }
}
void clear_left_hand(int i,int j)
{
   if(map1[i][j]=='/')//确定是左手
   {map1[i][j]=='.';
       if(j+1<m&&i-1>=0&&map1[i-1][j+1]=='O')  //头
            map1[i-1][j+1]='.';
       if(j+1<m&&map1[i][j+1]=='|') //躯干
            map1[i][j+1]='.';
       if(j+1<m&&map1[i][j+2]==92) //右手
            map1[i][j+2]='.';
 
       if(i+1<nline&&map1[i+1][j]=='(') //左脚
            map1[i+1][j]='.';
       if(j+2<m&&i+1<nline&&map1[i+1][j+2]==')') //右脚
            map1[i+1][j+2]='.';
    }
}
void clear_right_hand(int i,int j)
{
   if(map1[i][j]==92)//确定是右手
   {map1[i][j]=='.';
       if(j-1>=0&&i-1>=0&&map1[i-1][j-1]=='O')  //头
            map1[i-1][j-1]='.';
       if(j-1>=0&&map1[i][j-1]=='|') //躯干
            map1[i][j-1]='.';
       if(j-2>=0&&map1[i][j-2]=='/')//左手
            map1[i][j-2]='.';
 
       if(j-2>=0&&i+1<nline&&map1[i+1][j-2]=='(') //左脚
            map1[i+1][j-2]='.';
       if(i+1<nline&&map1[i+1][j]==')') //右脚
            map1[i+1][j]='.';
    }
}
void clear_left_foot(int i,int j)
{
   if(map1[i][j]=='(')//确定是左脚
   {map1[i][j]=='.';
       if(j+1<m&&i-2>=0&&map1[i-2][j+1]=='O')  //头
            map1[i-2][j+1]='.';
       if(i-1>=0&&j+1<m&&map1[i-1][j+1]=='|') //躯干
            map1[i-1][j+1]='.';
       if(j+2<m&&i-1>=0&&map1[i-1][j+2]==92)//右手
            map1[i-1][j+2]='.';
 
       if(i-1>=0&&map1[i-1][j]=='/') //左手
            map1[i-1][j]='.';
       if(j+2<m&&map1[i][j+2]==')') //右脚
            map1[i][j+2]='.';
    }
}
void clear_right_foot(int i,int j)
{
   if(map1[i][j]==')')//确定是右脚
   {map1[i][j]=='.';
       if(j-1>=0&&i-2>=0&&map1[i-2][j-1]=='O')  //头
            map1[i-2][j-1]='.';
       if(j-1>=0&&i-1>=0&&map1[i-1][j-1]=='|') //躯干
            map1[i-1][j-1]='.';
       if(i-1>=0&&map1[i-1][j]==92)//右手
            map1[i-1][j]='.';
 
       if(j-2>=0&&map1[i][j-2]=='(') //左脚
            map1[i][j-2]='.';
       if(j-2>=0&&i-1>=0&&map1[i-1][j-2]=='/') //左手
            map1[i-1][j-2]='.';
    }
}
void clear_body(int i,int j)
{
   if(map1[i][j]=='|')//确定是身体
   {map1[i][j]=='.';
       if(i-1>=0&&map1[i-1][j]=='O') //头
            map1[i-1][j]='.';
       if(j-1>=0&&map1[i][j-1]=='/') //左手
            map1[i][j-1]='.';
       if(j+1<m&&map1[i][j+1]==92)//右手
            map1[i][j+1]='.';
 
       if(j-1>=0&&i+1<nline&&map1[i+1][j-1]=='(') //左脚
            map1[i+1][j-1]='.';
       if(j+1<m&&i+1<nline&&map1[i+1][j+1]==')') //右脚
            map1[i+1][j+1]='.';
    }
}
int solve()
{
   int ans=0;
   for(int i=0;i<nline;i++)
       {
           for(int j=0;j<m;j++)
           {
                switch(map1[i][j])// 函数名字绝对不要瞎起
                {//这道题如果你把函数名字乱起,并且调试遇到了问题
                  //队友看你的代码,想死的心都有。
                    case '.' : continue;
                    case 'O' :clear_head(i,j);ans++;continue;
                    case '/' :clear_left_hand(i,j);ans++;continue;
 
                    case '(' :clear_left_foot(i,j);ans++;continue;
                    case ')' :clear_right_foot(i,j);ans++;continue;
                    case '|' :clear_body(i,j);ans++;continue;
           //右手是\ ,,我直接用了它的ascii,不想使用转义字符'\\'
                    case 92  : clear_right_hand(i,j);ans++;continue;
                }
            }
       }
  return ans;
} // 题目不难,一次就好。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值