社区讨论

测样例怎么是9啊,求调

P1451求细胞数量参与者 2已保存回复 2

讨论操作

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

当前回复
2 条
当前快照
1 份
快照标识符
@m49yipfh
此快照首次捕获于
2024/12/04 22:01
去年
此快照最后确认于
2024/12/05 14:56
去年
查看原帖
code:
CPP
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define max(l, r) ((l) < (r) ? (r) : (l))
#define min(l, r) ((l) > (r) ? (r) : (l))
short walk[4][2]{ {0,1},{1,0},{0,-1},{-1,0} };
struct pbb { bool val = false; short x = 0, y = 0; };
static void solve() {
	char ch;
	short n, m, ans = 0;
	cin >> n >> m;
	vector<vector<pbb>>v(n, vector<pbb>(m));
	for (short i = 0; i < n; i++) for (short j = 0; j < m; j++) cin >> ch, v[i][j].val = ch == '0' ? false : true;
	for (short i = 0; i < n; i++) {
		for (short j = 0; j < m; j++) {
			if (v[i][j].val) {
				pbb head = v[i][j];
				queue<pbb>q;
				q.push(head);
				while (!q.empty()) {
					pbb x = q.front();
					q.pop();
					for (int i = 0; i < 4; i++) {
						pbb y;
						y.x = x.x + walk[i][0], y.y = x.y + walk[i][1];
						if (y.x == -1 || y.x == n || y.y == -1 || y.y == m || !v[y.x][y.y].val) continue;
						v[y.x][y.y].val = '0';
						q.push(y);
					}
				}
				ans++;
			}
		}
	}
	cout << ans << '\n';
	return;
}
int main() {
	ios::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	solve();
	return 0;
}

回复

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

正在加载回复...