码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2251—— BFS——Dungeon Master

时间:2015-08-09 12:12:24      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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

/*
简单bfs,向六个方向拓展

*/
/************************************************
* Author        :Powatr
* Created Time  :2015-8-9 9:23:15
* File Name     :B.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dirx[] = {1, -1, 0, 0, 0};
int diry[] = {0, 0, 1, -1, 0};
struct P{
    int z, x, y; 
    int step;
}a[110]; 
int L, R, C;
int sx, sy, sz, ex, ey, ez;
queue <P> q;
bool vis[40][40][40];
char maze[40][40][40];
bool ok(int z, int x, int y)
{
    if(x >= 1 && x <= R && y >= 1 && y <= C && z >= 1 && z <= L && (maze[z][x][y] == ‘.‘ || maze[z][x][y] == ‘S‘ || maze[z][x][y] == ‘E‘) && !vis[z][x][y] ) 
        return true;
    return false;
}
int main(){
    while(~scanf("%d%d%d", &L, &R, &C) && L && R && C){
        getchar();
        memset(vis, false, sizeof(vis));
        memset(maze, ‘.‘, sizeof(maze));
        for(int i = 1; i <= L; i++){
            for(int j = 1; j <= R; j++){
                for(int k = 1; k <= C; k++){
                    scanf("%c", &maze[i][j][k]);
                    if(maze[i][j][k] == ‘S‘){
                        sx = j, sy = k, sz = i;
                    }
                    if(maze[i][j][k] == ‘E‘){
                        ex = j, ey = k, ez = i;
                    }
                }
                getchar();
            }
            getchar();
        }
        while(!q.empty())
        q.pop();
        q.push((P){sz, sx, sy, 0});
        int flag = 1;
        while(!q.empty()){
            P now = q.front();
            q.pop();
            int x = now.x, y = now.y, z = now.z;
            int step = now.step;
            if(!ok(z, x, y)) continue;
            if(x == ex && y == ey && z == ez) {
                printf("Escaped in %d minute(s).\n", step);
                flag = 0;
                break;
            }
            for(int i = 0; i < 4; i++){
               int dx = x + dirx[i], dy = y + diry[i];
         //   printf("%d %d %d\n", z, dx, dy);
                if(ok(z, dx, dy)){
                    vis[z][x][y] = true;
                    q.push((P){z, dx, dy, step+1});
                }
            }
            if(ok(z+1, x, y)){
                vis[z][x][y] = true;
                q.push((P){z+1, x, y, step+1});
            }
            if(ok(z-1, x, y)){
                vis[z][x][y] = true;
                q.push((P){z-1, x, y, step+1});
            }
        }
        if(flag) printf("Trapped!\n");
    }
    return 0;
}

  

POJ2251—— BFS——Dungeon Master

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4714715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!