BFS 死循环 求调!!!
查看原帖
BFS 死循环 求调!!!
960206
topcsa楼主2024/8/10 20:21
#include <bits/stdc++.h>
using namespace std;
int x, y;
string s[600];
int a[550][550];
int state[550][550];
int tap[550][550];
int ans = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
queue<int> qx;
queue<int> qy;
void bfs(int x0, int y0) {
	qx.push(x0);
	qy.push(y0);
	while (!qx.empty()) {
		int nox = qx.front();
		int noy = qy.front();
		for (int i = 0; i < 4; i++) {
			int nx = nox + dx[i];
			int ny = noy + dy[i];
			if (nx >= 0 && nx <= x && ny >= 0 && ny <= y && state[nx][ny] != 1) {
				qx.push(nx);
				qy.push(ny);
				tap[nx][ny] = 1;
			}
		}
		qx.pop();
		qy.pop();
	}
	return ;
}
int main()
{
	cin >> x >> y;
	for (int i = 0; i < x; i++) {
		cin >> s[i];
	}
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			a[i][j] = s[i - 1][j - 1] == '*' ? 1 : 0;
			state[i][j] = a[i][j];
			if (a[i][j] == 1) {
				tap[i][j] = 0x3f3f3f3f;
			}
		}
	}
	bfs(0, 0);
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			if (tap[i][j] != 0x3f3f3f3f) {
				if (tap[i][j] != 0) continue;
				ans++;
			}
		}
	}
	cout << ans << '\n';
	return 0;
}
2024/8/10 20:21
加载中...