国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

Dungeon Master [bfs]

2019-11-11 05:06:42
字體:
供稿:網(wǎng)友

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!

解題報告

只要細心就是水題,bfs走一遍即可

#include<stdio.h>#include<string.h>#include<queue>#define MAX_N 32using namespace std;typedef pair<int,int> P;bool map[MAX_N][MAX_N][MAX_N];int best[MAX_N][MAX_N][MAX_N];int s_x,s_y,s_t,e_x,e_y,e_t,H,W,T;int ox[]={0,0,0,1,-1};int oy[]={0,1,-1,0,0};int ot[]={0,1,-1};void bfs(){ memset(best,0x3f,sizeof(best)); queue<P> que; que.push(make_pair(s_x+s_y*H,s_t)); best[s_t][s_x][s_y]=0; while(!que.empty()){ int X=que.front().first%H,Y=que.front().first/H,now=que.front().second;que.pop(); int step=best[now][X][Y]; if(X==e_x&&Y==e_y&&now==e_t){ printf("Escaped in %d minute(s)./n",step); return ; } for(int i=0;i<5;i++){ int x=ox[i]+X; int y=oy[i]+Y; if(0<=x&&x<H&&0<=y&&y<W&&map[now][x][y]){ for(int u=0;u<3;u++){ int el=ot[u]+now; int r=ot[u]&&i>0?1:0; if(T>el&&el>=0&&map[el][x][y]&&best[el][x][y]>step+1+r){ best[el][x][y]=step+1+r; que.push(make_pair(x+y*H,el)); } } } } } puts("Trapped!");}int main(){ char str[32]; while(~scanf("%d%d%d",&T,&H,&W)&&T&&H&&W){ for(int t=0;t<T;t++) for(int i=0;i<H;i++){ scanf("%s",str); for(int j=0;j<W;j++){ switch(str[j]){ case 'S':s_x=i,s_y=j,s_t=t;map[t][i][j]=true;break; case 'E':e_x=i,e_y=j,e_t=t;map[t][i][j]=true;break; case '.':map[t][i][j]=true;break; case '#':map[t][i][j]=false;break; } } } bfs(); } return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 军事| 永兴县| 射洪县| 大丰市| 卢氏县| 兖州市| 昭平县| 开封市| 启东市| 遂溪县| 昭通市| 松原市| 施甸县| 胶南市| 桂阳县| 通化市| 珲春市| 昆山市| 公安县| 图们市| 嘉黎县| 武宁县| 通山县| 塔城市| 山东| 正定县| 东兰县| 金堂县| 嫩江县| 荥阳市| 营山县| 浦县| 隆回县| 芒康县| 汨罗市| 保山市| 巴林右旗| 石门县| 鲁山县| 石首市| 楚雄市|