地下迷宫(bfs输出路径)

时间:2021-02-02 11:37:39

地下迷宫(bfs输出路径)地下迷宫(bfs输出路径)地下迷宫(bfs输出路径)

题解:开一个pre数组用编号代替当前位置,编号用结构题另存,其实也可以i*m+j来代替,我写的有点麻烦了;

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#pragma(1)
typedef struct Node{
int x, y;
int t;
int sz;
friend bool operator < (Node a, Node b){
return a.t > b.t;
}
}Node; Node node[];
priority_queue<Node>Q;
int vis[][];
int mp[][];
int pre[];
int disx[] = {,,,-};
int disy[] = {,-,,};
int mv(int x, int y){
if(x == ){
if(y == ){
return ;
}
else if(y == -){
return ;
}
}else if(x == ){
if(y == )
return ;
}else if(x == -){
if(y == )
return ;
}
}
void print(int sz){
if(sz == )return;
print(pre[sz]);
printf(",[%d,%d]", node[sz].x, node[sz].y);
}
void bfs(int n, int m, int p){
Node a,b;
a.x = , a.y = , a.t = p, a.sz = ;
while(!Q.empty()){
Q.pop();
}
Q.push(a);
memset(vis, , sizeof(vis));
memset(pre, , sizeof(pre));
memset(node, , sizeof(node));
vis[][] = ;
node[] = {, , , };
int sz = ;
while(!Q.empty()){
a = Q.top();
Q.pop();
for(int i = ; i < ; i ++){
b.x = a.x + disx[i];
b.y = a.y + disy[i];
b.t = a.t - mv(disx[i], disy[i]);
b.sz = ++sz;
node[sz] = {b.x, b.y, b.t, b.sz};
pre[sz] = a.sz;
if(b.x < || b.y < || b.x >= n || b.y >= m)continue;
if(vis[b.x][b.y])continue;
if(b.t < )continue;
if(mp[b.x][b.y] == )continue;
if(b.x == && b.y == m - ){
printf("[%d,%d]",,);
print(sz);
puts("");
return;
}
vis[b.x][b.y] = ;
Q.push(b);
}
}
puts("Can not escape!");
return;
}
int main(){
int n, m, q;
while(~scanf("%d%d%d", &n, &m, &q)){
memset(mp, , sizeof(mp));
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
scanf("%d", &mp[i][j]);
}
}
bfs(n, m, q);
}
return ;
}