[BFS] hdu 1253 胜利大逃亡
来源:互联网 发布:java判断float整数 编辑:程序博客网 时间:2024/05/19 18:14
/**[BFS]hdu 1253 胜利大逃亡bfs一水,一直WA,因为已经处于起点,然后mat[0][0][0] == 1也是可行的*/#include <stdio.h>#include <string.h>#include <queue>using namespace std;const int N = 55;int mat[N][N][N],a,b,c,T;struct node{ int x,y,z,step; node(int d = 0,int e = 0,int f = 0){x = d; y = e; z = f;}};int move[6][3] = {0,0,1, 0,1,0, 0,0,-1, 0,-1,0, 1,0,0, -1,0,0};bool ok(node ps){ return ps.x >= 0 && ps.x < a && ps.y >=0 && ps.y < b && ps.z >=0 && ps.z < c && mat[ps.x][ps.y][ps.z] == 0;}int bfs(){ if(mat[a-1][b-1][c-1] == 1) return -1; if(a + b + c - 3 > T) return -1; if(a == 1 && b == 1 && c == 1) return 0; queue<node> que; node p,pt; mat[0][0][0] = 1; p.x = p.y = p.z = p.step = 0; que.push(p); while(!que.empty()) { p = que.front(); que.pop(); if(mat[p.x][p.y][p.z] >= T) return -1; for(int i = 0; i < 6; ++i) { pt = node(p.x+move[i][0],p.y+move[i][1],p.z+move[i][2]); if(pt.x == a-1 && pt.y == b-1 && pt.z == c-1) return mat[p.x][p.y][p.z]; if(ok(pt)) { mat[pt.x][pt.y][pt.z] = mat[p.x][p.y][p.z] + 1; que.push(pt); } } } return -1;}int main(){ int t,i,j,k; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&a,&b,&c,&T); for(i = 0; i < a; ++i) for(j = 0; j < b; ++j) for(k = 0; k < c; ++k) scanf("%d",&mat[i][j][k]); printf("%d\n",bfs()); } return 0;}