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

来源:互联网 发布:竖笛指法软件 编辑:程序博客网 时间:2024/06/11 20:52

题目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;} // 因为题目不难,一次就ac了


/*吐槽一下机房的破电脑,鼠标单击键是坏的,根本想复制粘贴样例进行测试,就是拖不上,还好懂得 shit+方向  拖黑文本,鼠标键盘对体验影响特别大,建议广大机房管理人员,电脑5年不更换,优化一下还可以用,鼠标单击坏了,i7都恼火,及时把坏鼠标给换下来。*/

 

0 0
原创粉丝点击