Files
python/TangDou/AcWing/Dfs/1112_bfs_bool.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

63 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
struct Node {
int x;
int y;
int step;
};
const int N = 110;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
char g[N][N];
bool st[N][N];
int n;
int xa, ya, xb, yb; // 不能使用x1,y1这样的变量名,可能是万能头中有重复声明
bool flag;
void bfs() {
flag = false;
if (g[xa][ya] == '#') return;
if (g[xb][yb] == '#') return;
memset(st, 0, sizeof st);
if (xa == xb && ya == yb) {
flag = true;
return;
}
queue<Node> q;
q.push({xa, ya, 0});
st[xa][ya] = true;
while (q.size()) {
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (st[x][y]) continue;
if (g[x][y] == '#') continue;
if (x == xb && y == yb) {
flag = true;
return;
}
q.push({x, y, t.step + 1});
st[x][y] = true;
}
}
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n;
for (int i = 0; i < n; i++) cin >> g[i];
cin >> xa >> ya >> xb >> yb;
bfs();
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}