POJ 1164 The Castle

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.

  入门的DFS,预处理有个技巧,不用一个个情况列举。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

struct Node{
	bool vis;
	int E, W, N, S;
	int num;
};

int H, W, cnt, ans = 0, numb = 0;
Node a[53][53];

void Predo()
{
	for(int i = 1; i <= H; i++)
		for(int j = 1; j <= W; j++) {
			if(a[i][j].num >= 8)	a[i][j].num -= 8,	a[i][j].S ++;
			if(a[i][j].num >= 4)	a[i][j].num -= 4,	a[i][j].E ++;
			if(a[i][j].num >= 2)	a[i][j].num -= 2,	a[i][j].N ++;
			if(a[i][j].num >= 1)	a[i][j].num -= 1,	a[i][j].W ++;
			a[i][j].vis = false;
		}		
}

int DFS(int x, int y)
{
	cnt ++;
	a[x][y].vis = true;
	if(!a[x][y].N && !a[x-1][y].vis)	DFS(x-1, y);
	if(!a[x][y].S && !a[x+1][y].vis)	DFS(x+1, y);
	if(!a[x][y].W && !a[x][y-1].vis)	DFS(x, y-1);
	if(!a[x][y].E && !a[x][y+1].vis)	DFS(x, y+1);
	return 0;
}

int main()
{
	scanf("%d%d", &H, &W);
	for(int i = 1; i <= H; i++)
		for(int j = 1; j <= W; j++)
			scanf("%d", &a[i][j].num);
	
	Predo();
	
	for(int i = 1; i <= H; i++)
		for(int j = 1; j <= W; j++)
			if(!a[i][j].vis)
			{
				cnt = 0;
				DFS(i, j);
				ans = max(ans, cnt);
				numb ++;
			}
	
	printf("%d\n%d", numb, ans);
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值