poj1164The Castle(搜索)

The Castle
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6944 Accepted: 3892

Description

     1   2   3   4   5   6   7  

   #############################

 1 #   |   #   |   #   |   |   #

   #####---#####---#---#####---#

 2 #   #   |   #   #   #   #   #

   #---#####---#####---#####---#

 3 #   |   |   #   #   #   #   #

   #---#########---#####---#---#

 4 #   #   |   |   |   |   #   #

   #############################

(Figure 1)



#  = Wall   

|  = No wall

-  = No wall

Figure 1 shows the map of a castle.Write a program that calculates 
1. how many rooms the castle has 
2. how big the largest room is 
The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls. 

Input

Your program is to read from standard input. The first line contains the number of modules in the north-south direction and the number of modules in the east-west direction. In the following lines each module is described by a number (0 <= p <= 15). This number is the sum of: 1 (= wall to the west), 2 (= wall to the north), 4 (= wall to the east), 8 (= wall to the south). Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1. The castle always has at least two rooms.

Output

Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).

Sample Input

4
7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13

Sample Output

5
9
题目大意:
n*m个方格组成的城堡,其中方格的每个墙都有一个值,西墙-1,东墙-4,北墙-2,南墙-8;
输入N*M个数字(为每个方格所包含墙的值之和),分别表示每个方格的墙的的情况。
问这个城堡共有多少个房间,其中最大的房间所包含的方格数是多少?
解题思路:
dfs搜索,不难发现,当方格没有南墙时,其对应的值一定小于8,这样就可以向下继续搜索了,当有南墙时,其值大于8
先让其值对8取余再赋给它(这样就可以避免南墙的干扰),然后判断是否有东墙,如何判断呢?仍然是求余有东墙,看求余的值与4比较,如果比4大,说明有东墙,否则没有,可以发现;如果对4求余,并不能保证可以判断出是否有东墙,例如,9%4=1<4,12%4=0<4;当两个例子一个有东墙,一个没有。所以不能对4求余。对8呢?可以区分,9%8=1<4;12%8=4不小于4;
然后在对4取余在赋给它自己;依次类推,判断是否有北墙对4在求余看是否小于2,当大于2时,表示有北墙,在对2取余,然后对2取余判断是否小于1.
AC代码:
#include<stdio.h>
#include<string.h>
int map[60][60],vis[60][60];
int n,m,count;
void dfs(int x,int y)
{
	if(vis[x][y]) return;   //如果已经访问过,回到上一个位置
	else
	{
		count++;      //否则,count加一统计该房间包含的方格数;并对该方格标记为已访问过。
		vis[x][y]=1;
	}
	if(map[x][y]<8)      //判断是否有南墙,没有,则向下继续搜索
	 dfs(x+1,y);
	else    
	 map[x][y]%=8;      //有南墙,对8求余;向下判断。
	if(map[x][y]%8<4)   //判断是否有东墙,没有,则向右继续搜索。
	 dfs(x,y+1);
	else
	 map[x][y]%=4;     //有东墙,对4求余,继续向下判断。
	if(map[x][y]%4<2)  //判断是否有北墙,没有,则向上继续搜索
	 dfs(x-1,y);
	else
	 map[x][y]%=2;     //有北墙,对2求余,继续向下判断
	if(map[x][y]%2==0) //判读是否有西墙,没有,向左继续搜索
	 dfs(x,y-1);
}
int main()
{
	int maxroom,cnt,i,j;
	while(scanf("%d%d",&n,&m)==2)
	{
		memset(map,0,sizeof(map));
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				scanf("%d",&map[i][j]);
			}
		}
		maxroom=cnt=0;
		for(i=0;i<n;i++)
		{
	        for(j=0;j<m;j++)
			{
				count=0;
				dfs(i,j);
				if(count!=0)
				 cnt++;
				if(maxroom<count) maxroom=count;				
			}		
		}
	printf("%d\n%d\n",cnt,maxroom);
	}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bokzmm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值