(bfs+路径回溯)POJ3984 迷宫问题

传送门: POJ3984 迷宫问题

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)
回头再做这个题,第一个代码应该不是我想出来的,没什么印象啦,第二个代码是现在(2018/5/15)写的,不如第一个简洁,,向第一个代码学习哭,pair和回溯可以替换掉结构体和栈,大大简化代码。再来个代码3好了,代码2的修改版。
题目代码1:
//对于方向向量的赋值有疑问??? 已解决
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int a[5][5];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//错误 上,下,右,左
//int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//正确 第一个是行+1,列不变,所以就是向下 后面的同理分别是右,上,左
//其实都算对,主要是下面没有设置访问标志,导致访问出错
pair<int,int> path[25][25],tmp;
int vis[5][5];

void dfs()
{
    memset(vis,0,sizeof(vis));
    queue<pair<int,int> > q;
    q.push(make_pair(0,0));
    vis[0][0]=1;
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp.first==4&&tmp.second==4)
            return ;
        for(int i=0;i<4;i++)
        {
            int x=tmp.first+dir[i][0];
            int y=tmp.second+dir[i][1];
            if(x>=0&&y>=0&&x<5&y<5&&!vis[x][y]&&a[x][y]==0)
            {
                q.push(make_pair(x,y));
                vis[x][y]=1;
                path[x][y].first=tmp.first,
                path[x][y].second=tmp.second;
            }
        }
    }
}

void putout(int x,int y)
{
    if(x==0&&y==0)
    {
         printf("(0, 0)\n");
         return ;
    }
    putout(path[x][y].first,path[x][y].second);
    printf("(%d, %d)\n",x,y);

}

int main()
{
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    dfs();
    putout(4,4);
    return 0;
}

代码2

#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<string>
#include<string.h>
using namespace std;
int mp[5][5],vis[5][5];
typedef struct Node{
    int u,v;
}Node;

Node path[5][5];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

void dfs(int x,int y){
    memset(vis,0,sizeof(vis));
    queue<Node> q;
    Node tmp,nxt;
    nxt.u=x;
    nxt.v=y;
    vis[x][y]=1;
    q.push(nxt);
    while(!q.empty()){
        //cout<<"poopp"<<endl;
        tmp=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            nxt.u=tmp.u+dir[i][0];
            nxt.v=tmp.v+dir[i][1];
            if(nxt.u<0||nxt.v<0||nxt.u>4||nxt.v>4||mp[nxt.u][nxt.v]==1)
                continue;
            if(!vis[nxt.u][nxt.v]){
                vis[nxt.u][nxt.v]=1;
                path[nxt.u][nxt.v].u=tmp.u;
                path[nxt.u][nxt.v].v=tmp.v;
                if(nxt.u==4&&nxt.v==4)
                    return ;
                q.push(nxt);
            }
        }
    }
}

int main(){
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            scanf("%d",&mp[i][j]);
        }
    }
    dfs(0,0);
    stack<Node> s;
    Node tmp;
    int i=4,j=4,t;
    while(i!=0||j!=0){
        tmp.u=i;
        tmp.v=j;
        s.push(tmp);
        t=path[i][j].u;
        j=path[i][j].v;
        i=t;
    }
    printf("(0, 0)\n");
    while(!s.empty()){
        tmp=s.top();
        s.pop();
        printf("(%d, %d)\n",tmp.u,tmp.v);
    }
    return 0;
}

代码3(代码2的修改版):

#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<string>
#include<string.h>
using namespace std;
int mp[5][5],vis[5][5];

pair<int,int> path[5][5];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

void dfs(int x,int y){
    memset(vis,0,sizeof(vis));
    queue<pair<int,int> > q;
    pair<int,int> tmp,nxt;
    vis[x][y]=1;
    q.push(make_pair(x,y));
    while(!q.empty()){
        //cout<<"poopp"<<endl;
        tmp=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            nxt.first=tmp.first+dir[i][0];
            nxt.second=tmp.second+dir[i][1];
            if(nxt.first<0||nxt.second<0||nxt.first>4||nxt.second>4||mp[nxt.first][nxt.second]==1)
                continue;
            if(!vis[nxt.first][nxt.second]){
                vis[nxt.first][nxt.second]=1;
                path[nxt.first][nxt.second].first=tmp.first;
                path[nxt.first][nxt.second].second=tmp.second;
                if(nxt.first==4&&nxt.second==4)
                    return ;
                q.push(nxt);
            }
        }
    }
}

void Input(int x,int y){
    if(x==0&&y==0){
        printf("(0, 0)\n");
        return ;
    }
    Input(path[x][y].first,path[x][y].second);
    printf("(%d, %d)\n",x,y);
}

int main(){
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            scanf("%d",&mp[i][j]);
        }
    }
    dfs(0,0);
    Input(4,4);
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值