hdu 1253 3D bfs

来源:互联网 发布:怎么清除手机数据 编辑:程序博客网 时间:2024/05/19 19:42

题意:

3D迷宫bfs。


解析:

和之前的那个3D迷宫差不多。


代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int maxn = 50 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-6;const double pi = acos(-1.0);const double ee = exp(1.0);int dir[][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};int n, m, l;int backTime;int g[maxn][maxn][maxn];int step[maxn][maxn][maxn];struct Node{    int x, y, z;    Node(int _x, int _y, int _z)    {        x = _x;        y = _y;        z = _z;    }};bool ok(int x, int y, int z){    if (0 <= x && x < n && 0 <= y && y < m && 0 <= z && z < l && g[x][y][z] != 1)        return true;    return false;}int bfs(){    memset(step, -1, sizeof(step));    queue<Node> q;    step[0][0][0] = 0;    q.push(Node(0, 0, 0));    while (!q.empty())    {        Node now = q.front();        q.pop();        int x = now.x;        int y = now.y;        int z = now.z;        if (x == n - 1 && y == m - 1 && z == l - 1)        {            return step[x][y][z];        }        for (int i = 0; i < 6; i++)        {            int nx = x + dir[i][0];            int ny = y + dir[i][1];            int nz = z + dir[i][2];            if (ok(nx, ny, nz))            {                if (step[nx][ny][nz] == -1)                {                    step[nx][ny][nz] = step[x][y][z] + 1;                    if (step[nx][ny][nz] <= backTime)                    {                        q.push(Node(nx, ny, nz));                    }                }            }        }    }    return -1;}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);#endif // LOCAL    int ncase;    scanf("%d", &ncase);    while (ncase--)    {        scanf("%d%d%d%d", &l, &n, &m, &backTime);        for (int k = 0; k < l; k++)        {           for (int i = 0; i < n; i++)           {               for (int j = 0; j < m; j++)               {                   scanf("%d", &g[i][j][k]);               }           }        }        printf("%d\n", bfs());    }    return 0;}


0 0
原创粉丝点击