Dungeon Master

题目链接:http://poj.org/problem?id=2251

这个题就多了两个方向的BFS,但是我做了整整一个下午, 一直是WA,找不出BUG,整个人都很气愤,洗完澡后再来小组看突然就发现问题所在了

sync_with_stdio(false);

这个语句是为了提高性能,iostream默认是与stdio关联在一起的,以使两者同步,因此消耗了iostream不少性能,设置为false后,不再同步了,iostream的性能提高了很多倍。
一切都怪它,我在用它的时候cout和printf一起用了,导致输出有问题,因为一道题差点怀疑人生,心累。

下面是AC代码

#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
char map[35][35][35];
bool vis[35][35][35];
int L, R, C;
int ex, ey, ez;
int dx[6] = {0, 0, -1, 1, 0, 0};
int dy[6] = {0, 0, 0, 0, 1, -1};
int dz[6] = {1, -1, 0, 0, 0, 0};

struct Node{
    int x, y, z;
    int time;
}start;

queue <Node> Q;

bool check(int x, int y, int z){ //边界判断
    if(x >= 0 && y >= 0 && z >= 0 && x < L && y < R && z < C){
        return true;
    }
    return false;
}



int bfs(){
    while(!Q.empty()){
        Node now = Q.front();
        Q.pop();
        Node next;
        for(int i = 0; i < 6; i++){
            next.x = now.x + dx[i];
            next.y = now.y + dy[i];
            next.z = now.z + dz[i];
            if(check(next.x, next.y, next.z) && map[next.x][next.y][next.z] != '#' && vis[next.x][next.y][next.z] == false){
                if(next.x == ex && next.y == ey && next.z == ez){
                    return now.time+1;
                }
                next.time = now.time +1;
                vis[next.x][next.y][next.z] = true;
                Q.push(next);
            }
        }

    }
    return -1;
}



int main(){
    ios::sync_with_stdio(false);
    freopen("btest.txt", "r", stdin);

    while(cin >> L >> R >> C && R+L+C){
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < L; i++){
            for(int j = 0; j < R; j++){
                for(int k = 0; k < C; k++){
                    char &tt = map[i][j][k];
                    cin >> tt;
                    if(tt == 'S'){
                        start.x = i;
                        start.y = j;
                        start.z = k;
                        start.time = 0;
                        Q.push(start);
                        vis[i][j][k] = true; //
                    }
                    else if(tt == 'E'){
                        ex = i;
                        ey = j;
                        ez = k;
                    }
                }
            }
        }

        int ret = bfs();
        if(ret == -1){
            cout << "Trapped!" << endl;
        }
        else{
            cout << "Escaped in " << ret << " minute(s)." << endl;
        }

        while(!Q.empty()){
            Q.pop();
        }
    }
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值