注意初始点的判断:切忌
#include<iostream> #include<algorithm> #include<cstring> using namespace std ; const int N = 110 ; int dis[4][2] = {{-1,0},{1,0},{0,-1},{0,1}} ; char g[N][N] ; int n ; int a,b,c,d ; bool vis[N][N] ; bool flag ; void dfs(int u , int v ){ if(u == c && d == v){ flag = 1 ; return ; } for(int i = 0 ; i < 4 ; i++){ int x = u + dis[i][0] , y = v + dis[i][1] ; if(x<0||x>=n||y<0||y>=n||vis[x][y]||g[x][y] == '#') continue ; if(flag) return ; vis[x][y] = 1 ; dfs(x,y) ; } return ; } int main(){ int t ; cin >> t ; while(t --){ cin >> n ; memset(vis,0,sizeof(vis)) ;flag = 0 ; for(int i = 0 ; i < n ;i ++) cin >> g[i] ; cin >> a >> b >> c >> d ; vis[a][b] = 1 ; dfs(a,b) ; if(g[a][b] == '#') flag = 0 ; if(a == c && c == d) flag = 1 ; if(flag) cout << "YES" << endl ; else cout <<"NO" << endl ; } }