openjudge2815 城堡问题

描述

     1   2   3   4   5   6   7  
   #############################
 1 #   |   #   |   #   |   |   #
   #####---#####---#---#####---#
 2 #   #   |   #   #   #   #   #
   #---#####---#####---#####---#
 3 #   |   |   #   #   #   #   #
   #---#########---#####---#---#
 4 #   #   |   |   |   |   #   #
   #############################
           (图 1)

   #  = Wall   
   |  = No wall
   -  = No wall

图1是一个城堡的地形图。请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。城堡被分割成mn(m≤50,n≤50)个方块,每个方块可以有0~4面墙。
输入
程序从标准输入设备读入数据。第一行是两个整数,分别是南北向、东西向的方块数。在接下来的输入行里,每个方块用一个数字(0≤p≤50)描述。用一个数字表示方块周围的墙,1表示西墙,2表示北墙,4表示东墙,8表示南墙。每个方块用代表其周围墙的数字之和表示。城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。输入的数据保证城堡至少有两个房间。
输出
城堡的房间数、城堡中最大房间所包括的方块数。结果显示在标准输出设备上。
样例输入
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 
样例输出
5
9


#include<iostream>
#include<string.h>
using namespace std;

int r,c;
int color[60][60];
int room[60][60];
int RoomNum=0,MaxRoomArea=0,RoomArea;

void Dfs(int i,int j)
{
	if(color[i][j]!=0)//已被访问过 
		return;
	++RoomArea;
	color[i][j]=RoomNum;
	if((room[i][j]&1)==0) Dfs(i,j-1);//这里用到了与运算,把room[i][j]换成二进制和0001进行与运算,从而判断西边是否有墙
	if((room[i][j]&2)==0) Dfs(i-1,j);
	if((room[i][j]&4)==0) Dfs(i,j+1);
	if((room[i][j]&8)==0) Dfs(i+1,j);
}
int main()
{
	while(cin>>r>>c)
	{
		for(int i=1;i<=r;i++)
			for(int j=1;j<=c;j++)
				cin>>room[i][j];
		memset(color,0,sizeof(color));
		for(int i=1;i<=r;i++)
		{
			for(int j=1;j<=c;j++)
			{
				if(color[i][j]==0)//未被访问
				{
					++RoomNum; 
					RoomArea=0;
					Dfs(i,j);
					MaxRoomArea=max(RoomArea,MaxRoomArea);
				} 
			}
		}
		cout<<RoomNum<<endl;
		cout<<MaxRoomArea<<endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值