hdu1253 ——三维地图搜索

Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 

魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1. 


Input输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample Input中的迷宫描述,它表示的就是上图中的迷宫) 

特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交. 
Output对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1. 
Sample Input
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
Sample Output
11



思路: 三维数组,数据量很大,不能用dfs,只能用bfs。其实还是套模板,只不过坐标换成了三个。这里要注意x,y,z代表的坐标含义一定要搞清楚,x代表层数,y,z代表一层里的二维矩阵。这里要枚举六个方向,上下左右前后,next[6][3] = {{1,0,0},{-1,0,0},{0,0,1},{0,1,0},{0,0,-1},{0,-1,0}}。

还有两个判断条件,如果不加则会超时:

1. 如果终点是1,则不用搜了,一定到不了

2. 不看墙的最短路径(即假设没有墙,全是0)=  (A-1)+(B-1)+(C-1)  如果>T,一定出不去



#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef struct zuobiao{
    int x,y,z;
    int s;
}zb;
int A,B,C,T;
int a[55][55][55];
int book[55][55][55];
queue<zb> q;
int flag = 1;
void clearq(){
    while(!q.empty()) q.pop();
}
void bfs(int x,int y,int z){
    book[x][y][z] = 1;
    zb t;
    t.x = x;
    t.y = y;
    t.z = z;
    t.s = 0;
    q.push(t);
    while(!q.empty()){
        int next[6][3] = {{1,0,0},{-1,0,0},{0,0,1},{0,1,0},{0,0,-1},{0,-1,0}};
        for(int i = 0; i < 6; ++i){
            int tx = q.front().x + next[i][0];
            int ty = q.front().y + next[i][1];
            int tz = q.front().z + next[i][2];
            if(tx < 0 || tx > A-1 || ty < 0 || ty > B-1 || tz < 0 || tz > C-1) continue;
            if(a[tx][ty][tz] == 0 && book[tx][ty][tz] == 0){
                book[tx][ty][tz] = 1;
                t.x = tx;
                t.y = ty;
                t.z = tz;
                t.s = q.front().s+1;
                q.push(t);
            }
            if(tx == A-1 && ty == B-1 && tz == C-1){
                flag = 0;
                if(q.front().s+1 <= T) printf("%d\n",q.front().s+1);
                else printf("-1\n");
                return;
            }
        }
        q.pop();
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        flag = 1;
        memset(book,0,sizeof(book));
        clearq();
        scanf("%d%d%d%d",&A,&B,&C,&T);
        for(int i = 0; i < A; ++i){
            for(int j = 0; j < B; ++j){
                for(int k = 0; k < C; ++k){
                    scanf("%d",&a[i][j][k]);
                }
            }
        }
        if(A+B+C-3>T||a[A-1][B-1][C-1]==1)
        {
            printf("-1\n");
            continue;
        }
        if(A==B && B==C && C==1)
        {
            printf("0\n");
            continue;
        }
        bfs(0,0,0);
        if(flag) printf("-1\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/shengsikandan/article/details/53728468