bfs样例过了全wa
查看原帖
bfs样例过了全wa
1174631
chenyanhui楼主2024/11/3 16:41
#include<bits/stdc++.h>
using namespace std;
const int N = 1145;
int n,m;
char a[N][N];
int xx[4] = { 0,0,1,-1 };
int yy[4] = { 1,-1,0,0 };
struct inf {
	int x, y;
};
void bfs() {
	queue<inf>q;
	q.push({ 1,1 });
	a[1][1] = '#';
	while (!q.empty()) {
		inf t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = t.x + xx[i];
			int ny = t.y + yy[i];
			if (nx > 0 && nx <= n && ny > 0 && ny <= m && a[nx][ny] == '.') {
				a[nx][ny] = '#';
				if (t.x == n && t.y == m) {
					return;
				}
				q.push({ nx,ny });
			}
		}

	}
}
int main() {
	cin >> n>>m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
		}
	}
	bfs();
	if (a[n][m] == '#') {
		cout << "YES";
	}
	else {
		cout << "NO";
	}
	return 0;
}
2024/11/3 16:41
加载中...