深搜和广搜

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 58498 Accepted: 21525

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 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!

Source

Ulm Local 1997

深搜只是确定它是否存在;(深搜时间复杂度远远大于广搜,所以一般找到结果之后都会选择直接退出)

广搜才是确定它的最短路径;

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int vis[36][36][36];
int ans;
char tu[36][36][36];
int l,r,c;
int flag;
int cnt;
int zhong;
int movee[19]= {0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1, 1,0,0, -1,0,0 };
//1 (1)   4(2)     7(3)    10(4)    13(5)    16(6)

void in(int high,int x,int y)
{
    if(tu[high][x][y]=='E' && cnt < zhong)
    {
        zhong=cnt;
        ans=cnt;
        return;
    }
    else
    {
        for(int i=1; i<=16; i=i+3)
        {

            high=high+movee[i];
            x=x+movee[i+1];
            y=y+movee[i+2];

            if( ( tu[high][x][y]=='.' || tu[high][x][y]=='E' ) && vis[high][x][y]==0 && high>=1 && high<=l && x>=1 && x<=r &&y>=1 &&y<=c )
            {

                vis[high][x][y]=1;
                cnt++;

                in(high,x,y);

                cnt--;
                vis[high][x][y]=0;
            }
            high=high-movee[i];
            x=x-movee[i+1];
            y=y-movee[i+2];

        }
    }
    return;
}


int main()
{

    while((cin>>l>>r>>c) && l && r &&c)
    {
        memset(vis,0,sizeof(vis));
        cnt=0;
        int high,x,y;
        int high_end,x_end,y_end;
        ans=0;
        zhong=0x3f3f3f;

        for(int i=1; i<=l; i++)
        {
            for(int j=1; j<=r; j++)
            {
                for(int k=1; k<=c; k++)
                {
                    cin>>tu[i][j][k];

                    if( tu[i][j][k]=='S' )
                    {
                        high=i;
                        x=j;
                        y=k;
                        //cout<<i<<j<<k<<endl;
                    }
                    if( tu[i][j][k]=='E' )
                    {
                        high_end=i;
                        x_end=j;
                        y_end=k;
                    }
                }

            }

        }
        vis[high][x][y]=1;
        in(high,x,y);
        if(ans)
        {
           printf("Escaped in %d minute(s).\n",ans);
        }
        else
        {
           printf("Trapped!\n");
        }


    }


}

 

#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
char map[30][30][30];        //记录节点信息
int vis[30][30][30];        //标记是否访问

int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };

int L, R, C;

typedef struct
{
    int x, y, z;            //位置坐标
    int step;                //出发点到该点的步数
} node;

node s;                //起点
node e;                //终点
node curp;             //跳出循环时的节点


bool success(node cur)
{
    if (cur.x == e.x  &&  cur.y == e.y && cur.z == e.z)
        return true;
    else
        return false;
}


bool check(int x, int y, int z)
{
    if ( (x >= 0) && (x < L) && (y >= 0) && (y < R) && (z >= 0) && (z < C) && (!vis[x][y][z]) && (map[x][y][z] == '.' || map[x][y][z] == 'E'))
        return true;
    else
        return false;
}


void bfs()
{

    queue<node> q;
    q.push(s);

    while (!q.empty())
    {
        curp = q.front();
        q.pop();

        if ( success(curp) )
            return;

        else
        {
            vis[curp.x][curp.y][curp.z] = 1;

            node next;
            for (int i = 0; i < 6; i++)
            {
                next.x = curp.x + base[i][0];
                next.y = curp.y + base[i][1];
                next.z = curp.z + base[i][2];

                if ( check(next.x, next.y, next.z) )        //扩展队列
                {
                    next.step = curp.step + 1;
                    vis[next.x][next.y][next.z] = 1;

                    q.push(next);

                }
            }
        }
    }
}
int main()
{
    while (scanf("%d%d%d", &L, &R, &C))
    {
        if((L == 0) && (R == 0) && (C == 0))
            break;

        memset(vis, 0, sizeof(vis));

        for (int i = 0; i < L; i++)
        {
            for (int j = 0; j < R; j++)
            {
                for (int k = 0; k < C; k++)
                {
                    cin>>map[i][j][k];
                    if (map[i][j][k] == 'S')
                    {
                        s.x = i;
                        s.y = j;
                        s.z = k;
                        s.step = 0;
                    }
                    else if (map[i][j][k] == 'E')
                    {
                        e.x = i;
                        e.y = j;
                        e.z = k;
                    }
                }

            }
        }

        bfs();
        if ( curp.x == e.x && curp.y == e.y && curp.z == e.z )
            printf("Escaped in %d minute(s).\n", curp.step);
        else
            printf("Trapped!\n");
    }
    return 0;
}

 

深度优先索(DFS)和广度优先索(BFS)是两种常用的图索算法。 深度优先索是一种先从起点出发,沿着一条路径尽可能深入索直到无法继续才回溯的方法。在DFS中,我们会优先访问新的未访问过的顶点,直到无法再前进为止,然后回溯到上一个顶点,继续探索其他路径。这个过程可以使用递归实现,也可以使用栈来存储未访问顶点。 广度优先索则是一种先寻找离起点最近的顶点,继而寻找离起点更远但是可以通过更少的边连接的顶点的方法。在BFS中,我们会首先访问起点,然后依次访问起点的邻居顶点,再访问邻居的邻居顶点,以此类推。这个过程可以使用队列来实现。 在Goland这个问题中,我们可以将Goland中的字符作为图的顶点,G和o之间的连接关系作为边,然后使用DFS或BFS来索是否存在一条从G到o到l到a到n到d的路径。 如果使用DFS,我们从G开始,依次访问G的邻居o,然后再访问o的邻居l,以此类推,直到找到字符d或者无法继续索。如果找到了字符d,那么存在一条从G到d的路径。 如果使用BFS,我们首先访问起点G,然后依次访问G的邻居o和G的邻居l,以此类推,直到找到字符d或者无法继续索。如果找到了字符d,那么存在一条从G到d的路径。 综上所述,DFS和BFS都可以用于索Goland中的字符路径,具体使用哪个算法取决于问题的要求和实际情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值