USACO2.1.1 The Castle (castle)

先给房间染色,第一问和第二问就解决了。

然后枚举每一面相邻的房间是不同颜色的墙,再统计最大值。

/*
ID:shijiey1
PROG:castle
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n, m;
int arr[55][55];
int color[55][55];
int cnt[3000];
int curColor = 0;

void dfs(int x, int y) {
	if (x > 1 && !(arr[x][y] & 2) && !color[x - 1][y]) {
		color[x - 1][y] = curColor;
		cnt[curColor]++;
		dfs(x - 1, y);
	}
	if (x < n && !(arr[x][y] & 8) && !color[x + 1][y]) {
		color[x + 1][y] = curColor;
		cnt[curColor]++;
		dfs(x + 1, y);
	}
	if (y > 1 && !(arr[x][y] & 1) && !color[x][y - 1]) {
		color[x][y - 1] = curColor;
		cnt[curColor]++;
		dfs(x, y - 1);
	}
	if (y < m && !(arr[x][y] & 4) && !color[x][y + 1]) {
		color[x][y + 1] = curColor;
		cnt[curColor]++;
		dfs(x, y + 1);
	}
}

int main() {
	freopen("castle.in", "r", stdin);
	freopen("castle.out", "w", stdout);
	scanf("%d %d", &m, &n);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf("%d", &arr[i][j]);
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (!color[i][j]) {
				curColor++;
				color[i][j] = curColor;
				cnt[curColor]++;
				dfs(i, j);
			}
		}
	}
	int x, y;
	int ans = 0;
	char c = 'E';
	
	for (int j = 1; j <= m; j++) {
		for (int i = n; i >= 1; i--) {
			if (i > 1 && (arr[i][j] & 2) && color[i][j] != color[i - 1][j]) {
				if (cnt[color[i][j]] + cnt[color[i - 1][j]] > ans) {
					ans = cnt[color[i][j]] + cnt[color[i - 1][j]];
					x = i;
					y = j;
					c = 'N';
				}
			}
			if (j < m && (arr[i][j] & 4) && color[i][j] != color[i][j + 1]) {
				if (cnt[color[i][j]] + cnt[color[i][j + 1]] > ans) {
					ans = cnt[color[i][j]] + cnt[color[i][j + 1]];
					x = i;
					y = j;
					c = 'E';
				}
			}
		}
	}
	sort(cnt + 1, cnt + 1 + curColor);
	printf("%d\n%d\n", curColor, cnt[curColor]);
	printf("%d\n%d %d %c\n", ans, x, y, c);

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值