ZOJ-3944

ZOJ Problem Set - 3944
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 HW (1 ≤ HW ≤ 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

Author:  Lu, Yi

Source: The 13th Zhejiang Provincial Collegiate Programming Contest


水水的博主刚开始还以为是dfs联通块,后来发现就是很简单的乱搜,只要把每次出现的第一个器官作为起点,然后以他为中心找另外四个器官。

如果找得到所对应的器官就将他标记。统计一下一共有多少种“第一个器官”。PS:有很多种情况是可以省略的,但是我刚开始漏了几种 结果就

爆炸了,如果逻辑不是很清楚的话,推荐把所有情况的罗列出来。



#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char map[105][105];
int vis[105][105];
int m,n;
int T;
int sum = 0,flag,flagg;
void  dfs(int i,int j)
{	
	if(i<0||i>=n||j<0||j>=m)
	{
	return;
	}
	if(map[i][j] == '.')
	return;
	if(vis[i][j] ==1)
	return;
	if(map[i][j] == 'O')
	{	sum++;
			if(map[i+1][j] == '|')
		vis[i+1][j] = 1;
			if(map[i+1][j-1] == '/')
		vis[i+1][j-1] = 1;
			if(map[i+1][j+1] == '\\')
		vis[i+1][j+1] = 1;
			if(map[i+2][j-1] == '(')
		vis[i+2][j-1] = 1;
			if(map[i+2][j+1] == ')')
		vis[i+2][j+1] = 1;

	}
	if(map[i][j] == '|')
	{
		if(vis[i][j]==0)
		{
			sum++;
				if(map[i][j-1] == '//')
		vis[i][j-1] = 1;
			if(map[i][j+1] == '\\')
		vis[i][j+1] = 1;
			if(map[i+1][j-1] == '(')
		vis[i+1][j-1] = 1;
			if(map[i+1][j+1] == ')')
		vis[i+1][j+1] = 1;
		}
	}
	if(map[i][j] == '/')
	{
	
		if(vis[i][j] == 0)
		{
			sum++;
			vis[i][j] = 1;
				if(map[i][j+1] == '|')
		vis[i][j+1] = 1;
			if(map[i][j+2] == '\\')
		vis[i][j+2] = 1;
			if(map[i+1][j] == '(')
		vis[i+1][j] = 1;
			if(map[i+1][j+2] == ')')
		vis[i+1][j+2] = 1;	
		}
				
	}
		if(map[i][j] == '\\')
	{
	
		if(vis[i][j] == 0)
		{
			sum++;
			vis[i][j] = 1;
			if(map[i][j-1] == '|')
		vis[i][j-1] = 1;
			if(map[i][j-2] == '/')
		vis[i][j-2] = 1;
				if(map[i+1][j] == ')')
			vis[i+1][j] = 1;
				if(map[i+1][j-2] == '(')
			vis[i+1][j-2] = 1;
		}
		
	}
	if( map[i][j] == '(')
	{
		if(vis[i][j] == 0 )
		{
			sum++;
		if(map[i][j+2] == ')')
		vis[i][j+2] = 1;
		}
	}
 	if( map[i][j] == ')')
	{
	
		if(vis[i][j] == 0 )
		{
		sum++;
		if(map[i][j-2] == '(')
		vis[i][j-2] = 1;
		}
	}
//	flag=flagg=0;

}		
	
int main()
{
	//freopen("in.txt","r",stdin);
	scanf("%d",&T);
	while(T--)
	{	
		scanf("%d%d",&n,&m);
		sum = 0;
		memset(map,0,sizeof(map));
		memset(vis,0,sizeof(vis));
		for(int i = 0; i < n;i++)
		scanf("%s",map[i]);
		for(int x = 0;x < n;x++)
	{
			for(int y = 0;y < m;y++)
		{
		//printf("%c\n",map[i][j]); 
		//	printf("%d%d\n",i,j);
			dfs(x,y);
		//	printf("!!!!aaaaa\n");
		}
	}
		//count<<map[1][1]<<endl;
		cout<<sum<<endl;
	}	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值