定义一个二维数组:

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)

解题思路

广搜问题:在正常基础上多定义一个父节点,可以根据结构体回溯

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[6][6],book[6][6];
struct node
{
    int x;
    int y;
    int s;
    int f;
}que[100];
int main()
{
    int next[4][2]={1,0,0,-1,-1,0,0,1};
    for(int i=0;i<5;i++)
    for(int j=0;j<5;j++)
    scanf("%d",&a[i][j]);
    int head=1,tail=1;
    que[head].x=0;
    que[head].y=0;
    que[head].s=0;
    que[head].f=0;
    book[1][1]=1;
    tail++;
    int flag=0,tx,ty;
    while(head<tail)
    {
        for(int k=0;k<=3;k++)
        {
            tx=que[head].x+next[k][0];
            ty=que[head].y+next[k][1];
            if(tx<0||tx>4||ty<0||ty>4)
            continue;
            if(a[tx][ty]==0&&book[tx][ty]==0)
            {
                book[tx][ty]=1;
                que[tail].x=tx;
                que[tail].y=ty;
                que[tail].s=que[head].s+1;
                que[tail].f=head;
                tail++;
            }
            if(tx==4&&ty==4)
            {
                flag=1;
                break;
            }

}
        if(flag==1)
            break;
            head++;

}
    int ax[30]={0},ay[30]={0};
    int z=-1;
    ax[++z]=que[tail-1].x;
    ay[z]=que[tail-1].y;
      int sum=que[tail-1].s;
   tail=tail-1;

for(int i=1;i<sum;i++)
    {
        tail=que[tail].f;
        ax[++z]=que[tail].x;
        ay[z]=que[tail].y;
    }
    ax[++z]=0;
    ay[z]=0;

for(int j=z;j>=0;j--)
    printf("(%d, %d)\n",ax[j],ay[j]);
    return 0;
}

poj3984的更多相关文章

  1. BFS算法入门--POJ3984

    迷宫问题–POJ3984 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22008 Accepted: 12848 Descri ...

  2. poj3984迷宫问题 广搜+最短路径+模拟队列

    转自:http://blog.csdn.net/no_retreats/article/details/8146585   定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...

  3. 迷宫bfs POJ3984

    #include<stdio.h> int map[5][5]={0,1,0,0,0,       0,1,0,1,0,       0,0,0,0,0,       0,1,1,1,0, ...

  4. 暑假集训(1)第八弹 -----简单迷宫(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, ...

  5. POJ3984 迷宫问题

    典型BFS. #include <iostream> #include <memory.h> #include <queue> #include <map&g ...

  6. poj3984(经典dfs)

    题目链接:http://poj.org/problem?id=3984 分析:直接深搜从起点到终点,如何取最短路线,其实只要优先向下或向右走即可. #include <cstdio> #i ...

  7. poj3984迷宫问题

    一个5 × 5的二维数组,表示一个迷宫.其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 很简单的一道题,迷宫问题,一般都选择两种优先搜索 ...

  8. [poj3984]迷宫问题_bfs

    迷宫问题 题目大意:给你一个5*5的矩阵,求左上角到左下角的最短路径. 注释:0或1的矩阵,1表示不能走,0表示能走,保证有唯一最短路径. 想法:bfs爆搜练习题.通过其实点,定义方向数组,然后进行b ...

  9. poj3984迷宫问题(DFS广搜)

    迷宫问题 Time Limit: 1000MSMemory Limit: 65536K Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, ...

随机推荐

  1. 20145212 《Java程序设计》第8周学习总结

    20145212 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以让你 ...

  2. codeforces 709B Checkpoints

    题目链接:http://codeforces.com/problemset/problem/709/B 题目大意: 第一行给出两个数 n,x.第二行 输入 n 个数. 要求:从x位置遍历 n-1 个位 ...

  3. JS实现的一个query字符串转Json格式数据的方法

    输入字符串的格式是 a=1&b=2&c=3 $.par2Json = function (string, overwrite) { var obj = {}, pairs = stri ...

  4. yourtour的几种链接

    php,html {:URL('User-Register/index')}    格式:http://www.xxx.com/index.php?g=User&m=User&a=in ...

  5. mysql 查询表结构 查询索引

    首先进入到mysql里 show databases; 选择数据库 use xxxcms; 查询数据库下的表结构 show create table 表名; 这样看着不太好可以后面加\G show c ...

  6. border opacity

    div { border: 1px solid rgb(127, 0, 0); border: 1px solid rgba(255, 0, 0, .5); -webkit-background-cl ...

  7. 经纬度距离计算Java实现代码

    public class test { private static double rad(double d) { return d * Math.PI / 180.0; } public stati ...

  8. asp.net core 中的MD5加密

    尝试了很长时间,但是一直报core 5 不可用,当时就崩溃了. 但是偶然的机会 我添加了Microsoft.AspNet.Identity 之后.MD5就好用了. 估计是这个报实现了core5下的MD ...

  9. 在linux下运行apt-get update 时,报错/var/lib/apt/lists/lock

    在运行apt-get update 时,报下面的错误: E: 无法获得锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavai ...

  10. firefox浏览器不能使用window.close的解决方案

    javascript中window.close()函数用来关闭窗体,而且IE.google.firefox浏览均支持,但由于firefox浏览器dom.allow_scripts_to_close_w ...