POJ2251 地下城主3维迷宫
题目内容
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10134 Accepted: 3908
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!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
解法:广度优先搜索,需要注意细节(提交3次才过,原因是check函数写丢了一个等号,一次很好的教训)。
自己写的是模拟队列,其实用STL也可。两篇代码全部贴出,第一篇有较详细的注释。
模拟队列,16MS
Queue Code
1 #include<cstdio>
2 #include<cstring>
3 using namespace std;
4 const int MAXN=30;
5 const short dir[][3]={
6 {1,0,0}, //floor up
7 {-1,0,0}, //floor down
8 {0,1,0}, //up
9 {0,-1,0}, //down
10 {0,0,-1}, //left
11 {0,0,1}}; //right
12 char cell[MAXN][MAXN][MAXN];
13 bool visited[MAXN][MAXN][MAXN];
14 long l,r,c;
15 struct loc
16 {
17 long f,x,y,step;//floor row column
18 loc()
19 {
20 x=0;
21 y=0;
22 f=0;
23 step=0;
24 }
25 }st,q[40000];
26 long q_head,q_end;
27 bool q_empty()
28 {
29 if(q_head==q_end)
30 return true;
31 return false;
32 }
33 void q_push(loc x)
34 {
35 q[q_end]=x;
36 q_end++;
37 if(q_end==40000)
38 q_end=0;
39 }
40 loc q_front()
41 {
42 loc x=q[q_head];
43 return x;
44 }
45 void q_pop()
46 {
47 q_head++;
48 if(q_head==40000)
49 q_head=0;
50 return;
51 }
52 void q_clear()
53 {
54 q_head=q_end=0;
55 }
56 bool check(loc a)
57 {
58 if(a.x<0||a.x>=r||a.y<0||a.y>=c||a.f<0||a.f>=l) //这个一定要搞清楚等号问题
59 return false;
60 if(cell[a.f][a.x][a.y]=='#') //不必要,检查当前点是否为石头
61 return false;
62 return true;
63 }
64 long search(loc sta)
65 {
66 q_push(sta); //起点入队
67 loc t,node;
68 while(!q_empty())
69 {
70 t=q_front();
71 q_pop();
72 for(long i=0;i!=6;i++) //按上层,下层,上,下,左,右六个方向产生当前位置结点
73 {
74 node.f=t.f+dir[i][0],node.x=t.x+dir[i][1],node.y=t.y+dir[i][2];
75 node.step=t.step+1; //走了一步自然要step+1
76 if(check(node)&&visited[node.f][node.x][node.y]==false) //check函数检查坐标合法性,visited若为false说明此点未访问过
77 {
78 if(cell[node.f][node.x][node.y]=='E')
79 return node.step; //抵达终点,函数返回
80 visited[node.f][node.x][node.y]=true; //标记已经访问过
81 q_push(node);
82 }
83 }
84 }
85 return -1;
86 }
87 void Init()
88 {
89 memset(cell,0,sizeof(cell));
90 memset(visited,false,sizeof(visited)); //标记所有点均未访问过
91 for(long i=0;i!=l;i++)
92 {
93 for(long j=0;j!=r;j++)
94 {
95 scanf("%s",cell[i][j]);
96 for(long k=0;k!=c;k++)
97 {
98 if(cell[i][j][k]=='S') //起点标记
99 {
100 st.x=j,st.y=k,st.f=i;
101 }
102 }
103 }
104 getchar();
105 }
106 q_clear();
107 visited[st.f][st.x][st.y]=true;
108 }
109 int main(void)
110 {
111 while(scanf("%ld%ld%ld",&l,&r,&c)==3)
112 {
113 if(l==0&&r==0&&c==0)
114 break;
115 Init();
116 long ans=search(st); //调用BFS函数search
117 if(ans>=0)
118 printf("Escaped in %ld minute(s).\n",ans);
119 else
120 printf("Trapped!\n");
121 }
122 return 0;
123 }
STL队列:0MS
STL Code
1 #include<cstdio>
2 #include<cstring>
3 #include<queue>
4 using namespace std;
5 const int MAXN=30;
6 const short dir[][3]={
7 {1,0,0}, //floor up
8 {-1,0,0}, //floor down
9 {0,1,0}, //up
10 {0,-1,0}, //down
11 {0,0,-1}, //left
12 {0,0,1}}; //right
13 char cell[MAXN][MAXN][MAXN];
14 bool visited[MAXN][MAXN][MAXN];
15 long l,r,c;
16 struct loc
17 {
18 long f,x,y,step;//floor row column
19 loc()
20 {
21 x=0;
22 y=0;
23 f=0;
24 step=0;
25 }
26 }st,end;
27 queue<loc>q;
28 bool check(loc a)
29 {
30 if(a.x<0||a.x>=r||a.y<0||a.y>=c||a.f<0||a.f>=l)
31 return false;
32 if(cell[a.f][a.x][a.y]=='#')
33 return false;
34 return true;
35 }
36 long search(loc sta)
37 {
38 q.push(sta);
39 loc t,node;
40 if(cell[sta.f][sta.x][sta.y]=='E')
41 return 0;
42 while(!q.empty())
43 {
44 t=q.front();
45 q.pop();
46 for(long i=0;i!=6;i++)
47 {
48 node.f=t.f+dir[i][0],node.x=t.x+dir[i][1],node.y=t.y+dir[i][2];
49 node.step=t.step+1;
50 if(check(node)&&visited[node.f][node.x][node.y]==false)
51 {
52 if(cell[node.f][node.x][node.y]=='E')
53 return node.step;
54 visited[node.f][node.x][node.y]=true;
55 q.push(node);
56 }
57 }
58 }
59 return -1;
60 }
61 void Init()
62 {
63 memset(cell,0,sizeof(cell));
64 memset(visited,false,sizeof(visited));
65 for(long i=0;i!=l;i++)
66 {
67 for(long j=0;j!=r;j++)
68 {
69 scanf("%s",cell[i][j]);
70 for(long k=0;k!=c;k++)
71 {
72 if(cell[i][j][k]=='S')
73 {
74 st.x=j,st.y=k,st.f=i;
75 }
76 }
77 }
78 getchar();
79 }
80 while(!q.empty())
81 q.pop();
82 visited[st.f][st.x][st.y]=true;
83 }
84 int main(void)
85 {
86 while(scanf("%ld%ld%ld",&l,&r,&c)==3)
87 {
88 if(l==0&&r==0&&c==0)
89 break;
90 Init();
91 long ans=search(st);
92 if(ans>=0)
93 printf("Escaped in %ld minute(s).\n",ans);
94 else
95 printf("Trapped!\n");
96 }
97 return 0;
98 }
posted on 2011-10-30 09:22 SilVeRyELF 阅读(172) 评论(0) 收藏 举报

浙公网安备 33010602011771号