栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 2251 Dungeon Master

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 2251 Dungeon Master

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;#define maxn 35struct Point{    int x, y, z;} s, e;int dir[6][3] ={{ 1, 0, 0 },{ -1, 0, 0 },{ 0, 1, 0 },{ 0, -1, 0 },{ 0, 0, 1 },{ 0, 0, -1 } };bool map[maxn][maxn][maxn];int dist[maxn][maxn][maxn];Point q[maxn * maxn * maxn];int front, rear;int nx, ny, nz;void input(){    memset(map, 0, sizeof(map));    getchar();    for (int i = 0; i < nz; i++)    {        for (int j = 0; j < nx; j++)        { for (int k = 0; k < ny; k++) {     char ch = getchar();     if (ch == '#')         map[j][k][i] = false;     else         map[j][k][i] = true;     if (ch == 'S')     {         s.z = i;         s.x = j;         s.y = k;     }     if (ch == 'E')     {         e.z = i;         e.x = j;         e.y = k;     } } getchar();        }        getchar();    }}bool check(int x, int y, int z){    if (x < 0 || y < 0 || z < 0 || x >= nx || y >= ny || z >= nz)        return false;    if (dist[x][y][z] != -1)        return false;    return map[x][y][z];}int bfs(){    memset(dist, -1, sizeof(dist));    front = 0;    rear = 1;    q[0] = s;    dist[s.x][s.y][s.z] = 0;    while (front != rear)    {        Point a = q[front++];        if (front == maxn * maxn * maxn) front = 0;        for (int i = 0; i < 6; i++)        { int x = dir[i][0] + a.x; int y = dir[i][1] + a.y; int z = dir[i][2] + a.z; if (check(x, y, z)) {     q[rear].x = x;     q[rear].y = y;     q[rear++].z = z;     if (rear == maxn * maxn * maxn)         rear = 0;     dist[x][y][z] = dist[a.x][a.y][a.z] + 1;     if (x == e.x && y == e.y && z == e.z)         return dist[x][y][z]; }        }    }    return -1;}int main(){    while (scanf("%d%d%d", &nz, &nx, &ny), nx | ny | nz)    {        input();        int ans = bfs();        if (ans == -1) printf("Trapped!n");        else printf("Escaped in %d minute(s).n", ans);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372809.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号