zoukankan      html  css  js  c++  java
  • Dungeon Master POJ 2251

    题目链接: http://poj.org/problem?id=2251

    题意:现有一个三维的地牢,问你能否逃出。

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #include<algorithm>
    #include<queue>
    #include<iostream>
    using namespace std;
    
    #define maxn 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} };
    int l, r, c;
    char maps[maxn][maxn][maxn];
    int v[maxn][maxn][maxn];
    
    struct node
    {
        int x, y, z;
        int step;
    };
    
    int BFS(node s, node e)
    {
        queue<node>Q;
        s.step = 0;
        Q.push(s);
    
        while(Q.size())
        {
            node now, next;
            now = Q.front();
            Q.pop();
    
            if(now.x == e.x && now.y == e.y && now.z == e.z)
                return now.step;
    
            for(int i=0; i<6; i++)
            {
                next.x = now.x + dir[i][0];
                next.y = now.y + dir[i][1];
                next.z = now.z + dir[i][2];
    
                if(next.x>=0 && next.x<l && next.y>=0 && next.y<r && next.z>=0 && next.z<c
                   && !v[next.x][next.y][next.z] && maps[next.x][next.y][next.z]!='#')
                {
                    v[next.x][next.y][next.z] = 1;
                    next.step = now.step + 1;
                    Q.push(next);
                }
            }
        }
        return -1;
    }
    int main()
    {
        node s, e;
        while(scanf("%d %d %d", &l, &r, &c), l+r+c)
        {
    
            memset(v, 0, sizeof(v));
            for(int i=0; i<l; i++)
            {
                for(int j=0; j<r; j++)
                {
                    scanf("%s", maps[i][j]);
                    for(int k=0; k<c; k++)
                    {
                        if(maps[i][j][k] == 'S')
                            s.x=i,s.y=j,s.z=k;
                        if(maps[i][j][k] == 'E')
                            e.x=i,e.y=j,e.z=k;
                    }
                }
            }
    
            int ans = BFS(s,e);
    
            if(ans == -1)  printf("Trapped!
    ");
            else printf("Escaped in %d minute(s).
    ",ans);
    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    欧拉函数的一个性质及其证明
    【机器人M号】题解
    【求和】题解
    uva11292贪心基础题目
    hdu 1009 贪心基础题
    近期计划,理清思路,大步向前
    hdu1712 分组背包
    TOJ3596 二维背包
    hdu1114 完全背包
    BestCoder Round #81 (div.2)1001
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5675872.html
Copyright © 2011-2022 走看看