#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define N 101
using namespace std;
char ch;
int n,xa,ya,xb,yb;
bool flag;
int vis[N][N];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int x,int y)
{
for(int i=0;i<4;i++)
{
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(nx>=0&&nx<n&&ny>=0&&ny<n&&vis[nx][ny]==0)
{
vis[nx][ny]=1;
if(nx==xb&&ny==yb)
{
cout<<"YES"<<endl;
flag=true;
break;
}
else
dfs(nx,ny);
}
}
}
int main()
{
int k;
cin>>k;
while(k--)
{
memset(vis,0,sizeof(vis));
flag=false;
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
cin>>ch;
if(ch=='#')
vis[i][j]=1;
}
cin>>xa>>ya;
cin>>xb>>yb;
if(vis[xa][ya]||vis[xb][yb])
{
cout<<"NO"<<endl;
continue;
}
else
dfs(xa,ya);
if(!flag)
cout<<"NO"<<endl;
}
return 0;
}
解法2
#include <bits/stdc++.h>
using namespace std;
#define N 105
int n, ha, la, hb, lb;
char mp[N][N];
bool vis[N][N];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
bool isReach;
void dfs(int sx, int sy)
{
if(isReach)//剪枝:如果发现已经能到了,就不再递归运行了
return;
for(int i = 0; i < 4; ++i)
{
int x = sx + dir[i][0], y = sy + dir[i][1];
if(x >= 0 && x < n && y >= 0 && y < n && vis[x][y] == false && mp[x][y] == '.')
{
vis[x][y] = true;
if(x == hb && y == lb)
{
isReach = true;
return;
}
else
dfs(x, y);
}
}
}
int main()
{//判断起点终点是否在一个连通块内
int k;
cin >> k;
while(k--)
{
cin >> n;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
cin >> mp[i][j];
cin >> ha >> la >> hb >> lb;
memset(vis, 0, sizeof(vis));
isReach = false;
vis[ha][la] = true;
if(ha == hb && la == lb)//如果起点和终点重合,直接就能到达
isReach = true;
if(mp[ha][la] != '#' && mp[hb][lb] != '#')
dfs(ha, la);
cout << (isReach ? "YES" : "NO") << endl;
}
return 0;
}
评论区