题解
2024-10-27 09:34:57
发布于:江苏
0阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[50][50];
bool flag = false;
int v[50][50];
bool in(int x , int y)
{
return (x>=1 and x<=n and y>=1 and y<=m);
}
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
void DFS(int x , int y){
if(x == n and y == m){
flag = true;
return ;
}
v[x][y] = 1;
int nx , ny;
for(int i=0;i<4;i++)
{
nx = x + dx[i];
ny = y + dy[i];
if( in(nx,ny) and a[nx][ny] != 1 and v[nx][ny] != 1) DFS(nx,ny);
}
}
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
DFS(1,1);
if(flag == true) cout<<"YES";
else cout<<"NO";
return 0;
}
这里空空如也
有帮助,赞一个