poj 2251Dungeon Master+hdu 1253 胜利大逃亡(bfs)

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18875 Accepted: 7324

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<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int a,b,c,t,vis[33][33][33],sx,sy,sz,ex,ey,ez;
char map[33][33][33];
struct node{
    int x,y,z,step;
};
int tx[]={1,-1,0,0,0,0};
int ty[]={0,0,1,-1,0,0};
int tz[]={0,0,0,0,1,-1};
int bfs(int x,int y,int z)
{
    int i;
    queue<node>Q;
    node p,q;
    p.x=x;
    p.y=y;
    p.z=z;
    p.step=0;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(p.x==ex&&p.y==ey&&p.z==ez)return p.step;
        for(i=0;i<6;i++)
        {
            q=p;
            q.x=p.x+tx[i];
            q.y=p.y+ty[i];
            q.z=p.z+tz[i];
            if(q.x<=0||q.y<=0||q.z<=0||q.x>a||q.y>b||q.z>c)continue;
            if(!vis[q.x][q.y][q.z]&&map[q.x][q.y][q.z]!='#')
            {
                q.step++;
                vis[q.x][q.y][q.z]=1;
                Q.push(q);
            }
        }
    }
    return -1;
}
int main()
{
    int i,j,k;
       while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        if(a==0&&b==0&&c==0)break;
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
      for(i=1;i<=a;i++)
      for(j=1;j<=b;j++)
      for(k=1;k<=c;k++)
      {
          cin>>map[i][j][k];
          if(map[i][j][k]=='S'){
              sx=i;sy=j;sz=k;
          }
          if(map[i][j][k]=='E'){
              ex=i;ey=j;ez=k;
          }
      }     

      int ans;
      ans=bfs(sx,sy,sz);
        if(ans==-1)printf("Trapped!\n");
        else {
        if(ans==1)printf("Escaped in %d minute.\n",ans);
        else
        printf("Escaped in %d minute(s).\n",ans);
       }
       }    
       return 0;
}


胜利大逃亡

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28565    Accepted Submission(s): 10815


Problem Description
Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.

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


 

Input
输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1&lt;=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample Input中的迷宫描述,它表示的就是上图中的迷宫)

特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.
 

Output
对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.
 

Sample Input
   
   
1 3 3 4 20 0 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 0 0 0 0 1 1 0 0 1 1 0
 

Sample Output
   
   
11
 

Author
Ignatius.L


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<queue>
using namespace std;
int map[55][55][55],vis[55][55][55];
int a,b,c,T;
struct node{
    int x,y,z,step;
};
int tx[]={1,-1,0,0,0,0};
int ty[]={0,0,1,-1,0,0};
int tz[]={0,0,0,0,1,-1};
int bfs(int x,int y,int z)
{    
    queue<node>Q;
    node p,q;
    int i,j;
    p.x=x;
    p.y=y;
    p.z=z;

    p.step=0;
    Q.push(p);
    vis[x][y][z]=1;
    while(!Q.empty())
    {
    
        p=Q.front();
        Q.pop();
    
        if(p.x==a-1&&p.y==b-1&&p.z==c-1&&p.step<=T)return p.step;
        if(p.step>T)return -1;
        for(i=0;i<6;i++)
        {
            q=p;
            q.x=p.x+tx[i];
            q.y=p.y+ty[i];
            q.z=p.z+tz[i];
            if(q.x<0||q.y<0||q.z<0||q.x>=a||q.y>=b||q.z>=c)continue;
            if(!vis[q.x][q.y][q.z]&&map[q.x][q.y][q.z]==0)
            {q.step++;
                vis[q.x][q.y][q.z]=1;
                if(abs(q.x-a+1)+abs(q.y-b+1)+abs(q.z-c+1)>T)continue;
            Q.push(q);
            }
        }
    }
    return -1;
}

int main()
{
    int K,i,j,k;
    scanf("%d",&K);
    while(K--)
    {
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        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",&map[i][j][k]);
        int ans=bfs(0,0,0);
        printf("%d\n",ans);
        
    }
}

两题都是一个类型,三维的Bfs,说实话以前从来没有用过三维数组。无非就是多加了上下两个方向来进行搜索。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值