uva532

来源:互联网 发布:airplay软件下载 编辑:程序博客网 时间:2024/05/21 12:46

题意:

三维空间,从s走到e,遇到#不能走,求最短路径。

最短路劲用BFS

代码:

#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn = 30+5;int L,R,C;struct state {int x, y, z;state(int a, int b, int c) {x = a;y = b;z = c;}};char maze[maxn][maxn][maxn];int v[maxn][maxn][maxn],d[maxn][maxn][maxn];const int d1[] = {1, -1, 0, 0, 0, 0};const int d2[] = {0, 0, 1, -1, 0, 0};const int d3[] = {0, 0, 0, 0, 1, -1};int dfs(int l, int r, int c) {int l2, r2, c2, i, j, k;queue<state> q;d[l][r][c] = 0;v[l][r][c] = 1;q.push(state(l,r,c));while(!q.empty()) {state s = q.front();q.pop();for(i=0; i<6; i++) {l2 = s.x+d1[i];r2 = s.y+d2[i];c2 = s.z+d3[i];if(l2>=0 && l2<L && r2>=0 && r2<R && c2>=0 && c2<C && maze[l2][r2][c2] != '#' &&!v[l2][r2][c2]) {q.push(state(l2,r2,c2));d[l2][r2][c2] = d[s.x][s.y][s.z]+1;v[l2][r2][c2] = 1;if(maze[l2][r2][c2] == 'E') return d[l2][r2][c2];}}}return -1;}int main() {int i, j, k, l1, r1, c1;while(scanf("%d%d%d", &L, &R, &C) && (L+R+C)) {for(k=0; k<L; k++) {for(i=0; i<R; i++) {scanf("%s", maze[k][i]);for(j=0; j<C; j++) {if(maze[k][i][j] == 'S') {l1 = k;r1 = i;c1 = j;}}}}memset(v,0,sizeof(v));int ans =  dfs(l1,r1,c1);if(ans >= 0) printf("Escaped in %d minute(s).\n", ans);else printf("Trapped!\n");}return 0;}


0 0
原创粉丝点击