2013 HIT winter training FINAL contest H题 三维矩阵水题
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.
[cpp]
/*
题目: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;
补充:软件开发 , C++ ,
上一个:杭电1019
下一个:17_1_2递归求函数
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊