Dungeon Master POJ

来源:互联网 发布:软件系统响应时间 编辑:程序博客网 时间:2024/06/04 01:15

一道简单的3维的BFS搜索,,一开始受上一个题的影响,直接跑到DFS去了,DFS然后取最优结果,妥妥的超时,期间还犯了一个错误,一个三维数组map[i][j][k], i 应该是对应的z轴,j对应x轴,k对应y轴; 之后换为BFS后,忘了再压入队列的时候把标记设为true,交了一发又超时了,。。改过来之后又交了一发结果wrong了,郁闷,一对比,发现输出有个字母打错了,郁闷了一下午了

//leehaoze#include <iostream>#include <deque>#include <string>#include <vector>#include <queue>#include <cstdio>#include <stack>#include <algorithm>#include <cstring>#include <cctype>#include <cstdio>#include <cmath>#include <cstdlib>using namespace std;const int INF = 1 << 29;#define INC_SAT(val) (val = ((val)+1 > (val)) ? (val)+1 : (val))#define ARR_SIZE(a) ( sizeof( (a) ) / sizeof( (a[0]) ) )#define ULL unsigned long long#define MAXN 30struct Pos{    int x_,y_,z_,step_;    Pos():step_(0){}    Pos(int x,int y,int z,int step):x_(x),y_(y),z_(z),step_(step){}    bool operator==(const Pos &B){        return x_ == B.x_ && y_ == B.y_ && z_ == B.z_;    }};int Move_X[] = {0, 0, 0, 0, 1,-1};int Move_Y[] = {0, 0, 1,-1, 0, 0};int Move_Z[] = {1,-1, 0, 0, 0, 0};int L, R, C, ans;char map[MAXN][MAXN][MAXN];bool book[MAXN][MAXN][MAXN];Pos start;Pos target;bool Input() {    scanf("%d%d%d",&L,&R,&C);    getchar();    if (L + R + C == 0)return false;    for (int i = 0; i < L; ++i) {        for (int j = 0; j < R; ++j) {            for (int k = 0; k < C; ++k) {                scanf("%c",&map[i][j][k]);                book[i][j][k] = false;                if (map[i][j][k] == 'S') {                    start.x_ = j;                    start.y_ = k;                    start.z_ = i;                }                if(map[i][j][k] == 'E'){                    target.x_ = j;                    target.y_ = k;                    target.z_ = i;                }            }            getchar();        }        getchar();    }    ans = INF;    return true;}bool Legal(int x, int y, int z) {    if (x >= 0 && x < R && y >= 0 && y < C && z >= 0 && z < L && map[z][x][y] != '#') {        return !book[z][x][y];    }    return false;}int BFS() {    queue<Pos> Q;    Q.push(start);    book[start.z_][start.x_][start.y_] = true;    while(!Q.empty()){        Pos now = Q.front();        Q.pop();        if(now == target){            return now.step_;        }        for (int i = 0; i < 6; ++i) {            int dx = now.x_ + Move_X[i];            int dy = now.y_ + Move_Y[i];            int dz = now.z_ + Move_Z[i];            if(Legal(dx,dy,dz)){                book[dz][dx][dy] = true;                Q.push(Pos(dx,dy,dz,now.step_ + 1));            }        }    }    return INF;}int main() {#ifdef LOCAL    freopen("IN.txt", "r", stdin);#endif    std::ios::sync_with_stdio(false);    while (Input()) {        ans = BFS();        if (ans == INF) {            cout << "Trapped!" << endl;        } else {            cout << "Escaped in " << ans << " minute(s)." << endl;        }    }}
0 0
原创粉丝点击