POJ 3984 迷宫问题

题目链接 POJ 3984

迷宫问题
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11047 Accepted: 6602

Description

定义一个二维数组: 
int maze[5][5] = {

	0, 1, 0, 0, 0,

	0, 1, 0, 1, 0,

	0, 0, 0, 0, 0,

	0, 1, 1, 1, 0,

	0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)


分析:

网上大部分解法是用结构体数组模拟队列,bfs,设置队列的首,尾指针,模拟广搜的过程.

每次取队首元素,看是否走到了终点,若走到就将路径递归打印出来,没有走到的话就继续走(废话),

向四个方向搜索,并记录他的前一个位置的编号。


代码:

#include <cstdio>
#include <iostream>
using namespace std;

const int dir[][2] = {0,1, 0,-1, 1,0, -1,0};
int Im[8][8];

struct pos
{
    int x, y;    //坐标
    int pre;     //记录当前位置的前一个位置的编号
} Q[50];

void print(int x)       //递归打印路径
{
    if(x != -1)
    {
        print(Q[x].pre);
        cout << '(' << Q[x].x << ", " << Q[x].y << ")\n";
    }
}

void bfs()
{
    Q[0].x = Q[0].y = 0;      //初始化起点
    Q[0].pre = -1;
    int Front = 0, Rear = 1;    //队列的头,尾指针
    while(Front < Rear)         //当队列非空
    {
        if(Q[Front].x == 4 && Q[Front].y == 4)    //走到了终点,快累死了
        {
            print(Front);
            return;
        }
        for(int i = 0; i < 4; i++)      //搜索四个方向
        {
            int tx = Q[Front].x + dir[i][0];
            int ty = Q[Front].y + dir[i][1];
            if(tx >= 0 && tx < 5 && ty >= 0 && ty < 5 && Im[tx][ty] == 0)
            {
                Im[tx][ty] = 1;     //标记为已经搜过
                Q[Rear].x = tx;     //将搜到的位置加入队列尾部
                Q[Rear].y = ty;
                Q[Rear].pre = Front;
                Rear++;             //尾部指针后移
            }
        }
        Front++;                    //头指针后移,就是将队列首元素舍弃
    }
}
int main()
{
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++)
            scanf("%d", &Im[i][j]);
    }
    bfs();
    return 0;
}

还有一种解法,用队列,真正的队列,如假包换。结构体里用 string 记录走到当前位置的最短路径,

由于是5 X 5 的数组,就算将所有可到达的点的路径记录下来也不会爆内存,最后搜到终点时输出路径即可。


代码:

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

const int dir[][2] = {0,1, 0,-1, 1,0, -1,0};
int Im[8][8];
struct pos
{
    int x, y;       //坐标
    string str;     //记录路径,5 X 5 的二维数组不会超内存
};

void bfs()
{
    queue<pos> Q;
    pos Now, Next;
    Now.x = Now.y = 0;      //初始化起点
    Now.str = "00";
    Q.push(Now);        //起点进队列
    while(!Q.empty())
    {
        Now = Q.front();
        Q.pop();
        if(Now.x == 4 && Now.y == 4)    //走到了终点,快累死了
        {
            int len = Now.str.length();     //打印路径
            for(int i = 0; i < len; i += 2)
                cout << "(" << Now.str[i] << ", " << Now.str[i + 1] << ")\n";
        }
        for(int i = 0; i < 4; i++)      //搜索四个方向
        {
            int tx = Now.x + dir[i][0];
            int ty = Now.y + dir[i][1];
            if(tx >= 0 && tx < 5 && ty >= 0 && ty < 5 && Im[tx][ty] == 0)
            {
                Im[tx][ty] = 1;     //标记为已经搜过
                Next.x = tx;
                Next.y = ty;
                char c = tx + '0';      //记录路径
                Next.str = Now.str + c;
                c = ty + '0';
                Next.str += c;
                Q.push(Next);
            }
        }
    }
}
int main()
{
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++)
            scanf("%d", &Im[i][j]);
    }
    bfs();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值