poj2251-dungeon master(bfs广搜)

来源:互联网 发布:linux基本命令使用 编辑:程序博客网 时间:2024/04/25 23:47

复制题目过来的时候出现了不可调和的错误,又貼链接吧
poj2251

这个题目是平面迷宫的一个拓展问题,不过并没有实质性的突破,只是变成了三维的而已,多加了个上下楼。因此照着深搜的框架写就可以了。但我在写的时候出现了诸多bug,比如赋值的时候写了两个等号,标记走过的路线就失效了,把所有路线都往队列里压,结果就MLE了,题目给的数据范围是30,但讨论区里有人开到一百多才过,我的测试31就可以过了。

代码是老套路,不解释了

#include<iostream>#include<queue>#include<cstring>#include<cstdlib>#include<cstdio>using namespace std;const int sz = 110;char maze[sz][sz][sz];int l, r, c;int dir[6][3] = {{0,1,0},{0,0,1},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}};struct point{       int x,        y,        z,        step;}now, next;queue<point> q;bool vtd[sz][sz][sz];bool inside_x(int x){    if(x >= 0 && x < l)        return true;    return false;}bool inside_y(int x){    if(x >= 0 && x < r)        return true;    return false;}bool inside_z(int x){    if(x >= 0 && x < c)        return true;    return false;}int bfs(int x, int y, int z, int step){    while(!q.empty())        q.pop();    now.x = x, now.y = y, now.z = z, now.step = step;    q.push(now);    vtd[x][y][z] = true;    while(!q.empty())    {        now = q.front();        q.pop();        next = now;        for(int i = 0; i < 6; i ++)        {            next.x = now.x + dir[i][0];            next.y = now.y + dir[i][1];            next.z = now.z + dir[i][2];            if(maze[next.x][next.y][next.z] == '#' || !inside_x(next.x) || !inside_y(next.y) || !inside_z(next.z))                continue;            if(vtd[next.x][next.y][next.z])            {                continue;            }            if(maze[next.x][next.y][next.z] == 'E')                return next.step + 1;            next.step = now.step + 1;            vtd[next.x][next.y][next.z] = true;            q.push(next);        }    }    return -1;}int main(){   // freopen("output.txt","r",stdin);    while(scanf("%d%d%d", &l, &r, &c))    {        if(l == 0)            break;        int res;        point start;        int findStart = 1;        memset(vtd,false,sizeof(vtd));        memset(maze,0,sizeof(maze));        for(int i = 0; i < l; i ++)        {            for(int j = 0; j < r; j ++)            {                scanf("%s", &maze[i][j]);                if(findStart)                    for(int k = 0; k < c; k ++)                    {                            if(maze[i][j][k] == 'S')                            {                                start.x = i;                                start.y = j;                                start.z = k;                                findStart = 0;                            }                    }            }        }        res = bfs(start.x,start.y,start.z,0);        if(res > 0)            printf("Escaped in %d minute(s).\n", res);        else            cout << "Trapped!" << endl;    }}
0 0
原创粉丝点击