pku 2251 Dungeon Master

http://poj.org/problem?id=2251 题目就是给你一个三维的矩阵,让你从‘S’ 位置找到 ‘E’ 所走的步数,其实就是bfs类型的水题,只不过三维的可能感觉起来有点麻烦。

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<queue>
5 using namespace std;
6 #define inf 9999999
7 #define N 35
8 int move[6][3] = { {0,0,-1},{0,0,1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
9 char map[N][N][N];
10 int l,r,c;
11 struct node
12 {
13 int x,y,z;
14 int step;
15 };
16 node s;
17 int sum;
18 bool juge(int x,int y,int z)
19 {
20 if(x < l && x >= 0 && y < r && y >= 0 && z < c && z >= 0 && map[x][y][z] != '#')
21 return true;
22 return false;
23 }
24 void bfs()
25 {
26 queue<node>qu;
27 node t,k;
28 int i;
29 qu.push(s);
30 while(!qu.empty())
31 {
32 t=qu.front();
33 qu.pop();
34 for(i = 0; i < 6; i++)
35 {
36 k.x = t.x + move[i][0];
37 k.y = t.y + move[i][1];
38 k.z = t.z + move[i][2];
39 k.step = t.step + 1;
40 if(map[k.x][k.y][k.z] == 'E') {sum = k.step ;break;}
41 if(juge(k.x,k.y,k.z))
42 {
43 qu.push(k);
44 map[k.x][k.y][k.z] = '#';
45 }
46 }
47 }
48 }
49 int main()
50 {
51 int i,j,k;
52 while(cin>>l>>r>>c,l+r+c)
53 {
54 for(i = 0; i < l; i++)
55 {
56 for(j = 0; j < r; j++)
57 {
58 for(k = 0; k < c; k++)
59 {
60 cin>>map[i][j][k];
61 if(map[i][j][k] == 'S')
62 {s.x = i;s.y = j;s.z = k;s.step = 0;}
63 }
64 }
65 }
66
67 sum = inf;
68 map[s.x][s.y][s.z] = '#';
69 bfs();
70 if(sum == inf) cout<<"Trapped!\n";
71 else printf("Escaped in %d minute(s).\n",sum);
72 }
73 return 0;
74 }

 

posted @ 2012-03-20 21:29  AC_Girl  阅读(192)  评论(0编辑  收藏  举报