题目
分析:
分类讨论:若A,B点没法走直接结束,否则dfs看能不能走到。注意走过的格子一定要及时更新,否则,由于是上下左右四个方向走,下一次会走到上一次的格子,陷入死循环
代码:
#include <iostream>
using namespace std;
const int N = 110;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
char g[N][N];
bool st[N][N];
int k, n;
bool flag;
void dfs(int xa, int ya, int xb, int yb)
{
for(int i = 0; i < 4; i ++ )
{
int nx = xa + dx[i], ny = ya + dy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < n && g[nx][ny] == '.')
{
g[nx][ny] = '#';
if(nx == xb && ny == yb)
{
flag = true;
return;
}
dfs(nx, ny, xb, yb);
}
}
}
int main()
{
cin >> k;
while(k -- )
{
cin >> n;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < n; j ++ )
cin >> g[i][j];
int xa, ya, xb, yb;
cin >> xa >> ya >> xb >> yb;
if(g[xa][ya] == '#' || g[xb][yb] == '#')
{
puts("NO");
continue;
}
flag = false;
g[xa][ya] = '#';
dfs(xa, ya, xb, yb);
if(flag) puts("YES");
else puts("NO");
}
}