非常简单
2024-11-02 17:56:27
发布于:江苏
#include<bits/stdc++.h>
using namespace std;
int mp[50][50];
int vis[50][50];
int n,m,f;
void dfs(int x,int y)
{
vis[x][y]=1;
if(xn and ym)
{
f=1;
return;
}
else{
int nx,ny;
nx=x-1,ny=y;
if(mp[nx][ny]==0 and nx>=1 and nx<=n and ny>=1 and ny<=m and vis[nx][ny]==0)
{
dfs(nx,ny);
}
nx=x+1,ny=y;
if(mp[nx][ny]==0 and nx>=1 and nx<=n and ny>=1 and ny<=m and vis[nx][ny]==0)
{
dfs(nx,ny);
}
nx=x,ny=y-1;
if(mp[nx][ny]==0 and nx>=1 and nx<=n and ny>=1 and ny<=m and vis[nx][ny]==0)
{
dfs(nx,ny);
}
nx=x,ny=y+1;
if(mp[nx][ny]==0 and nx>=1 and nx<=n and ny>=1 and ny<=m and vis[nx][ny]0)
{
dfs(nx,ny);
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>mp[i][j];
}
}
dfs(1,1);
if(f1) cout<<"YES";
else cout<<"NO";
return 0;
}
这里空空如也
有帮助,赞一个