不值得是 提高-
2024-09-08 10:47:31
发布于:广东
11阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int a[10][10],dx[4] = {1,-1,0,0},dy[4] = {0,0,1,-1},vis[10][10];
constexpr int n = 5,m = 5;
struct node{
int x,y,step;
};
queue<node> q;
int main(){
for(int i = 0;i < n;i ++) for(int j = 0;j < m;j ++) cin >> a[i][j];
q.push({0,0,0});
while(!q.empty()){
int ix = q.front().x,iy = q.front().y,istep = q.front().step;
q.pop();
if(ix == 4 and iy == 4){
cout << istep;
return 0;
}
for(int i = 0;i < 4;i ++){
int nx = ix + dx[i],ny = iy + dy[i];
if(nx >= 0 and ny >= 0 and nx < n and ny < m and vis[nx][ny] == 0 and a[nx][ny] == 0){
vis[nx][ny] = 1;
q.push({nx,ny,istep + 1});
}
}
}
cout << -1;
return 0;
}
这里空空如也
有帮助,赞一个