提交时间:2025-07-21 11:01:40
运行 ID: 334975
#include<bits/stdc++.h> using namespace std; int n,s1,s2,e1,e2; int a[110][110]; int dx[10]={0,0,1,0,-1}; //右下左上 int dy[10]={0,1,0,-1,0}; bool f=0; //假设走不到终点 void dfs(int x, int y){ //cout<<x<<" "<<y<<endl; a[x][y]=1; //将走过的点标记为1 int tx,ty; for(int i=1; i<=4; i++){ tx=x+dx[i]; ty=y+dy[i]; //判断点是否有效 if(tx>=1 && tx<=n && ty>=1 && ty<=n && a[tx][ty]==0 ) //&&f==false if(tx==e1 && ty==e2){ f=1; //exit(0); //停止程序 }else{ dfs(tx,ty); } } } int main(){ cin>>n; for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ cin>>a[i][j]; } } cin>>s1>>s2>>e1>>e2; if(a[s1][s2]==1 || a[e1][e2]==1){ cout<<"NO"; }else{ dfs(s1,s2); if(f==true){ cout<<"YES"; }else{ cout<<"NO"; } } return 0; }