2013 HIT winter training FINAL contest H题 三维矩阵水题
H
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.3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0Sample 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;}