#include<stdio.h>
#include<queue>
using namespace std;
bool mark[50][50][50];
int maze[50][50][50];
int walknext[][3] =
{
1,0,0,
-1,0,0,
0,1,0,
0,-1,0,
0,0,1,
0,0,-1
};
struct point
{
int x, y, z;
int t;
};
queue<point> q;
int BFS(int x, int y, int z)
{
while (q.empty() == false)
{
point now = q.front();
q.pop();
for (int i = 0; i < 6; i++)
{
int nx = now.x + walknext[i][0];
int ny = now.y + walknext[i][1];
int nz = now.z + walknext[i][2];
if (nx < 0 || nx >= x || ny < 0 || ny >= y || nz < 0 || nz >= z)
continue;
if (maze[nx][ny][nz] == 1)
continue;
if (mark[nx][ny][nz] == true)
continue;
point temp;
temp.x = nx;
temp.y = ny;
temp.z = nz;
temp.t = now.t + 1;
q.push(temp);
mark[nx][ny][nz] = true;
if (nx == x - 1 && ny == y - 1 && nz == z - 1)
return temp.t;
}
}
return -1;
}
int main()
{
int a, b, c, t;
while (scanf("%d%d%d%d",&a,&b,&c,&t) != EOF)
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
for (int k = 0; k < c; k++)
{
scanf("%d", &maze[i][j][k]);
mark[i][j][k] = false;
}
}
}
while (!q.empty())
q.pop();
mark[0][0][0] = true;
point temp;
temp.x = temp.y = temp.z = temp.t = 0;
q.push(temp);
int rec = BFS(a, b, c);
if (rec <= t)
printf("%d\n", rec);
else
printf("-1\n");
}
return 0;
}
胜利大逃亡(九度OJ 1456)(BFS搜索)
最新推荐文章于 2020-08-28 18:47:50 发布