简单的BFS

  在很久很久以前,有一位大师级程序员,实力高强,深不可测,代码能力无人能及。从来没有人听说过他的真名,只知道他在完成一段代码后,总会跟上一行注释“十四出品,必属精品”,于是他在编程江湖上便有了绰号“十四”。

  随着十四大师声名远播,意图登门拜访,寻求编程秘法的人也渐渐多了起来。然而,正如他无人知晓的真名一般,十四大师的真面目也少有人得见。这并不是因为大师住处隐秘,而是因为大师居所前有着他亲自布下的阵法,它困住了无数虔诚的求知者。

  作为十四大师崇拜者的小胖经过苦心探寻,终于有一天得到了法阵的地图。

  地图显示,法阵是方形的,纵横皆为五里,在地图上简示为5*5的矩阵,且只由0或1组成。其中,0表示可以走的路,1表示阻止通行的屏障。左上角和右下角分别是阵的入口和出口,这两个位置的数字保证为0。

  既然得到了地图,破解法阵自然不再是难事。现在,小胖不仅想要走出法阵,还想知道怎样才能用最短的路线走出法阵。

Input

  输入是一个5 × 5的二维数组,仅由0、1两数字组成,表示法阵地图。

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)

Hint

  坐标(x, y)表示第x行第y列,行、列的编号从0开始,且以左上角为原点。

  另外注意,输出中分隔坐标的逗号后面应当有一个空格。

题目大意 :求最短路的路径。

题目分析 :其实套用BFS或DFS模板这道就算完成了。稍稍不同的是如何去把每一步都输出出来。

AC代码 :(题目原址

一、利用BFS但不利用队列queue,递推输出(利用指针指到最远位置的特性)。

#include <iostream>
#include <algorithm>
#include <cstdlib>
const int len = 5;
using namespace std;
int Game_map[len * 2][len * 2];
int ix[] = { 1,-1,0,0 };
int iy[] = { 0,0,1,-1 };
struct  Node
{
    int x, y, per;
}p[101];

void init()//地图初始化
{
    for (int i = 0; i < len; i++)
        for (int j = 0; j < len; j++)
            scanf("%d", &Game_map[i][j]);
}

bool icatch(int x, int y)//判断是否在地图内
{
    return x >= 0 && x < len && y >= 0 && y < len && !Game_map[x][y];
}

void print(int x)//递推输出
{
    if (p[x].per != -1)
        print(p[x].per), printf("(%d, %d)\n",p[x].x, p[x].y);
}

void BFS(int x1, int y1)
{
    int dy, dx, front = 0, rear = 1;
    Game_map[x1][y1] = 1;
    p[front].x = x1;
    p[front].y= y1;
    p[front].per = -1;//-1表示不选择
    while (front < rear)
    {
        for (int i = 0; i < 4; i++)
        {

            dx = p[front].x + ix[i];
            dy = p[front].y + iy[i];
            if (!icatch(dx, dy))
                continue;
            else
            {
                Game_map[dx][dy] = 1;
                p[rear].x = dx;
                p[rear].y = dy;
                p[rear].per = front;//有点类似指针,知道你想要去的地方。
                rear++;//四个方位
            }
            if (dx == 4 && dy == 4)
                 print(front);
        }
        front++;//寻找下一个方位
    }

}

int main()
{
    init();
    printf("(0, 0)\n");
    BFS(0, 0);
    printf("(4, 4)\n");
    return 0;
}

二、BFS用队列。

时间: 2024-08-24 11:43:39

简单的BFS的相关文章

超超超简单的bfs——POJ-3278

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 89836   Accepted: 28175 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

HDU 1253:胜利大逃亡(简单三维BFS)

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24937    Accepted Submission(s): 9535 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ--1276--机器设备(河南省第九届省赛,简单的bfs)

机器设备 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Alpha 公司设计出一种节能的机器设备.它的内部结构是由 N 个齿轮组成.整个机器设备有 一个驱动齿轮,当启动它时,它立即按 10,000 圈/小时转速顺时针转动,然后它又带动与它相切 的齿轮反方向,即逆时针转动.齿轮之间互相作用,每个齿轮都可能驱动着多个齿轮,最终带动 一个工作齿轮完成相应的任务. 在这套设备中,记录了每个齿轮的圆心坐标和齿轮半径.已知驱动齿轮位于(0,0),最终的 工作齿轮位于(Xt,

[kuangbin带你飞]专题一 简单搜索 bfs B - Dungeon Master

B - Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You c

hdu--1104--Remainder(简单的bfs)

Remainder Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4078    Accepted Submission(s): 1014 Problem Description Coco is a clever boy, who is good at mathematics. However, he is puzzled by a d

杭电ACM1240——Asteroids!~~简单的BFS

这道题目,三维空间上的BFS,给你起点和终点,看能否找到一条路,O表示可以走,X表示不可以走!~ 理解了题目,就可以用队列来实现BFS来求解. 下面的是AC 的代码: #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; class data { public: int xyz; int count; }; char map

超超超简单的bfs——POJ-1915

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26102   Accepted: 12305 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast

POJ - 2251 - Dungeon Master (简单BFS)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20450   Accepted: 7917 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

HDU - 2612 - Find a way (BFS)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6157    Accepted Submission(s): 2052 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally