社区讨论

求调程序(帮助必关)

灌水区参与者 2已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@mhk7gw1h
此快照首次捕获于
2025/11/04 14:46
4 个月前
此快照最后确认于
2025/11/04 14:46
4 个月前
查看原帖
题目链接this
我的代码
CPP
#include<bits/stdc++.h>
using namespace std;
struct ab {
	int x, y;
};
int n, m, d[4][2] = { {-1,0} ,{0,1},{1,0},{0,-1} }, k,ls,mdx,dx;
bool a[501][501][4];
bool book[501][501];
void nb(int x, int y) {
	queue<ab> q;
	q.push({ x,y });
	while (!q.empty()) {
		ab ls = q.front();
		q.pop();
		if (ls.x<1 || ls.x>n || ls.y<1 || ls.y>m || book[ls.x][ls.y] == 1) {
			continue;
		}
		book[ls.x][ls.y] = 1;
		for (int i = 0; i < 4; i++) {
			if (!a[ls.x][ls.y][i]) {
				q.push({ ls.x + d[i][0],ls.y + d[i][1] });
			}
		}
		dx++;
	}
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> ls;
			int lls = 8;
			for (int as = 0; as <4; as++) {
				if (ls >= lls) {
					ls -= lls;
					a[i][j][as] = 1;
				}
				lls /= 2;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (book[i][j]==0) {
				dx = 0;
				k++;
				nb(i, j);
				mdx = max(mdx, dx);
			}
		}
	}
	cout << k << endl;
	cout<< mdx;
	return 0;
}

回复

5 条回复,欢迎继续交流。

正在加载回复...