POJ 3984 迷宫问题
POJ入口:http://poj.org/problem?id=3984
参考网址:https://blog.csdn.net/qq_41402220/article/details/80114015
题目大意
一个迷宫
1表示墙壁 0表示可以走的路
只能横着走或竖着走 不能斜着走
输出左上角到右下角的最短路径(难点为路径的输出)
解题思路
待写 我太懒了…
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <deque>
using namespace std;
#define mm(a) memset(a, 0, sizeof(a))
const int MAXN = 10;
struct position{
int x, y;
};
struct road{
deque<position> rd;
};
int fx[4] = {0, 0, -1, 1};
int fy[4] = {-1, 1, 0, 0};
int maze[MAXN][MAXN];
int vis[MAXN][MAXN];
int step[MAXN][MAXN];
int main(){
mm(vis);
mm(maze);
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
scanf("%d", &maze[i][j]);
position ori;
ori.x = 0;
ori.y = 0;
vis[0][0] = 1;
road o;
o.rd.push_back(ori);
queue<road> process;
process.push(o);
while (process.size()){
road next = process.front();
position end_pos = next.rd.back();
if (end_pos.x == 4 && end_pos.y == 4){
while (next.rd.size()){
position ret = next.rd.front();
printf("(%d, %d)\n", ret.x, ret.y);
next.rd.pop_front();
}
return 0;
}
process.pop();
for (int i = 0; i < 4; i++){
road nroad = next;
int nx = end_pos.x+fx[i];
int ny = end_pos.y+fy[i];
if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5){
if (maze[nx][ny] != 1 && !vis[nx][ny]){
position tmp_pos;
tmp_pos.x = nx;
tmp_pos.y = ny;
nroad.rd.push_back(tmp_pos);
process.push(nroad);
vis[nx][ny] = 1;
step[nx][ny] = step[end_pos.x][end_pos.y]+1;
}
}
}
}
return 0;
}