hdoj_1253

来源:互联网 发布:知画的真名 编辑:程序博客网 时间:2024/06/05 20:44
//BFS#include<cstdio>#include<queue>#include<cstring>using namespace std;const int maxn = 60;int G[maxn][maxn][maxn];int vis[maxn][maxn][maxn];int A, B, C, T;//节点 typedef struct node {int x, y, z;int time;}Node;//存图void read_G() {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", &G[i][j][k]);}}}} //逃跑判断int bfs() {//三维坐标int dir[6][3] ={{0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}};queue<Node> coor;Node S = {0, 0, 0, 0};vis[0][0][0] = 1;coor.push(S);Node temp;int x1, y1, z1;while(!coor.empty()) {temp = coor.front(); coor.pop();if(temp.x == (A-1) && temp.y == (B-1) && temp.z == (C-1) && temp.time <= T && G[A-1][B-1][C-1] == 0) return temp.time;for(int i = 0; i < 6; i++) {x1 = temp.x + dir[i][0];y1 = temp.y + dir[i][1];z1 = temp.z + dir[i][2];if(x1 < A && x1 >= 0 && y1 < B && y1 >= 0 && z1 < C && z1 >= 0 && (temp.time+1) <= T && !G[x1][y1][z1] && !vis[x1][y1][z1]) {Node temp0 = {x1, y1, z1, temp.time+1};coor.push(temp0);vis[x1][y1][z1] = 1;}}}return -1;} int main() {int t;scanf("%d", &t);while(t--) {memset(vis, 0, sizeof(vis));read_G();printf("%d\n", bfs());}return 0;}

0 0
原创粉丝点击