USACO 2.1 The Castle

51 篇文章 0 订阅
5 篇文章 0 订阅

BFS用洪水填充染色。 记录每个颜色的房间数量


 然后按照从下往上,从左往右的顺序穷举一边,拆掉墙,如果墙两边的房间颜色相同,跳过。 颜色不同,把颜色所代表的房间数量相加,和答案对比,更新答案即可。


Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.008 secs, 3572 KB]
   Test 2: TEST OK [0.008 secs, 3572 KB]
   Test 3: TEST OK [0.005 secs, 3572 KB]
   Test 4: TEST OK [0.008 secs, 3572 KB]
   Test 5: TEST OK [0.008 secs, 3572 KB]
   Test 6: TEST OK [0.008 secs, 3704 KB]
   Test 7: TEST OK [0.008 secs, 3572 KB]
   Test 8: TEST OK [0.022 secs, 3704 KB]

All tests OK.

/*
TASK:castle
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
 
int wall[55][55]={0};
vector<int>path[2510];
int m, n, t=0;
int my_map[55][55]={0}, colo[2510] = {0}, colo_kinds=0;	
int room_size[2510];
const int dx[]={0,0,-1,0,1};
const int dy[]={0,-1,0,1,0};
const int D[] = {0,1,2,4,8};
 
void init()
{
	for (int i = 1; i <= m; ++ i)
		for (int j = n; j != 0; -- j)	my_map[j][i] = ++t;
	for (int i = 1; i <= n; ++ i)
		for (int j = 1; j <= m; ++ j)
		{
			cin >> wall[i][j];
			for (int k = 1; k <= 4; ++ k)
				if (!(wall[i][j] & D[k]))	path[my_map[i][j]].push_back(my_map[i + dx[k]][j + dy[k]]);	
		}
}
 
queue<int>q;
void doit()
{
	int max_room = 0;
	for (int i = 1; i <= m; ++ i)
		for (int j = n; j != 0; -- j)
		{
			int tmp = my_map[j][i];
			if (colo[tmp])	continue;
			q.push(tmp);
			colo[tmp] = ++ colo_kinds;
			room_size[colo_kinds] = 1;
			while (!q.empty())
			{
				int now = q.front();
				q.pop();	
				for (int i = 0; i != path[now].size(); ++ i)
				{
					int will = path[now][i];
					if (!will || colo[will])	continue;	
					++ room_size[colo_kinds];
					colo[will] = colo_kinds;
					q.push(will);
				}
				max_room = max(max_room, room_size[colo_kinds]);
			}
		}
	cout<<colo_kinds<<endl;
	cout<<max_room<<endl;
	max_room = 0;
	int dir(0);
	int tmpx, tmpy;
 
	for (int i = 1; i <= m; ++ i)
		for (int j = n; j != 0; -- j)
		{
			for (int k = 1; k <= 4; ++ k)
			{
				int now = colo[my_map[j][i]];
				int will = colo[my_map[j + dx[k]][i + dy[k]]];
				if (now == will)	continue;
				int tmp = room_size[now] + room_size[will];
				if (tmp > max_room)
				{
					tmpx = j;
					tmpy = i;
					max_room = tmp;
					dir = k;	
				}	
			}
		}
	cout<<max_room<<endl;
	cout<<tmpx<<" "<<tmpy<<" ";
	if (dir == 2)	cout<<'N'<<endl;
	else cout<<'E'<<endl;
}
 
int main()
{
	freopen("castle.in", "r", stdin);
	freopen("castle.out", "w", stdout);
	std::ios::sync_with_stdio(false);
	cin >>m >> n;
	init();
	doit();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值