Dungeon Master POJ - 2251

9 篇文章 0 订阅

刚开始是想只开个map数组,然后入队列的时候map变为‘#’,但是一直WA,暂时未明白,所以另开了个数组,来表示是否已存储。 

#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>

using namespace std;

int L, R, C, SL, SR, SC, EL, ER, EC;
char t;
char map[33][33][33];
bool pos[33][33][33];

int dir[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};

struct Node
{
    int l, r, c; //position
    int step;
    char t;
   // bool flag;
    Node(){
        l = r = c = 0;
        step = 0;
        t = '.';
//        flag = false;
    }
    Node(int q, int b, int d, int w, char m){
      l = q, r = b, c = d, step = w, t = m;
    }
};
Node tmp1;

void bfs()
{

    queue<Node> q;
//    Node tmp(SL, SR, SC, 0, 'S');
    Node tmp;
    tmp.l = SL;
    tmp.r = SR;
    tmp.c = SC;
    tmp.step = 0;
    tmp.t = 'S';
    //   cout << tmp.t << endl;
    q.push(tmp);
    pos[SL][SR][SC] = true;

    while(!q.empty()){
        tmp1 = q.front();

        if(tmp1.t == 'E'){
            return;
        }
        q.pop();

        for(int i=0; i<6; i++){

            Node tmp2;

            tmp2.l = tmp1.l+dir[i][0];
            tmp2.r = tmp1.r+dir[i][1];
            tmp2.c = tmp1.c+dir[i][2];

//            cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << " " << map[tmp2.l][tmp2.r][tmp2.c] << endl;
            //judge
            if((map[tmp2.l][tmp2.r][tmp2.c] == '.'||map[tmp2.l][tmp2.r][tmp2.c] == 'E')  && (!pos[tmp2.l][tmp2.r][tmp2.c]) && tmp2.l<=30 && tmp2.r<=30 && tmp2.c<=30 && tmp2.l>0 && tmp2.r>0 && tmp2.c>0){
//                tmp2.flag = true;
                tmp2.step = tmp1.step+1;
                tmp2.t = map[tmp2.l][tmp2.r][tmp2.c];
//                cout << tmp2.t << " ";
                q.push(tmp2);


                pos[tmp2.l][tmp2.r][tmp2.c] = true;

                map[tmp2.l][tmp2.r][tmp2.c] = '#';
            }
        }
//        cout << endl;

    }

}

int main()
{
//    memset(map, '#', sizeof(map));
 
    while(cin >> L >> R >> C && (L && R &&C)){
    memset(pos, false, sizeof(pos));

//        int n = 0;

        for(int i=1; i<=L; i++){
            getchar();//层之间的换行符
            for(int j=1; j<=R; j++){
                for(int k=1; k<=C; k++){

                    cin >> map[i][j][k];

                    t = map[i][j][k];

                    //记录起始位置和结束位置
                    if(t == 'S'){
                        SL = i, SR = j, SC = k;
                    }
                    if(t == 'E'){
                        EL = i, ER = j, EC = k;
                    }

                }
                getchar();//行之间的换行符
            }

        }
 //       cout << SL << SR << SC << endl;

        //开始走动
        bfs();

        if(tmp1.t == 'E'){
            printf("Escaped in %d minute(s).\n", tmp1.step);
        }
        else
            printf("Trapped!\n");

    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值