ZOJ 3944-People Counting【模拟】(2016浙江省大学生程序设计竞赛)

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 aH×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

解题思路:

找这个照片里有多少个人,我的思路就是在能在照片中显示肢体的位置(除了这幅图内),都打上头,然后暴力这个字符矩阵,如果在图片内有这个人的一段肢体,或几个肢体,我们就记录这是一个人。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char map[200][200];
int vis[200][200];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(map,'O',sizeof(map));
		memset(vis,0,sizeof(vis));
		int n,m;
		scanf("%d%d",&n,&m);
		int i,j;
		for(i=2;i<n+2;i++)
		{
			getchar();
			for(j=1;j<m+1;j++)
			{
				scanf("%c",&map[i][j]);
			}
		}
		int ans=0;
		for(i=0;i<n+2;i++)
		{
			for(j=0;j<m+2;j++)
			{
				bool cnmz=0;
				vis[i][j]=1;
				if(j==0)
				{
						if(map[i+1][j+1]=='\\')
						{
							cnmz=1;
							vis[i+1][j+1]=1;
						}	
						if(map[i+1][j]=='|')
						{
							cnmz=1;
							vis[i+1][j]=1;
						}
						if(map[i+2][j+1]==')')
						{
							cnmz=1;
							vis[i+2][j+1]=1;
						}
					}
					else if(j==m+1)
					{
						if(map[i+1][j-1]=='/')
						{
							cnmz=1;
							vis[i+1][j-1]=1;
						}	
						if(map[i+1][j]=='|')
						{
							cnmz=1;
							vis[i+1][j]=1;
						}
						if(map[i+2][j-1]=='(')
						{
							cnmz=1;
							vis[i+2][j-1]=1;
						}
					}
					else
					{
						if(map[i+1][j+1]=='\\')
						{
							cnmz=1;
							vis[i+1][j+1]=1;
						}	
						if(map[i+2][j+1]==')')
						{
							cnmz=1;
							vis[i+2][j+1]=1;
						}
						if(map[i+1][j-1]=='/')
						{
							cnmz=1;
							vis[i+1][j-1]=1;
						}	
						if(map[i+1][j]=='|')
						{
							cnmz=1;
							vis[i+1][j]=1;
						}
						if(map[i+2][j-1]=='(')
						{
							cnmz=1;
							vis[i+2][j-1]=1;
						}
					}
				if(cnmz==1)
				{
					ans++;
				}
				if(i>=2&&i<n+2&&j>=1&&j<m+1&&cnmz==0&&map[i][j]=='O')
				{
					ans++;
				}
				
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(模拟)