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

首頁 > 學院 > 開發設計 > 正文

Dungeon Master [bfs]

2019-11-11 04:33:06
字體:
來源:轉載
供稿:網友

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 峨眉山市| 南昌市| 安丘市| 石河子市| 饶平县| 镇江市| 华池县| 沈阳市| 微山县| 图片| 柳江县| 拉萨市| 安顺市| 绵阳市| 宁远县| 依安县| 商南县| 南昌市| 永胜县| 沈阳市| 洞口县| 井冈山市| 大化| 通化市| 湘潭县| 马龙县| 襄城县| 壤塘县| 华坪县| 三门峡市| 临夏县| 望城县| 屏南县| 漠河县| 五台县| 嘉鱼县| 衡阳县| 中牟县| 贵定县| 四川省| 家居|