想复习复习之前做过的题,不整理出来发现非常麻烦,感谢章鱼鱼小盆友的启发。。。
这道题和robot maze好像挺像,当时觉得robot maze好难还没做,这次做这个就理解了里面对方向的处理。
用scanf输入出了很多问题,就改成cin了。。。
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 Specification
The input file 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 Specification
Each maze generates one line of output. If it is possible to reach the exit, print a line of the formEscaped 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 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
#include<stdio.h>
#include<iostream>#include<queue>
#include<string.h>
using namespace std;
struct node{
int x, y, h, step; //坐标 and step
//int d;//direction
//char road; //地形
}direc[6];
char cell[40][40][40]; //l, r, c
//node direc[6];
int visited[40][40][40];//x-y-h
int minute, c, r, l;
void bfs(node st)
{
queue<node> q;
int i, nx, ny, nh, flag = 0, j, k;
node top, temp;
for(i = 0; i < l; i ++)
for(j = 0; j < r; j ++)
for(k = 0; k < c; k ++)
visited[i][j][k] = 0;
visited[st.h][st.x][st.y] = 1;
st.step = 1;
q.push(st); minute = 0;
while(!q.empty())
{
top = q.front();
q.pop();
for(i = 0; i < 6; i ++) //traverse each of the direction
{ //in this loop top cannot be modified
nx = top.x + direc[i].x;
ny = direc[i].y + top.y;
nh = top.h + direc[i].h; //top.d = i;
if(cell[nh][nx][ny] != '#' && !visited[nh][nx][ny] && nx<r&&nx>=0&&ny<c&&ny>=0&&nh<l&&nh>=0)//within dungeon
{
// printf("%d %d %d\n", nh, nx, ny);
// printf("%d %d %d\n", top.h, top.x, top.y);
if(cell[nh][nx][ny] == 'E') //exit
{
minute = top.step;
flag = 1;
break; //need to break from the outer "while"
}
else if(cell[nh][nx][ny] == '.')//continue traverse
{
// printf("hi1\n");
// printf("%d %d %d\n", nh, nx, ny);
visited[nh][nx][ny] = 1;
temp.step = top.step + 1;
temp.x = nx, temp.y = ny, temp.h = nh;
q.push(temp);
}
}
}
if(flag == 1)
break;
}
if(flag == 1)
printf("Escaped in %d minute(s).\n", minute);
else
printf("Trapped!\n");
}
int main()
{
direc[0].x = 0; direc[0].y = 1; direc[0].h = 0;//e
direc[1].x = 1; direc[1].y = 0; direc[1].h = 0;//s
direc[2].x = 0; direc[2].y = -1; direc[2].h = 0;//w
direc[3].x = -1; direc[3].y = 0; direc[3].h = 0;//n
direc[4].x = 0; direc[4].y = 0; direc[4].h = 1;//u
direc[5].x = 0; direc[5].y = 0; direc[5].h = -1;//d
int i, j, k;
node st, exit;
while(~scanf("%d%d%d", &l, &r, &c), l, r, c)
{
for(i = 0; i < l; i ++)
{
for(j = 0; j < r; j ++)
{
for(k = 0; k < c; k ++)
{
//scanf("%c", &cell[i][j][k]);
cin>>cell[i][j][k];
if(cell[i][j][k] == 'S')
{
//st.road = cell[i][j][k];
st.h = i, st.x = j, st.y = k;
}
}
//getchar();
}
// getchar();
} //input finish
bfs(st);
}
return 0;
}