POJ2251Dungeon Master&&nyoj523亡命逃窜

来源:互联网 发布:网络回合制游戏 编辑:程序博客网 时间:2024/05/16 06:36
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16977 Accepted: 6612

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!
题意:这里是个三维空间的广搜,意思是你被困在一个牢房里判断是否能逃脱,这里的广搜没有用到标记数组,直接在地图上面改的
#include <iostream>#include <cstdio>#include <queue>using namespace std;struct node{    int x, y, z, step;};int l, r, c, sx, sy, sz, ex,ey, ez;char maze[35][35][35];int di[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, -1, 0}, {0, 1, 0}, {1, 0, 0}, {-1, 0, 0}};int bfs(){    queue <node> q;    node no = {sx, sy, sz, 0};    q.push(no);    maze[no.x][no.y][no.z] = '#';    int i, current_x, current_y, current_z;    while(!q.empty())    {        no = q.front();        if(no.x == ex && no.y == ey && no.z == ez)        {            break ;        }        q.pop();        for(i = 0; i < 6; i++)        {            current_x = no.x + di[i][0];            current_y = no.y + di[i][1];            current_z = no.z + di[i][2];            if(current_x >= 0 && current_x < l && current_y >= 0 && current_y < r               && current_z >= 0 && current_z < c)            {                if(maze[current_x][current_y][current_z] == '.' ||                   maze[current_x][current_y][current_z] == 'E')                {                    node de = {current_x, current_y, current_z, no.step + 1};                    q.push(de);                    maze[current_x][current_y][current_z] = '#';                }            }        }    }    if(no.step > 0)        return no.step;    else        return -1;    return 0;}int main(){    int i, j, k, result;    while(~scanf("%d%d%d", &l, &r, &c) && l != 0 && r != 0 && c != 0)    {        result = 0;        for(i = 0; i < l; i++)        {           // getchar();            for(j = 0; j < r; j++)            {                //getchar();                scanf("%s", maze[i][j]);//这个输入还是极好的,避免了字符输入的麻烦                for(k = 0; k < c; k++)                {                    if(maze[i][j][k] == 'S')                    {                        sx = i;                        sy = j;                        sz = k;                    }                    if(maze[i][j][k] == 'E')                    {                        ex = i;                        ey = j;                        ez = k;                    }                }            }        }        result = bfs();        if(result == -1)        {            printf("Trapped!\n");        }        else        {            printf("Escaped in %d minute(s).\n", result);        }    }    return 0;}

亡命逃窜

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述

    从前有个叫hck的骑士,为了救我们美丽的公主,潜入魔王的老巢,够英雄吧。不过英雄不是这么好当的。这个可怜的娃被魔王抓住了,倍受折磨,生死一线。有一天魔王出去约会了,这可是一个千载难逢的逃命机会。你现在的任务就是判断一下这个英雄未遂的孩子能不能在魔王回来之前逃出魔王的城堡,成功逃生,最后迎娶我们美丽的公主。

    魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始hck被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,hck每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出hck能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.

如图所示,输入数据中的第0块的最左上角是hck被关的地方,第A-1块的最右下角是城堡的出口。按照图中红色箭头方向移动每一层以构成整个城堡

输入
输入数据的第一行是一个正整数K,表明测试数据的数量. 每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.
然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.
(如果对输入描述不清楚,可以参考上面的迷宫描述,它表示的就是上图中的迷宫)
输出
对于每组测试数据,如果hck能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.
样例输入
23 2 2 100 10 01 11 00 00 13 3 4 200 1 1 10 0 1 10 1 1 11 1 1 11 0 0 10 1 1 10 0 0 00 1 1 00 1 1 0
样例输出
-111
感觉这两个题目是一样的,所以就贴在一起了,都是三维空间的广搜,但是nyoj上的这个题目很久没过,是没有考虑可能在0~t时间里面走完了所有可以走的路却没有找到终点,在下面会提到改进和错误的方法
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;struct node{    int x, y, z, step;};int a, b, c, t;int maze[51][51][51], mark[51][51][51];int di[6][3] = {{0, 0, 1}, {0, 0, -1}, {-1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, -1, 0}};int bfs(){    node no = {0, 0, 0, 0};    queue <node> q;    q.push(no);    mark[no.x][no.y][no.z] = 1;    int i, current_x, current_y, current_z, flag = 0;    while(!q.empty())    {        no = q.front();        if(no.x == a - 1 && no.y == b - 1 && no.z == c - 1)        {flag = 1;//这里也可以不用flag,而是直接返回步数,然后在最后直接返回-1            break ;        }        q.pop();        for(i = 0; i < 6; i++)        {            current_x = no.x + di[i][0];            current_y = no.y + di[i][1];            current_z = no.z + di[i][2];            if(current_x >= 0 && current_x < a && current_y >= 0 && current_y < b               && current_z >= 0 && current_z < c)            {                if(mark[current_x][current_y][current_z] == 0 && maze[current_x][current_y][current_z] == 0)                {                    mark[current_x][current_y][current_z] = 1;                    node de = {current_x, current_y, current_z, no.step + 1};                    q.push(de);                }            }        }    }    if(no.step <= t && flag )//这里要么用flag判断要么直接返回-1,不要用no.step<=t&&no.step>0判断,会疏漏最开始说的那种情况        return no.step;    else        return -1;}int main(){    int n, i, j, k;    scanf("%d", &n);    while(n--)    {        scanf("%d%d%d%d", &a, &b, &c, &t);        for(i = 0; i < a; i++)        {            for(j = 0; j < b; j++)            {                for(k = 0; k < c; k++)                {                    scanf("%d", &maze[i][j][k]);                }            }        }        if(maze[a - 1][b - 1][c - 1] == 1 || a - 1 + b - 1 + c - 1 > t)//这里做了一种剪枝,如果是终点是墙和没有墙都是路时间大于t的情况直接就掏不出去了            printf("-1\n");        else            printf("%d\n", bfs());        memset(maze, 0, sizeof(maze));memset(mark, 0, sizeof(mark));//还有这些东西一定要清零啊,多么痛的领悟    }    return 0;}


0 0
原创粉丝点击