题目1456:胜利大逃亡(广度优先搜索BFS)

时间:2023-03-08 18:57:37

题目链接:http://ac.jobdu.com/problem.php?pid=1456

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
// 1456 胜利大逃亡.cpp
// Jobdu
//
// Created by PengFei_Zheng on 22/04/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <queue>
#define MAX_SIZE 50
#define WALL 1
#define MOVE 6
using namespace std; int space[MAX_SIZE][MAX_SIZE][MAX_SIZE];
bool visit[MAX_SIZE][MAX_SIZE][MAX_SIZE]; struct N{
int x;
int y;
int z;
int t;
};
queue<N>myQueue; int change[][]={
,,,
-,,,
,,,
,-,,
,,,
,,-
}; int k, a, b, c, t; int BFS(int a, int b , int c){
while(!myQueue.empty()){
N nowP = myQueue.front();
myQueue.pop();
for(int i = ; i < MOVE ; i ++){
int nx = nowP.x + change[i][];
int ny = nowP.y + change[i][];
int nz = nowP.z + change[i][];
if(nx< || nx>=a || ny< || ny>=b || nz< || nz>=c) continue;
if(space[nx][ny][nz] == WALL) continue;
if(visit[nx][ny][nz] == true) continue;
N tmp;
tmp.x = nx;
tmp.y = ny;
tmp.z = nz;
tmp.t = nowP.t + ;
myQueue.push(tmp);
visit[nx][ny][nz] = true;
if(nx==a- && ny==b- && nz==c-) return tmp.t;
}
}
return -;
} int main(){
scanf("%d",&k);
while(k--){
scanf("%d%d%d%d",&a,&b,&c,&t);
for(int i = ; i < a ; i++){
for(int j = ; j < b ; j++){
for(int k = ; k < c ; k++){
scanf("%d",&space[i][j][k]);
visit[i][j][k]=false;
}
}
}
while(!myQueue.empty()) myQueue.pop();
visit[][][] = true;
N tmp;
tmp.x=tmp.y=tmp.z=tmp.t=;
myQueue.push(tmp);
int cost = BFS(a,b,c);
if (cost <= t) printf("%d\n",cost);
else printf("-1\n");
}
return ;
} /**************************************************************
Problem: 1456
User: zpfbuaa
Language: C++
Result: Accepted
Time:20 ms
Memory:2132 kb
****************************************************************/