题目总版

wxhhpsmaq__ @ 2024-11-23 14:57:25

//
// Created by Administrator on 2024/11/23.
//
#include <bits/stdc++.h>
using namespace std;
int p[9999][9999],a;
int o[4]={-1,0,0,1};
int k[4]={0,1,-1,0};
int n,m;
bool oo= false;
int dfs(int x,int y)
{
    if(oo) return 0;
    if(p[x][y]==1) return 0;
    if(x==n&&y==m)
    {
        cout<<"yes";
        oo= true;
        return 0;
    }
    for (int i = 0; i < 4; ++i) {
        if(x+o[i]>0&&y+k[i]>0&&x+o[i]<=a&&y+k[i]<=a&&p[x+o[i]][y+k[i]]==0)
        {
            p[x][y]=1;
            dfs(x+o[i],y+k[i]);
            p[x][y]=0;
        }
    }
}
using namespace std;
int main()
{
    cin>>a;
    for (int i = 1; i <= a; ++i) {
        for (int j = 1; j <= a; ++j) {
            cin>>p[i][j];
        }
    }
    int i,j;
    cin>>i>>j>>n>>m;
    dfs(i,j);
    if(!oo) cout<<"NO";
}

|