ZOJ 1940 Dungeon Master 【BFS】

//2634394 	2011-08-21 00:56:14 	Accepted 	1940 	C++ 	0 	496 	ylwh@Unknown
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct node
{
    int x, y, z;
}Q[27001];
int main(void)
{
    char map[31][31][31];
    int flag[31][31][31], l, r, c, i, j, k;
    int head, tail, staue, dx, dy, dz;
    int dxdydz[][3] = { {0, 0, 1}, {0, 0, -1}, {1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0} };
    while( scanf("%d%d%d", &l, &r, &c), l )
    {
        memset(flag, 0, sizeof(flag));
        staue = 1;
        for(i=0; i<l; i++)
            for(j=0; j<r; j++)
                for(k=0; k<c; k++)
                {
                    scanf(" %c", &map[i][j][k]);
                    if( map[i][j][k] == 'S')
                    {
                        flag[i][j][k] = 1;
                        Q[0].x = i;
                        Q[0].y = j;
                        Q[0].z = k;
                    }
                }
        head = 0;
        tail = 1;
        while(head != tail)
        {
            for(i=0; i<6; i++)
            {
                dx = Q[head].x + dxdydz[i][0];
                dy = Q[head].y + dxdydz[i][1];
                dz = Q[head].z + dxdydz[i][2];
                if(dx >= 0 && dy >= 0 && dz >= 0 &&
                   dx < l && dy < r && dz < c)
                {
                    if(map[dx][dy][dz] == 'E')
                    {
                        flag[dx][dy][dz] = flag[Q[head].x][Q[head].y][Q[head].z] + 1;
                        goto X;
                    }
                    else if(map[dx][dy][dz] == '.' && flag[dx][dy][dz] == 0)
                    {
                        flag[dx][dy][dz] = flag[Q[head].x][Q[head].y][Q[head].z] + 1;
                        Q[tail].x = dx;
                        Q[tail].y = dy;
                        Q[tail].z = dz;
                        tail++;
                    }
                }
            }
            head++;
        }
        printf("Trapped!\n");
        staue = 0;
        X:
            if(staue)
                printf("Escaped in %d minute(s).\n", flag[dx][dy][dz]-1);
    }
    return 0;
}


你可能感兴趣的:(ZOJ 1940 Dungeon Master 【BFS】)