[POJ - 2251Dungeon Master ]

还是迷宫只是从之前的二维变成了三维而已。

#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
using namespace std;

struct test
{
    int x,y,z;
    int step;
};

int direction[6][3]={1,0,0, 0,1,0, 0,0,1, -1,0,0, 0,-1,0, 0,0,-1};
int main()
{
    int xi, yi, zi;
    queue<test> q;
    
    test temp, start, end;
    while(~scanf("%d%d%d", &zi,&yi,&xi) && zi+yi+xi)
    {
      while(!q.empty())
        q.pop(); 
    char s[31][31][31];
    int vis[31][31][31];
    
    memset(vis, 0, sizeof(vis));
    
    for(int i = 0;i < zi;i++)
        for(int j = 0; j< yi;j++)
            for(int k = 0; k<xi;k++)
                {
                    cin>>s[i][j][k];
                    if(s[i][j][k] == 'S')
                        {
                            start.x = k;
                            start.y = j;
                            start.z = i;
                        }
                    else if(s[i][j][k] == 'E')
                        {
                            end.x = k;
                            end.y = j;
                            end.z = i;    
                        }
                }
//    printf("start: %d %d %d\nend: %d %d %d", start.z, start.y, start.x, end.z, end.y, end.x);
    start.step = 0;            
    q.push(start);
    vis[start.z][start.y][start.x] = 1;
    int ans = 0;
    while(!q.empty())
    {
        start = q.front();
        if(start.z == end.z && start.x == end.x && start.y == end.y) 
        {ans = start.step; 
        break;
    }
        q.pop();
        for(int i = 0; i< 6; i++)
        {
            temp.z = start.z + direction[i][0];
            temp.y = start.y + direction[i][1];
            temp.x = start.x + direction[i][2];
            if(temp.x <xi&&temp.x>=0&&temp.y <yi&&temp.y>=0&&temp.z>=0&&temp.z<zi&&!vis[temp.z][temp.y][temp.x]&&s[temp.z][temp.y][temp.x] != '#')
                {
                    
                    temp.step = start.step+1;
                //    printf("temp: %d %d %d %d\n", temp.z, temp.y, temp.x, start.step);
                    q.push(temp);
                    vis[temp.z][temp.y][temp.x] = 1;
                }
        }
    }
    
    if(ans)
        printf("Escaped in %d minute(s).\n", ans);
    else
        printf("Trapped!\n");
         
    
}
    return 0;
 } 

 

转载于:https://www.cnblogs.com/Vikyanite/p/11382465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值