【九度】题目1456:胜利大逃亡

题目地址:http://ac.jobdu.com/problem.php?pid=1456
题目描述:

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

题目分析:
        解决迷宫类似问题,如果是能不能到达,一般选用的是dfs,如果是求最短步数,一般选用的是bfs。
基本思路:
       1、输入数据存储至结构体;
       2、从开始节点开始依次入队列,bfs访问,发现到终点,就退出。
       3、比较打印结果。
C++ AC代码 Java超时。

#include <stdio.h>
#include <stdlib.h>
#include<queue>
#define INF 1000000000;
using namespace std;
const int maxn = 52;
int visit[maxn][maxn][maxn];
int mazeArr[maxn][maxn][maxn];
int stepArr[6][3] = {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}};
int a , b , c;
    
struct Node{
    int x;
    int y;
    int z;
    int step;
    Node(int x1,int y1, int z1, int step1){
        x = x1;
        y = y1;
        z = z1;
        step = step1;
    }
};
    
bool isTrue(int m , int n){
    if(m >= 0 && m < n){
        return true;
    }
    return false;
}
   
int bfs() {
    Node node(0,0,0,0);
    queue<Node> q ;
    while(!q.empty()) q.pop();
    q.push(node);
    while (!q.empty()) {
        node = q.front();
        q.pop();
        visit[node.x][node.y][node.z] = 1;
        if (node.x == a-1 && node.y == b-1 && node.z == c-1) {
            return node.step;
        }
        for (int i = 0; i < 6; i++) {
            int x = node.x + stepArr[i][0];
            int y = node.y + stepArr[i][1];
            int z = node.z + stepArr[i][2];
             
            if (isTrue(x , a) && isTrue(y , b) && isTrue(z , c)
                        && visit[x][y][z] == 0 && mazeArr[x][y][z] == 0) {
                visit[x][y][z] = 1;
                Node next(x, y, z, node.step+1);
                q.push(next);
            }
        }
    }
    return INF;
}
int main()
{   
    int t ;
    scanf("%d",&t);
    while(t > 0){
        t--;
        int time;
        scanf("%d%d%d%d",&a,&b,&c,&time);
        for(int i = 0; i < a ; i++){
            for(int j = 0 ; j < b ; j++){
                for(int k = 0 ; k < c ; k++){
                    scanf("%d",&mazeArr[i][j][k]);
                    visit[i][j][k] = 0;
                } 
            }
        }
        if (a + b + c - 3 > time) { 
            printf("-1\n"); 
            continue;
        }
        if (mazeArr[a - 1][b - 1][c - 1] == 1) { 
            printf("-1\n"); 
            continue;
        }
        int allTime = bfs();
        printf("%d\n",allTime <= time ? allTime : -1);
    }
       
    return 0;
}
/**************************************************************
    Problem: 1456
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:30 ms
    Memory:2152 kb
****************************************************************/

你可能感兴趣的:(【九度】题目1456:胜利大逃亡)