Dungeon Master---2251(bfs)

http://poj.org/problem?id=2251

 

有一个三维的牢房地图 求从S点走E点的最小时间;

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;

#define N 50
#define INF 0xfffffff
int dir[6][3] = { {1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0} };
char map[N][N][N];
int L,R,C;
int vis[N][N][N];
struct node
{
    int x, y, z, step;
};

int bfs(node s, node e)
{
    memset(vis, 0, sizeof(vis));
    queue<node>Q;
    s.step = 0;
    vis[s.x][s.y][s.z] = 1;
    Q.push(s);
    while(Q.size())
    {
        node p, q = Q.front(); Q.pop();
        if(q.x == e.x && q.y == e.y && q.z == e.z )
            return q.step;
        for(int i=0; i<6; i++)
        {
            p.x = q.x + dir[i][0];
            p.y = q.y + dir[i][1];
            p.z = q.z + dir[i][2];
            if( p.x<L && p.x>=0 && p.y>=0 && p.y<R && p.z>=0 && p.z<C && !vis[p.x][p.y][p.z] && map[p.x][p.y][p.z] != '#')
            {
                p.step = q.step + 1;
                vis[p.x][p.y][p.z] = 1;
                Q.push(p);
            }
        }
    }
    return -1;
}

int main()
{
    char str[N];
    while(scanf("%d%d%d", &L, &R, &C), L + R + C)
    {
        node s, e;
        for(int i=0; i<L; i++)
        {
            gets(str);
            for(int j=0; j<R; j++)
            {
                gets(map[i][j]);
                for(int k=0; k<C; k++)
                {
                    if(map[i][j][k] == 'S')
                        s.x = i, s.y = j, s.z = k;
                    if(map[i][j][k] == 'E')
                        e.x = i, e.y = j, e.z = k;
                }
            }

        }
        int ans = bfs(s, e);
        if(ans==-1)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n", ans);
    }
    return 0;
}
View Code

 

posted @ 2015-07-29 09:15  西瓜不懂柠檬的酸  Views(157)  Comments(0Edit  收藏  举报
levels of contents