BFS

BFS实际上是用来解决最短路问题以及分支较少的问题的。

因为BFS是用队列的模型来做的,所以说当满足一定条件之后,就可以直接弹出去,这样就可以找到最短的那条路,至于长的路,他们还没有走到尽头,走的比较慢,而短路走的快,是第一个满足条件的,所以就直接出去了。

POJ-2251题目

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 cannot move diagonally and the maze is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line 
Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!
#include <cstdio>
#include <cstring>
#include <queue>

char map[35][35][35];
int vis[35][35][35];
int k,n,m,sx,sy,sz,ex,ey,ez;
int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
 
struct node
{
    int x,y,z,step;
};
 
int check(int x,int y,int z)
{
    if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m)//越界判断 
        return 1;
    else if(map[x][y][z] == '#')
        return 1;
    else if(vis[x][y][z])
        return 1;
    return 0;
}
 
int bfs()
{
    int i;
    node a,next;
    std::queue<node> Q;
    a.x = sx,a.y = sy,a.z = sz;
    a.step = 0;
    vis[sx][sy][sz] = 1;
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if(a.x == ex && a.y == ey && a.z == ez)
            return a.step;
        for(i = 0; i<6; i++)
        {
            next = a;
            next.x = a.x+to[i][0];
            next.y = a.y+to[i][1];
            next.z = a.z+to[i][2];
            if(check(next.x,next.y,next.z))
                continue;
            vis[next.x][next.y][next.z] = 1;
            next.step = a.step+1;
            Q.push(next);
        }//没有一步可走就被删除掉 
    }
    return 0;
}
 
int main()
{
    int i,j,r;
    while(scanf("%d%d%d",&k,&n,&m),n+m+k)
    {
        for(i = 0; i<k; i++)
        {
            for(j = 0; j<n; j++)
            {
                scanf("%s",&map[i][j]);
                for(r = 0; r<m; r++)
                {
                    if(map[i][j][r] == 'S')
                    {
                        sx = i,sy = j,sz = r;
                    }
                    else if(map[i][j][r] == 'E')
                    {
                        ex = i,ey = j,ez = r;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));
        int ans;
        ans = bfs();
        if(ans)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
 
    return 0;
}

这相当于一个3D的地牢,然后要你去寻找出路,关键在于无论怎么走,都只有六个方向,所以就可以确定是BFS的问题了。

这种题用BFS来写很简单,首先是一个数组,用来存放每次走的坐标,然后是一个结构体,记录当时的坐标以及走过的步数。

然后是一个check函数,这个可以写在里面或者外面都是可以的,但是越界判断,墙体判断以及走过的路数判断最好写成if else语句,

不然对于新手很容易写错。

然后就是BFS函数了,定义结构体变量,一个是当前的,一个是下一个的。对于当前的位置vis数组记录为真,就是visit,经过的意思。

定义过结构体类型的队列之后,再接着初始化,然后就进入while循环,首先判断队列是否为空,队首元素赋值给当前的结构体变量,

弹出顶端元素,结束条件的判断。

然后就是对每次要走的方向进行一次遍历,看看有哪些路是可以走的,当然首先是要满足条件的。然后就是对于当前位置的vis赋值为真。

然后压入这个下一步的数据。这样的话一次走路就完成了。

其实这就像是做贡献一样,走你这一步你可以贡献一个next,也可以不贡献,不贡献也就相当于没生儿子,没生儿子也就相当于这一路

就被删除了。如果有贡献的话,贡献几个就压入队列几个,然后依次向下寻找出口,就这样。

猜你喜欢

转载自www.cnblogs.com/xyqxyq/p/9338829.html
BFS