Hdu1253 胜利大逃亡 【简单bfs】
http://acm.hdu.edu.cn/showproblem.php?pid=1253
题目大意:一个三维的走迷宫游戏。
简单bfs
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> using namespace std; template <class T> void checkmin(T &t,T x) {if(x < t) t = x;} template <class T> void checkmax(T &t,T x) {if(x > t) t = x;} template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;} template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;} typedef pair <int,int> PII; typedef pair <double,double> PDD; typedef long long ll; #define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++) const int N = 55; int dir[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; int a , b , c , t; bool inmap(int x,int y,int z) { return x>=0&&x<a&&y>=0&&y<b&&z>=0&&z<c; } struct node { int x , y , z; }; int ma[N][N][N] , maze[N][N][N]; queue <node> q; void bfs() { node u , v; while(!q.empty()) q.pop(); u.x = u.y = u.z = 0; memset(ma,-1,sizeof(ma)); ma[0][0][0] = 0; q.push(u); while(!q.empty()) { u = q.front(); q.pop(); for(int i=0;i<6;i++) { int x = u.x + dir[i][0]; int y = u.y + dir[i][1]; int z = u.z + dir[i][2]; if(!inmap(x,y,z) || maze[x][y][z] == 1) continue; if(ma[x][y][z] == -1 || ma[u.x][u.y][u.z]+1<ma[x][y][z]) { ma[x][y][z] = ma[u.x][u.y][u.z] + 1; v.x = x , v.y = y , v.z = z; q.push(v); } } } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&a,&b,&c,&t); 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]); bfs(); if(ma[a-1][b-1][c-1] == -1 || ma[a-1][b-1][c-1] > t) puts("-1"); else printf("%d\n" , ma[a-1][b-1][c-1]); } return 0; }

浙公网安备 33010602011771号