POJ - 3984 迷宫问题 BFS+记录并输出最短路径

40 篇文章 0 订阅
16 篇文章 0 订阅

POJ - 3984 迷宫问题

思路:

重点是如何记录并输出最短路径。我们可以用一个 node类型的 pre[ ][ ] 二维数组记录这个状态的前一个状态。如果是正向搜索(从(0,0)到(4,4)),找到结果时需要逆序输出;如果是逆向搜索(从(4,4)到(0,0))找到结果直接输出。

逆向搜索(时间少)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn=1e6+10;
int vis[10][10];
int a[10][10];
int b[4][2]={
    -1,0,
    1,0,
    0,-1,
    0,1
};
struct node
{
    int x,y;
};
node pre[10][10];  //记录上一个状态
void bfs(int x,int y)
{
    queue<node> q;
    node w,h;
    w.x = x;
    w.y = y;
    q.push(w);
    vis[w.x][w.y] = 1;
    while(!q.empty())
    {
        h = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            w.x = h.x+b[i][0];
            w.y = h.y+b[i][1];
            if(!vis[w.x][w.y] && a[w.x][w.y]==0 && w.x>=0&&w.x<5 && w.y>=0&&w.y<5)
            {
                q.push(w);
                vis[w.x][w.y] = 1;
                pre[w.x][w.y] = h;
                if(w.x==0 && w.y==0)
                {
                    while(!(w.x==4 && w.y==4))
                    {
                        printf("(%d, %d)\n",w.x,w.y);
                        w = pre[w.x][w.y];
                    }
                    return ;
                }
            }
        }
    }
}
int main()
{
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
            scanf("%d",&a[i][j]);
    }
    memset(vis,0,sizeof(vis));
    bfs(4,4);
    printf("(4, 4)\n");
    return 0;
}

正向搜索(比逆向搜索多用了一个栈)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn=1e6+10;
int vis[10][10];
int a[10][10];
int b[4][2]={
    -1,0,
    1,0,
    0,-1,
    0,1
};
struct node
{
    int x,y;
};
node pre[10][10];
stack<node> s;
void bfs(int x,int y)
{
    queue<node> q;
    node w,h;
    w.x = x;
    w.y = y;
    q.push(w);
    vis[w.x][w.y] = 1;
    while(!q.empty())
    {
        h = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            w.x = h.x+b[i][0];
            w.y = h.y+b[i][1];
            if(!vis[w.x][w.y] && a[w.x][w.y]==0 && w.x>=0&&w.x<5 && w.y>=0&&w.y<5)
            {
                q.push(w);
                vis[w.x][w.y] = 1;
                pre[w.x][w.y] = h;
                if(w.x==4 && w.y==4)
                {
                    while(!(w.x==0 && w.y==0))
                    {
                        s.push(w);
                        w = pre[w.x][w.y];
                    }
                    return ;
                }
            }
        }
    }
}
void print()
{
    while(!s.empty())
    {
        node c = s.top();
        printf("(%d, %d)\n",c.x,c.y);
        s.pop();
    }
}
int main()
{
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
            scanf("%d",&a[i][j]);
    }
    memset(vis,0,sizeof(vis));
    bfs(0,0);
    printf("(0, 0)\n");
    print();
    return 0;
}

一开始用指针记录上一个状态,但是不知道哪里出了问题,请各位大佬指教(非常感谢)

错误代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn=1e6+10;
int vis[10][10];
int a[10][10];
int b[4][2]={
    -1,0,
    1,0,
    0,-1,
    0,1
};
struct node
{
    int x,y;
    node *pre;  //记录它的上一种状态
};
node h[10000];
stack<node> s;

void bfs(int x,int y)
{
    queue<node> q;
    node w;

    w.x = x;
    w.y = y;
    w.pre = NULL;
    q.push(w);
    vis[w.x][w.y] = 1;
    int cnt = -1;
    while(!q.empty())
    {
        h[++cnt] = q.front();
        cout<<cnt<<endl;
        q.pop();
        for(int i=0;i<4;i++)
        {
            w.x = h[cnt].x+b[i][0];
            w.y = h[cnt].y+b[i][1];
            if(!vis[w.x][w.y] && a[w.x][w.y]==0 && w.x>=0&&w.x<5 && w.y>=0&&w.y<5)
            {
                cout<<h[cnt].x<<" "<<h[cnt].y<<endl;
                cout<<w.x<<" "<<w.y<<endl;
                q.push(w);
                vis[w.x][w.y] = 1;
                w.pre = &h[cnt];
                if(w.x==4 && w.y==4)
                {
                    while(w.pre)
                    {
                        s.push(w);
                        w = *w.pre;
                    }
                    return ;
                }
            }
        }
    }
}
void print()
{
    while(!s.empty())
    {
        node c = s.top();
        printf("(%d, %d)\n",c.x,c.y);
        s.pop();
    }
}
int main()
{
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
            scanf("%d",&a[i][j]);
    }
    memset(vis,0,sizeof(vis));
    bfs(0,0);
    print();
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值