HDU 1253 胜利大逃亡
https://vjudge.net/problem/HDU-1253
题意:输入T组测试
每组测试输入 X,Y,Z,Time,X,YZ代表一个三维空间,Time表示魔王回来的时间
在三维空间中 1 代表墙,不可走,0代表路,可以走,每次向三维空间走一步,所
需时间 t+1,起始点是(0,0,0),终点是(x-1,y-1,z-1),若逃离所需时间t < Time
输出t,否则输出 -1.
题解:三维空间搜索 BFS。
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;
const int N = 55;
int t;
char mp[N][N][N]; //三维空间地图
int A,B,C,K;
int dir[6][3] = { //六个方向
{0,0,1},
{1,0,0},
{0,1,0},
{-1,0,0},
{0,0,-1},
{0,-1,0}
};
struct node
{
int x,y,z,time;
};
//判断方向是否合法
bool inbound(int x,int l,int r)
{
if(x < l || x >= r) return false;
return true;
}
//裸BFS
void BFS(int xx,int yy, int zz, int tt)
{
bool flag = true;
node strat;
strat.x = xx, strat.y = yy, strat.z = zz,strat.time = tt;
queue<node> q;
q.push(strat);
while(!q.empty())
{
node now = q.front();
q.pop();
if(now.x == A-1 && now.y == B-1 && now.z == C-1)
{
if(now.time <= K)
printf("%d\n",now.time);
else
printf("-1\n");
flag = false;
// printf("%d\n",now.time);
}
for(int i = 0; i < 6; i++) //六个方向扩展
{
node next;
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.z = now.z + dir[i][2];
next.time = now.time + 1;
if((!inbound(next.x,0,A)) || (!inbound(next.y,0,B)) || (!inbound(next.z,0,C))) continue;
if(mp[next.x][next.y][next.z] == '0')
{
// printf("%d\n",next.time);
mp[next.x][next.y][next.z] = '1';
q.push(next);
}
}
}
if(flag) puts("-1");
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(mp,0,sizeof(mp));
scanf("%d %d %d %d",&A,&B,&C,&K);
for(int i = 0; i < A; i++)
for(int j = 0; j < B; j++)
for(int k = 0; k < C; k++)
scanf(" %c",&mp[i][j][k]);
BFS(0,0,0,0);
}
return 0;
}
永远年轻 永远热泪盈眶!

浙公网安备 33010602011771号