标签:最短路 ann rmi find ems turn desc tps problem
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
分析:题目大意为求得一个3D立体迷宫中从S(起点)走向E(终点)的最短路径长度,BFS,用一个d[][][]三维数组记录长度输出即可
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; typedef struct { int x, y, z; }loc; char a[31][31][31]; int d[31][31][31]; int dx[6] = {0, 0, 0, 0, 1, -1}; int dy[6] = {1, -1, 0, 0, 0, 0}; int dz[6] = {0, 0, 1, -1, 0, 0}; int sx, sy, sz, ex, ey, ez; int l, r, c; int ans; int bfs() { queue<loc> q; loc lo; lo.x = sx; lo.y = sy; lo.z = sz; q.push(lo); while(q.size()) { loc p = q.front(); q.pop(); for(int i = 0; i < 6; i++) { int nx = p.x + dx[i]; int ny = p.y + dy[i]; int nz = p.z + dz[i]; if(nx >= 0 && nx < r && ny >= 0 && ny < c && nz >= 0 && nz < l && a[nz][nx][ny] != ‘#‘) { d[nz][nx][ny] = d[p.z][p.x][p.y] + 1; if(nx == ex && ny == ey && nz == ez) return ans = d[ez][ex][ey]; a[nz][nx][ny] = ‘#‘; lo.x = nx; lo.y = ny; lo.z = nz; q.push(lo); } } } return 0; } int main() { while(scanf("%d%d%d", &l, &r, &c) == 3) { if(!l && !r && !c) break; memset(d, 0, sizeof(d)); for(int i = 0; i < l; i++) { for(int j = 0; j < r; j++) { scanf("%s", a[i][j]); } } for(int i = 0; i < l; i++) { for(int j = 0; j < r; j++) { for(int k = 0; k < c; k++) { if(a[i][j][k] == ‘S‘) { sz = i; sx = j; sy = k; } if(a[i][j][k] == ‘E‘) { ez = i; ex = j; ey = k; } } } } if(bfs()) printf("Escaped in %d minute(s).\n", ans); else printf("Trapped!\n"); } return 0; }
标签:最短路 ann rmi find ems turn desc tps problem
原文地址:https://www.cnblogs.com/kindleheart/p/9102548.html