61 lines
1.5 KiB
C++
61 lines
1.5 KiB
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
const int INF = 0x3f3f3f3f;
|
||
// dfs只能求出来是否连通,第一次搜索到时并不能保证是最短距离
|
||
// bfs也可以做,可以保证第一次到达时是最短距离
|
||
// dfs好处是代码短,按时间排名,那么先AC的同学排名靠前
|
||
// 用标记数组进行标记,每个位置只使用一次,性能N*N
|
||
const int N = 110;
|
||
int n;
|
||
char g[N][N]; // 地图
|
||
int xa, ya, xb, yb;
|
||
int dx[] = {-1, 0, 1, 0};
|
||
int dy[] = {0, 1, 0, -1};
|
||
bool st[N][N]; // 是否走过
|
||
bool flag;
|
||
|
||
void dfs(int x, int y) {
|
||
if (x == xb && y == yb) {
|
||
flag = true;
|
||
return;
|
||
}
|
||
for (int i = 0; i < 4; i++) {
|
||
int tx = x + dx[i], ty = y + dy[i];
|
||
if (tx < 0 || tx == n || ty < 0 || ty == n) continue;
|
||
if (st[tx][ty]) continue;
|
||
if (g[tx][ty] == '#') continue;
|
||
st[tx][ty] = true;
|
||
dfs(tx, ty);
|
||
}
|
||
}
|
||
|
||
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;
|
||
// 多组测试数组,每次初始化0
|
||
memset(st, 0, sizeof st);
|
||
flag = false;
|
||
|
||
// 这小坑坑挺多啊
|
||
if (g[xa][ya] == '#' || g[xb][yb] == '#') {
|
||
puts("NO");
|
||
continue;
|
||
}
|
||
|
||
st[xa][ya] = true;
|
||
dfs(xa, ya);
|
||
|
||
if (flag)
|
||
puts("YES");
|
||
else
|
||
puts("NO");
|
||
}
|
||
|
||
return 0;
|
||
}
|