程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ZOJ 1940 Dungeon Master (三維廣搜)

ZOJ 1940 Dungeon Master (三維廣搜)

編輯:C++入門知識

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
#include
#include
#include
#include
#include
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 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
#include
#include
#include
#include
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 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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved