POJ 2251 Dungeon Master
2016-10-13 20:51
218 查看
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27449 Accepted: 10750
Description
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!
代码:
很简单的三维bfs模板题,我却非要用二维,mdzz
写完后看网上全是用的三维数组,md真简单
我用的二维数组,上下层之间加了一行#
出了两个脑残错误,感觉自己宛如一个智障==
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27449 Accepted: 10750
Description
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!
代码:
#include<cstdio> #include<iostream> #include<cstring> using namespace std; char map[2000][32]; int visit[2000][32]; struct node { int x; int y; int step; }que[60000]; int main() { int l,r,c; char x; while(scanf("%d%d%d%c",&l,&r,&c,&x)!=EOF) { int next[6][2]= { {1,0}, {-1,0}, {0,1}, {0,-1}, {r+1,0}, {-1*r-1,0} }; memset(visit,0,sizeof(visit)); int sx,sy,ex,ey; if(l==0&&r==0&&c==0) break; int m=0; for(int i=0;i<l*r+(l-1);i++) { m++; for(int j=0;j<c;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='S') { sx=i; sy=j; } if(map[i][j]=='E') { ex=i; ey=j; } } scanf("%c",&x); if(m%r==0&&i!=l*r+(l-1)-1) { scanf("%c",&x); i++; for(int k=0;k<c;k++) { map[i][k]='#'; } } } /*for(int i=0;i<l*r+(l-1);i++) { for(int j=0;j<c;j++) { printf("%c",map[i][j]); } printf("\n"); }*/ //printf("%d %d %d %d\n",sx,sy,ex,ey); //printf("%d %d %d\n",l,r,c); int head=1; int tail=1; que[tail].x=sx; que[tail].y=sy; que[tail].step=0; visit[sx][sy]=1; tail++; int tx; int ty; int flag=0; int flag2=0; while(head<tail) { for(int i=0;i<6;i++) { tx=que[head].x+next[i][0]; ty=que[head].y+next[i][1]; if(tx<0||tx>=l*r+(l-1)||ty<0||ty>=c) continue; if(map[tx][ty]!='#'&&visit[tx][ty]==0) { visit[tx][ty]=1; que[tail].x=tx; que[tail].y=ty; que[tail].step=que[head].step+1; //printf("%d %d\n",tx,ty); tail++; } if(tx==ex&&ty==ey) { //printf("2222\n"); flag=1; flag2=1; break; } } if(flag==1) break; head++; } if(flag2==1) printf("Escaped in %d minute(s).\n",que[tail-1].step); else printf("Trapped!\n"); } return 0; }
很简单的三维bfs模板题,我却非要用二维,mdzz
写完后看网上全是用的三维数组,md真简单
我用的二维数组,上下层之间加了一行#
出了两个脑残错误,感觉自己宛如一个智障==
相关文章推荐
- POJ2251—— BFS——Dungeon Master
- poj 【2251】 Dungeon Master
- POJ 2251 Dungeon Master
- POJ 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master :NYOJ 353 3D dungeon (三维空间上的搜索 bfs )
- POJ 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251, Dungeon Master
- 【POJ】-2251-Dungeon Master(BFS+队列)
- poj-2251-Dungeon Master【BFS】
- POJ_2251 Dungeon Master
- poj 2251 Dungeon Master
- poj2251-dungeon master(bfs广搜)
- poj 2251 Dungeon Master
- POJ 2251 Dungeon Master 笔记
- POJ2251 Dungeon Master 解题报告
- POJ 2251 Dungeon Master (三维迷宫 BFS)
- POJ 2251 Dungeon Master(BFS最短路)
- POJ 2251.Dungeon Master