全部都在注释里面了~~
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
char g[N][N];
int d[N][N];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int T;
int spfa(int x1, int y1, int x2, int y2, int n)
{
memset(d, 0x3f, sizeof d); // 初始化距离为正无穷
queue<PII> q; // 开队列
q.push({x1, y1});
d[x1][y1] = 0;
while(q.size())
{
auto t = q.front(); //取队头元素
q.pop();
for(int i = 0; i < 4; i ++)
{
int x = t.first + dx[i], y = t.second +dy[i]; // 扩展四个方向
if(x < 0 || x >= n || y < 0 || y >= n) continue; // 如果走出边界就下一次
if(g[x][y] == '#') continue; // 如果走到了障碍就下一次
d[x][y] = d[t.first][t.second] + 1; // 更新距离
g[x][y] = '#'; // 过河拆桥
q.push({x, y});
}
}
if(d[x2][y2] != 0x3f3f3f3f) return 1; // 只要距离不是正无穷就说明能找到。
else return 0;
}
int main()
{
cin >> T;
while(T --)
{
int n;
cin >> n;
memset(g, 0, sizeof g); // 初始化g 为0
for(int i = 0; i < n; i ++) cin >> g[i];
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2; // 读入两个点
if(g[x1][y1] == '#' || g[x2][y2] == '#') cout << "NO" << endl; // 如果起点和终点都是障碍就不对
else if(spfa(x1, y1, x2, y2, n) == 1) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}