胜利大逃亡

题目描述:
Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.
输入:
输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙。
输出:
对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.
样例输入:
1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0 
样例输出:
11
 1 #include <cstdio>
 2 #include <queue>
 3 using namespace std;
 4 
 5 struct Edge {
 6         int a, b, c , time;
 7         void init( int x, int y , int z, int cost){
 8               a = x;
 9               b = y;
10               c = z;
11               time = cost;
12         }
13 };
14 
15 queue<Edge > Q;
16 int buf[ 50][50 ][50];
17 bool mark[ 50][50 ][50];
18 int go[ 6][3 ] = { { - 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 }, { 1 , 0 , 0 }, { 0 , 1 ,
19                0 }, { 0, 0, 1 } };
20 
21 int bfs( int a, int b , int c) {
22        Edge tmp ;
23        Edge e ;
24         int x, y, z ;
25        tmp .init( 0, 0, 0, 0);
26        Q .push( tmp);
27         while (! Q.empty ()) {
28               tmp = Q. front();
29               Q .pop();
30                if(tmp .a == a - 1 && tmp.b == b - 1 && tmp. c == c - 1 )
31                       return tmp. time;
32                for ( int i = 0; i < 6; i ++) {
33                      x = tmp. a + go [i][ 0];
34                      y = tmp. b + go [i][ 1];
35                      z = tmp. c + go [i][ 2];
36                       if ( x < 0 || x >= a || y < 0 || y >= b || z < 0 || z >= c
37                                    || buf[ x][y ][z] || mark [x][ y][z ])
38                             continue;
39                      e .init( x, y , z, tmp.time + 1 );
40                      mark [x][ y][z ] = true;
41                      Q .push( e);
42                }
43         }
44         return - 1;
45 }
46 
47 int main() {
48         int K;
49         int A, B, C , T;
50         while (~ scanf("%d" , & K)) {
51                while ( K--) {
52                       while(!Q .empty())
53                            Q .pop();
54                      scanf ("%d%d%d%d", &A , & B, &C, &T );
55                       for ( int i = 0; i < A ; i++)
56                             for ( int j = 0; j < B ; j++)
57                                    for ( int k = 0; k < C ; k++)
58                                          mark [i][ j][k ] = false;
59                       for ( int i = 0; i < A ; i++)
60                             for ( int j = 0; j < B ; j++)
61                                    for ( int k = 0; k < C ; k++){
62                                          scanf ("%d", &buf [i][ j][k ]);
63                                    }
64                int time = bfs(A , B, C);
65                if(time <= T)
66                      printf ("%d\n", time);
67                else
68                      printf ("-1\n");
69                }
70         }
71         return 0 ;
72 }
73   

 

posted @ 2013-02-20 15:43  babyron  阅读(232)  评论(0编辑  收藏  举报