zoukankan      html  css  js  c++  java
  • 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!
    ");
            else
                printf("Escaped in %d minute(s).
    ", ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    R-时空可视化
    zz温故知新:Tomcat调优&JVM内存性能调优
    《CarbonData》
    《RocketMQ》
    《LinuxTools》
    《为什么说 Prometheus 是足以取代 Zabbix 的监控神器?》
    《Zabbix》
    zz《百度地图商业选址》
    《Dapper》
    BZOJ 1601: [Usaco2008 Oct]灌水 最小生成树_超级源点
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4685000.html
Copyright © 2011-2022 走看看