三维越狱问题bfs

https://blog.csdn.net/loy_184548/article/details/51035332?utm_source=blogxgwz5

//
//  B.cpp
//  搜索
//
//  Created by pro on 16/4/1.
//  Copyright (c) 2016年 pro. All rights reserved.
//

#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
int r,n,m;
string s[35][35];
struct node
{
    int x,y,z;
    int t;
};
node Begin,End;
queue <node> que;
int dx[6] = {0,0,0,0,1,-1};
int dy[6] = {0,0,-1,1,0,0};
int dz[6] = {1,-1,0,0,0,0};
int vis[35][35][35];
int bfs()
{
    que.push(Begin);
    vis[Begin.x][Begin.y][Begin.z] = 1;
    while(!que.empty())
    {
        node front = que.front();
        que.pop();
        if (front.x == End.x && front.y == End.y && front.z == End.z) return front.t;
        for (int i = 0; i < 6; i++)
        {
            node tmp;
            tmp.x = front.x + dx[i];
            tmp.y = front.y + dy[i];
            tmp.z = front.z + dz[i];
            tmp.t = front.t + 1;
            if (tmp.x >= 0 && tmp.x < r && tmp.y >= 0 && tmp.y < n && tmp.z >= 0 && tmp.z < m && !vis[tmp.x][tmp.y][tmp.z] && s[tmp.x][tmp.y][tmp.z] != '#')
            {
                que.push(tmp);
                vis[tmp.x][tmp.y][tmp.z] = 1;
            }
        }
    }
    return -1;
}
int main()
{
    while(cin >> r >> n >> m)
    {
        if (r == 0 && n == 0 && m == 0) break;
        memset(vis,0,sizeof(vis));
        while(!que.empty())
        {
            que.pop();
        }
        for (int i = 0; i < r; i++){
            string ret;
            for (int j = 0; j < n; j++){
                cin >> s[i][j];
                for (int k = 0; k < m; k++){
                    if (s[i][j][k] =='S'){
                        Begin.x = i,Begin.y = j,Begin.z = k;Begin.t = 0;
                    }
                    else if (s[i][j][k] == 'E'){
                        End.x = i,End.y = j,End.z = k;End.t= 0;
                    }
                }
            }
        }
        int ans = bfs();
        if (ans == -1){
            printf("Trapped!\n");
        }
        else{
            printf("Escaped in %d minute(s).\n",ans);
        }
    }

    return 0;
}
--------------------- 
作者:宣之于口 
来源:CSDN 
原文:https://blog.csdn.net/loy_184548/article/details/51035332 
版权声明:本文为博主原创文章,转载请附上博文链接!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值