POJ - 3984 迷宫问题 (BFS+前缀处理输出路径)

#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1005

using namespace std;

int Map[MAX][MAX],n,v[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};

struct node
{
    int x,y,step;
} ans[MAX][MAX];

void DFS(int x,int y)
{
    if(x==0&&y==0)
        return ;

    Ans(ans[x][y].x,ans[x][y].y);
    printf("(%d, %d)\n",x,y);
}

void BFS()
{
    queue<node>Q;
    node a,next;
    a.x=0;
    a.y=0;
    a.step=0;
    Q.push(a);
    Map[0][0]=1;

    while(!Q.empty())
    {
        a=Q.front();
        Q.pop();

        if(a.x==4 && a.y==4)
        {
            printf("(0, 0)\n");
            Ans(4,4);
            return;
        }

        for(int i=0; i<4; i++)
        {
            next.x=a.x+v[i][0];
            next.y=a.y+v[i][1];

            if(next.x>=0 && next.x<5 && next.y>=0 && next.y<5 && Map[next.x][next.y]==0)
            {
                Map[next.x][next.y]=1;
                next.step=a.step+1;

                ans[next.x][next.y].x=a.x;//记录上一个点
                ans[next.x][next.y].y=a.y;
                Q.push(next);
            }
        }
    }
}

int main()
{
    int i,j;

    for(i=0; i<5; i++)
        for(j=0; j<5; j++)
            scanf("%d",&Map[i][j]);

    BFS();

    return 0;
}
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int map[6][6];
int book[6][6];
int ds[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
struct node
{
    int s,y;
}point[6][6];
int judge(int a,int b)
{
    if(a>=1&&a<=5&&b>=1&&b<=5)
        return 1;
    return 0;
}
void dfs(int s,int y)
{
    if(s==1&&y==1)
        return ;
    dfs(point[s][y].s,point[s][y].y);
    printf("(%d, %d)\n",s-1,y-1);
}
int bfs(int a,int b)
{
    book[a][b]=1;
    queue<node>Q;
    node temp1;
    temp1.s=a;
    temp1.y=b;
    Q.push(temp1);
    while(!Q.empty())
    {
        node temp2=Q.front();
        Q.pop();
        if(temp2.s==5&&temp2.y==5)
        {
            printf("(0, 0)\n");
            dfs(point[5][5].s,point[5][5].y);
            printf("(4, 4)\n");
            return 0;
        }
        for(int i=0;i<4;i++)
        {
            temp1.s=temp2.s+ds[i];
            temp1.y=temp2.y+dy[i];
            if(judge(temp1.s,temp1.y)&&map[temp1.s][temp1.y]==0)
            {
                if(book[temp1.s][temp1.y]==0)
                {
                    //printf("1");
                    book[temp1.s][temp1.y]=1;
                    point[temp1.s][temp1.y].s=temp2.s;
                    point[temp1.s][temp1.y].y=temp2.y;
                    Q.push(temp1);
                }
            }
        }
    }
}

int main()
{
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=5;j++)
        {
            scanf("%d",&map[i][j]);
        }
    }
    bfs(1,1);
}


 
div class="plm" style="text-align: center; font-size: 12pt; color: rgb(34, 34, 34); clear: both;"><div class="ptt" id="problem_title" style="font-size: 18pt; font-weight: bold; color: blue; padding: 10px;"><span style="color: green;">- </span>迷宫问题</div><span id="crawlSuccess" class="crawlInfo" style="display: inline;"><strong>Time Limit:</strong><span id="timeLimit">1000</span>MS     <strong>Memory Limit:</strong><span id="memoryLimit">65536</span>KB     <strong>64bit IO Format:</strong><span id="_64IOFormat">%I64d & %I64u</span></span><div id="problem_opt" style="font-size: 12px; margin-top: 10px;"><a target=_blank id="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url("../jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png") 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Submit</span></a> <a target=_blank id="problem_status" href="http://acm.hust.edu.cn/vjudge/contest/122002#status//N/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url("../jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png") 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Status</span></a></div></div><div style="color: rgb(34, 34, 34); font-family: Verdana, Arial, sans-serif; font-size: 14px; width: 960px; margin: auto;"><div class="hiddable" id="vj_description" style="font-family: "times new roman"; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif; font-size: 12pt;">定义一个二维数组: 
<pre style="white-space: pre-wrap; word-wrap: break-word;">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)

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值