标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30841 Accepted Submission(s): 11509
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 struct Piont{ 7 int x; 8 int y; 9 int z; 10 Piont(int x, int y, int z) :x(x), y(y), z(z){}; 11 }; 12 13 typedef Piont P; 14 const int INF = 100000000; 15 int m[50][50][50]; 16 int d[50][50][50]; 17 int dx[6] = { 1, -1, 0, 0, 0, 0 }; 18 int dy[6] = { 0, 0, 1, -1, 0, 0 }; 19 int dz[6] = { 0, 0, 0, 0, 1, -1 }; 20 21 int main() 22 { 23 int K, L, R, C, time; 24 int Sx, Sy, Sz, Ex, Ey, Ez; 25 scanf("%d", &K); 26 while (K--) 27 { 28 scanf("%d%d%d%d", &L, &R, &C, &time); 29 memset(m, 0, sizeof(m)); 30 Sx = 0; Sy = 0; Sz = 0; 31 Ex = L - 1; Ey = R - 1; Ez = C - 1; 32 for (int i = 0; i < L; i++) 33 { 34 for (int j = 0; j < R; j++) 35 { 36 for (int k = 0; k < C; k++) 37 { 38 scanf("%d", &m[i][j][k]); 39 } 40 } 41 } 42 43 44 for (int i = 0; i < L; i++) 45 for (int j = 0; j < R; j++) 46 for (int k = 0; k < C; k++) 47 d[i][j][k] = INF; 48 49 queue<P> q; 50 q.push(P(Sx, Sy, Sz)); 51 d[Sx][Sy][Sz] = 0; 52 53 while (q.size()) 54 { 55 P p = q.front(); 56 q.pop(); 57 if (p.x == Ex && p.y == Ey && p.z == Ez) 58 break; 59 for (int i = 0; i < 6; i++) 60 { 61 int nx = p.x + dx[i]; 62 int ny = p.y + dy[i]; 63 int nz = p.z + dz[i]; 64 65 if (0 <= nx&&nx < L && 0 <= ny&&ny < R && 0 <= nz&&nz < C 66 && m[nx][ny][nz] == 0 && d[nx][ny][nz] == INF) 67 { 68 q.push(P(nx, ny, nz)); 69 d[nx][ny][nz] = d[p.x][p.y][p.z] + 1; 70 } 71 } 72 } 73 74 if (d[Ex][Ey][Ez] != INF&&d[Ex][Ey][Ez] < time) 75 { 76 printf("%d\n", d[Ex][Ey][Ez]); 77 } 78 else 79 { 80 printf("-1\n"); 81 } 82 83 } 84 }
标签:
原文地址:http://www.cnblogs.com/cumulonimbus/p/5158794.html