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