7 solutions

  • 0
    @ 2022-3-30 20:50:28
    • 注意要特判起点和终点是否是可通行的,不然有一个测试点过不了
    #include <iostream>
    using namespace std;
    const int N=101;
    int a[N][N],ma[N][N];
    int n,sx,sy,fx,fy,nex,ney,ans;
    int opx[5]={1,-1,0,0};
    int opy[5]={0,0,1,-1};
    void dfs(int x,int y)
    {
    	if(x==fx&&y==fy) ans++;
    	else{
    		for(int i=0;i<4;i++)
    		{
    			nex=x+opx[i];
    			ney=y+opy[i];
    			if(nex>=1&&nex<=n&&ney>=1&&ney<=n&&ma[nex][ney]==0)
    			{
    				ma[nex][ney]=1;
    				dfs(nex,ney);
    				ma[nex][ney]=0;
    			}
    		}
    	}
    }
    int main()
    {
    	cin>>n;
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=1;j<=n;j++)
    		{
    			cin>>a[i][j];
    			if(a[i][j]==1)
    				ma[i][j]=1;
    		}
    	}
    	cin>>sx>>sy>>fx>>fy;
    	if(ma[sx][sy]==1||ma[fx][fy]==1)
    	{
    		cout<<"NO"<<endl;
    	}
    	else
    	{
    	dfs(sx,sy);
    	if(ans==0)
    	{
    		cout<<"NO"<<endl;
    	}
    	else{
    		cout<<"YES"<<endl;
    	}
    	}
    	return 0;
    }
    

    Information

    ID
    767
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    8
    Tags
    # Submissions
    927
    Accepted
    165
    Uploaded By