胜利大逃亡(bfs)

题目

题目描述:
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

思路

  • 剪枝法的bfs题目
  • 注意特殊条件的判断,这里出口为墙则出不去,入口为墙则可以出去

AC代码

#include <stdio.h>
#include <stdlib.h>
 
#define QUEUESIZE 51 * 51 * 51
#define MAX 1005
 
int direction[6][3] = {{0, 0, 1}, {0, 0, -1}, {1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}};
 
int A, B, C, T, spend, maze[51][51][51];
 
 
struct node
{
    int x, y, z;
    int time;
};
 
struct queue
{
    struct node data[QUEUESIZE];
    int front, rear, count;
};
 
void en_queue(struct queue *Q, struct node p);
struct node de_queue(struct queue *Q);
int breadth_first_search();
 
int main()
{
    int n, x, y, z;
    scanf("%d", &n);
 
    while (n --) {
        scanf("%d %d %d %d", &A, &B, &C, &T);
 
        for (z = 0; z < A; z ++) {
            for (x = 0; x < B; x ++) {
                for (y = 0; y < C; y ++) {
                    scanf("%d", &maze[z][x][y]);
                }
            }
        }
 
        if (A + B + C - 3 > T) {
            printf("-1\n");
        }else if (maze[A - 1][B - 1][C - 1] == 1) {
            printf("-1\n");
        }else if (A == 1 && B == 1 && C == 1) {
            printf("0\n");
        }else {
            spend = breadth_first_search();
            printf("%d\n", spend);
        }
    }
 
    return 0;
}
 
/**
 * Description:入队操作
 */
void en_queue(struct queue *Q, struct node p)
{
    if (Q->count < QUEUESIZE) {
        Q->data[Q->rear] = p;
        Q->count ++;
        Q->rear += 1;
    }else {
        exit(-1);
    }
}
 
/**
 * Description:出队操作
 */
struct node de_queue(struct queue *Q)
{
    if (Q->count > 0) {
        struct node p;
        p = Q->data[Q->front];
        Q->front += 1;
        Q->count --;
        return p;
    }else {
        exit(-1);
    }
}
 
/**
 * Description:广度优先搜索
 */
int breadth_first_search()
{
    int i, tz, tx, ty;
 
    struct node s, u, p;
 
    // 初始化bfs起始节点
    s.x = s.y = s.z = s.time = 0;
    maze[0][0][0] = 1;
    // 初始化队列
    struct queue *Q = (struct queue *)malloc(sizeof(struct queue));
    Q->front = Q->rear = Q->count = 0;
 
    // 初始节点入队列
    en_queue(Q, s);
 
    while (Q->count != 0) {
        u = de_queue(Q);
 
        if (u.z == A - 1 && u.x == B - 1 && u.y == C - 1 && u.time <= T) {
            return u.time;
        }
 
        for (i = 0; i < 6; i ++) {
            tz = u.z + direction[i][0];
            tx = u.x + direction[i][1];
            ty = u.y + direction[i][2];
            if (tz >= 0 && tz < A && tx >= 0 && tx < B && ty >= 0 && ty < C && maze[tz][tx][ty] == 0) {
                maze[tz][tx][ty] = 1;
                p.x = tx;
                p.y = ty;
                p.z = tz;
                p.time = u.time + 1;
                en_queue(Q, p);
            }
        }
    }
 
    return -1;
}
/**************************************************************
    Problem: 1456
    User: wangzhengyi
    Language: C
    Result: Accepted
    Time:30 ms
    Memory:32568 kb
****************************************************************/


posted @ 2013-04-07 09:36  java程序员填空  阅读(145)  评论(0编辑  收藏  举报