BFS,遍历过程对每个节点记录步数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int maze[50][50][50];
int dir[6][3] = {1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};
int a = 0, b = 0, c = 0;
int T = 0;
typedef struct node
{int x, y, z;int t;node() : t(0), x(0), y(0), z(0){};node(int x, int y, int z, int t, int state) : x(x), y(y), z(z), t(t) { maze[x][y][z] = state; };node(int state) : t(0), x(0), y(0), z(0) { maze[x][y][z] = state; };
} node;bool checkIt(int x, int y, int z)
{if (x >= 0 && x < a && y >= 0 && y < b && z >= 0 && z < c)return !maze[x][y][z];return false;
}
int bfs()
{queue<node> que;que.push(node(1));int nextX, nextY, nextZ;node curNode;while (!que.empty()){curNode = que.front();que.pop();for (size_t i = 0; i < 6; i++){nextX = curNode.x + dir[i][0];nextY = curNode.y + dir[i][1];nextZ = curNode.z + dir[i][2];if (checkIt(nextX, nextY, nextZ)){node newNode(nextX, nextY, nextZ, curNode.t + 1, 1);que.push(newNode);if (nextX == a - 1 && nextY == b - 1 && nextZ == c - 1)return newNode.t;}}}return INT_MAX;
}
int main()
{int K;scanf("%d", &K);while (K--){// input.scanf("%d %d %d %d", &a, &b, &c, &T);for (size_t i = 0; i < a; i++)for (size_t j = 0; j < b; j++)for (size_t k = 0; k < c; k++)scanf("%d", &maze[i][j][k]);// cal.int t = bfs();if (t <= T)printf("%d\n", t);elseprintf("-1\n");}system("pause");return 0;
}