ZOJ 1940 Dungeon Master (三维广搜)

Dungeon Master

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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!


比赛时一看像搜索题,我就直接把这道题跳过去做其他题了。比赛完听队友说其实这道题很简单,就是一个简单地广搜。但是因为之前我没怎么学过搜索,所以并不后悔。只是我该好好学习搜索了。

题意:一个三维的地牢,给你起点和终点,问从起点到终点至少需要多长时间,每移动一次耗时1minute;如果不能到达终点,输出“Trapped!“。因为是三维的,所以从 每个点开始搜索时都会有6个方向,设置6个方向向量,然后每搜到一个点就判断是不是终点。利用队列即可。
下面是我的AC代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;

int dx[6] = {0,0,-1,1,0,0};
int dy[6] = {0,0,0,0,1,-1};
int dz[6] = {1,-1,0,0,0,0};

char Map[40][40][40];
int vis[40][40][40], L, R, C;

struct node
{
    int x, y, z;
    int time;
}st, ed;
queue<node> q;

bool check(int x, int y, int z) 
{
    if(x >= 1 && x <= L && y >= 1 && y <= R && z >= 1 && z <= C)
        return true;
    return false;
}

int BFS()
{
    int x, y, z, t, i;
    while(!q.empty())
    {
        node tmp = q.front();
        q.pop();
        x = tmp.x;
        y = tmp.y;
        z = tmp.z;
        t = tmp.time;
        for(i = 0; i < 6; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            int nz = z + dz[i];
            if(!vis[nx][ny][nz] && Map[nx][ny][nz] != '#' && check(nx,ny,nz))
            {
                if(nx == ed.x && ny == ed.y && nz == ed.z)
                    return t+1;
                vis[nx][ny][nz] = 1;
                node temp;
                temp.x = nx;
                temp.y = ny;
                temp.z = nz;
                temp.time = t + 1;
                q.push(temp);
            }
        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d%d%d",&L, &R, &C) && (L + R + C))
    {
        memset(vis,0,sizeof(vis));
        int i, j, k;
        for(i = 1; i <= L; i++)
        {
            for(j = 1; j <= R; j++)
            {
                for(k = 1; k <= C; k++)
                {

                    cin>>Map[i][j][k];
                    if(Map[i][j][k] == 'S')
                    {
                        st.x = i, st.y = j, st.z = k;st.time = 0;
                        q.push(st);
                         vis[i][j][k] = 1;
                    }
                    else if(Map[i][j][k] == 'E')
                        ed.x = i, ed.y = j, ed.z = k;
                }
            }
        }
        int ans = BFS();
        if(ans == -1)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",ans);
        while(!q.empty())  q.pop();
    }
    return 0;
}

后来队友和我说了他的做法:不必判断搜到的点是否越界,只需先预处理一下,把可以经过的点设为1,不能经过的点即是墙的点设为0,然后存地图的时候从第1行,第1列,第1层开始存,这样边界都是0,即都是墙。搜索时就不用判断是否越界了。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;

int dx[6] = {0,0,0,0,1,-1};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {1,-1,0,0,0,0};

int vis[40][40][40], Map[40][40][40];
struct node
{
    int x, y, z, step;
}st, ed;
queue<node> Q;

bool judge(int x, int y, int z) //判断当前位置是否可以访问
{
    if(!Map[x][y][z] || vis[x][y][z])
        return false;
    return true;
}

int BFS()
{
    int x, y, z, t, i;
    while(!Q.empty())
    {
        node tmp = Q.front();
        Q.pop();
        x = tmp.x;
        y = tmp.y;
        z = tmp.z;
        t = tmp.step;
        for(i = 0; i < 6; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            int nz = z + dz[i];
            if(judge(nx,ny,nz))
            {
                 if(nx == ed.x && ny == ed.y && nz == ed.z)
                    return t + 1;
                 vis[nx][ny][nz] = 1;
                 node temp;
                 temp.x = nx;
                 temp.y = ny;
                 temp.z = nz;
                 temp.step = t + 1;
                 Q.push(temp);
            }
        }
    }
    return -1;
}

int main()
{
    int L, R, C, i, j, k;
    char ch;
    while(~scanf("%d%d%d",&L, &R, &C) && (L + R + C))
    {
        while(!Q.empty())  Q.pop();
        memset(vis,0,sizeof(vis));
        memset(Map,0,sizeof(Map));
        for(i = 1; i <= L; i++)
            for(j = 1; j <= R; j++)
                for(k = 1; k <= C; k++)
                {
                    cin >> ch;
                    if(ch == '.')
                        Map[j][k][i] = 1;
                    else if(ch == 'S')
                    {
                        st.x = j;
                        st.y = k;
                        st.z = i;
                        st.step = 0;
                        vis[j][k][i] = 1;
                        Map[j][k][i] = 1;
                        Q.push(st);
                    }
                    else if(ch == 'E')
                    {
                        ed.x = j;
                        ed.y = k;
                        ed.z = i;
                        Map[j][k][i] = 1;
                    }
                }
        int ans = BFS();
        if(ans == -1)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题是一个典型的搜索问题,可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来解决。以下是使用DFS的代码实现: ```c++ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 20; const int MAXM = 20; int n, m, sx, sy, ex, ey; char maze[MAXN][MAXM]; // 迷宫 int vis[MAXN][MAXM]; // 标记数组 int dx[] = {0, 0, 1, -1}; // 方向数组 int dy[] = {1, -1, 0, 0}; void dfs(int x, int y) { if (x == ex && y == ey) { // 到达终点 printf("(%d,%d)", x, y); return; } for (int i = 0; i < 4; i++) { // 依次尝试四个方向 int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != '#' && !vis[nx][ny]) { vis[nx][ny] = 1; // 标记已访问 printf("(%d,%d)->", x, y); dfs(nx, ny); return; } } } int main() { while (scanf("%d%d", &n, &m) == 2) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%s", maze[i]); for (int j = 0; j < m; j++) { if (maze[i][j] == 'S') { sx = i; sy = j; } else if (maze[i][j] == 'T') { ex = i; ey = j; } } } vis[sx][sy] = 1; dfs(sx, sy); printf("\n"); } return 0; } ``` 代码实现中,使用了一个标记数组 `vis` 来标记每个位置是否已经被访问过,避免走重复的路线。使用DFS的时候,每次从当前位置依次尝试四个方向,如果某个方向可以走,则标记该位置已经被访问过,并输出当前位置的坐标,然后递归进入下一个位置。如果当前位置是终点,则直接输出并返回。 在输出路径的时候,由于是递归调用,所以输出的路径是反向的,需要将其反转过来,即从终点往起点遍历输出。 需要注意的是,题目中要求输出的路径是 `(x1,y1)->(x2,y2)->...->(xn,yn)` 的形式,每个坐标之间用 `->` 连接。所以在输出的时候需要特别处理第一个坐标和最后一个坐标的格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值