2013 HIT winter training FINAL contest H题 三维矩阵水题

H

My Tags  (Edit)
Cancel - Seperate tags with commas.
 Source : - Sealed -
 Time limit : 2 sec Memory limit : 32 M

Submitted : 77, Accepted : 31

Goddess is a super Xueba. He even keeps studying when she is sleeping.Days ago she made a very complicated dream, where she was told that the answers to the final exams hides somewhere in her dream and she had to find it out.
Let's assume that Goddess's dream is a two-dimensional labyrinth consisted of r rows and c columns. But because of Goddess's high IQ, she sometimes makes dreams in her dreams, which means there can be k levels of dreams and the answer always hides in her deepest dream. That drives Goddess crazy. Can you help her get the answers to the final exams as soon as possible?

Input

There are multiple testcases. Each starts with three integers k,r,c, 1<=k,r,c<=30 Then there will follow k blocks consisted of r lines each containing c characters. Each character describes one cell of Goddess's dream.
'.'means the cell is empty and you can step on it.
'#'means there are rocks and you cannot be there.
'S'means the initial position of you in the first level of dreams.
'E'means the destination you need to reach in the deepest dream.
Every step takes exactly 1 minute.
Moving to an adjacent dream(including the upper or lower dream) also takes 1 minute.
You need to find the fastest way to get the answer.
0 0 0 means the end of test cases.

Output

Got the answer in x minutes!
where x is replaced by the time you take to find the answer.
If you can't find a way to the destination, just print "Goddess will fail her final exams."

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Got the answer in 11 minutes!
Goddess will fail her final exams.
 
 
/*
题目:H题
http://acm.hit.edu.cn/hoj/problem/view?id=4000600
题目大意:给出k块r*c的矩阵,即一个三维矩阵 第一层矩阵中有一个start 最有一层有一个end,从start出发,走一格或跳到下一层矩阵对应位置需要1分钟,求到end最快多少分钟;
#为不可走 .为可走   开始位置为S 结束位置为E   

解题思路:BFS,从start开始(为0分钟)对当前位置的前后左右下5个方向判断是否满足条件(即是否为'.'),求时间直到end。
比以前做的那些2维的矩阵多考虑了一个方向而已
*/
#include<stdio.h>
#include<queue>
using namespace std;
char map[50][50][50];
int n,m,k,s_x,s_y,e_x,e_y;
struct haha
{
        int z;
        int x;
        int y;
        int val;
        friend bool operator<(struct haha a,struct haha b)
        {
                return a.val>b.val;
        }
}q,temp;
int step[5][3]={0,1,0, 0,-1,0, 0,0,1, 0,0,-1, 1,0,0};
void BFS()
{
        int i,xx,yy,zz,ans=999999999;
        priority_queue<haha>que;
        q.z=1;
        q.x=s_x;
        q.y=s_y;
        q.val=0;
        que.push(q);
        while(!que.empty())
        {
                temp=que.top();
                que.pop();
                if(temp.z==k&&temp.x==e_x&&temp.y==e_y)
                {
                        ans=temp.val;
                        printf("Got the answer in %d minutes!\n",ans);
                        break;
                }
                for(i=0;i<5;i++)
                {
                        zz=temp.z+step[i][0];
                        xx=temp.x+step[i][1];
                        yy=temp.y+step[i][2];
                        
                        if(zz>=1&&zz<=k&&xx>=1&&xx<=n&&yy>=1&&yy<=m&&map[zz][xx][yy]!='#')
                        {
                                q.x=xx;
                                q.y=yy;
                                q.z=zz;
                                q.val=temp.val+1;
                                map[zz][xx][yy]='#';
                                que.push(q);
                        }
                }   
        }
        if(ans==999999999)  printf("Goddess will fail her final exams.\n");
}

int main()
{
        int i,j,t;
        while(scanf("%d %d %d",&k,&n,&m)!=EOF)
        {
                if(k==0&&n==0&&m==0) break;
                for(t=1;t<=k;t++)
                {
                        for(i=1;i<=n;i++)
                        {
                                        scanf("%s",map[t][i]+1);
                                        for(j=1;j<=m;j++)
                                        {
                                        if(map[t][i][j]=='S') {s_x=i;s_y=j;}
                                            if(map[t][i][j]=='E') {e_x=i;e_y=j;}
                                        }
                        }   
                }
                BFS();
        }
        return 0;
}


 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值