POJ 3984 迷宫问题

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值