标签:scanf eve south cell line ring memset cannot start
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!
三维的bfs, 总共可以向6个方向走,走一步步数+1, 可以按行读取地图, 把z轴设为最高维度, 基本过程和二维的bfs类似
#include <iostream> #include <stdio.h> #include <cstring> #include <queue> using namespace std; char maze[31][31][31]; bool v[31][31][31]; int l, r, c; int dir[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; struct node { int x, y, z, t; }now, Next; int bx, by, bz; int ex, ey, ez; int bfs() { now.x = bx; now.y = by; now.z = bz; now.t = 0; v[bz][bx][by] = 1; queue<node> q; q.push(now); while (!q.empty()) { now = q.front(); q.pop(); if (now.z == ez && now.x == ex && now.y == ey) return now.t; for (int i = 0; i < 6; i++) { Next.z = now.z + dir[i][0]; Next.x = now.x + dir[i][1]; Next.y = now.y + dir[i][2]; Next.t = now.t + 1; if (Next.x>=0&&Next.x<r&&Next.y>=0&&Next.y<c&&Next.z>=0&&Next.z<l && !v[Next.z][Next.x][Next.y] && maze[Next.z][Next.x][Next.y] != ‘#‘) { q.push(Next); v[Next.z][Next.x][Next.y] = 1; } } } return 0; } int main() { //freopen("1.txt", "r", stdin); while (~scanf("%d%d%d", &l, &r, &c)) { if (l+c+r == 0) break; memset(v, 0, sizeof(v)); for (int i = 0; i < l; i++) for (int j = 0; j < r; j++) scanf("%s", maze[i][j]); for (int i = 0; i < l; i++) for (int j = 0; j < r; j++) for (int k = 0; k < c; k++) { if (maze[i][j][k] == ‘S‘) { bz = i; bx = j; by = k; } if (maze[i][j][k] == ‘E‘) { ez = i; ex = j; ey = k; } } int ans = bfs(); if (ans == 0) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n", ans); } return 0; }
标签:scanf eve south cell line ring memset cannot start
原文地址:http://www.cnblogs.com/whileskies/p/7181417.html