简单的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)
",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)
");
    BFS(0, 0);
    printf("(4, 4)
");
    return 0;
}
View Code

二、BFS用队列。

 

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <deque>
const int len = 5;
using namespace std;
int Game_map[len * 2][len * 2];//游戏地图
int vis[2 * len][2 * len];//标记是否走过
int ix[] = { 1,-1,0,0 };
int iy[] = { 0,0,1,-1 };

struct ss
{
    int x, y;//记录每个点的坐标
};

struct TnT
{
    deque<ss> mm;//每条路线的坐标全部储存起来
};

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 && !vis[x][y] && !Game_map[x][y];
}


TnT BFS(int x, int y)//开始执行
{
    TnT gg;
    ss gl;
    gl.x = x, gl.y = y;
    vis[x][y] = 1;
    gg.mm.push_back(gl);
    queue<TnT> q;
    q.push(gg);//存入最初的起始点
    while (!q.empty())
    {
        TnT ml = q.front();
        ss xg = ml.mm.back();
        if (xg.x == 4 && xg.y == 4)//如果到达则将这条路线返回
            return ml;
        for (int i = 0; i < 4; i++)//四个方向
        {
            TnT sg = ml;
            ss x1 = ml.mm.back();
            x1.x += ix[i], x1.y += iy[i];
            if (!icatch(x1.x, x1.y))//不满足则继续
                continue;
            else
            {
                vis[x1.x][x1.y] = 1;
                sg.mm.push_back(x1);
                q.push(sg);
            }
        }
        q.pop();
    }
    return q.front();
}


int main()
{
    //freopen("out.txt", "r", stdin);
    init();
    TnT q = BFS(0, 0);
    while (!q.mm.empty())
    {
        ss sl = q.mm.front();
        q.mm.pop_front();
        cout << "(" << sl.x << ", " << sl.y << ")" << endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/7750-13/p/7470036.html