标签:character clu std making tac mem one als led
Escaped in x minute(s).
Trapped!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0Sample Output
Escaped in 11 minute(s). Trapped!
题意 : 可以向上下左右 上一层或下一层图走 , 问最少的步数 ?
思路:求最短路通常采用 BFS , 一圈一圈的搜 , 肯定是先找到最近的路,建立一个记录步数的数组 ,记录到每个点的步数。
注意 : 存图 一定要用 char 来存 !!! 经常写成 int 型的
代码示例 :
/* * Author: ry * Created Time: 2017/10/24 20:30:13 * File Name: 1.cpp */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <time.h> using namespace std; const int eps = 1e6+5; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; #define Max(a,b) a>b?a:b #define Min(a,b) a>b?b:a #define ll long long char mp[35][35][35]; int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}}; int l, n, m; int sx, sy, sz, gx, gy, gz; int pre[35][35][35]; struct node { int x, y, z; }; bool check(int x, int y, int z){ if (x >= 1 && x <= l && y >= 1 && y <= n && z >= 1 && z <= m && pre[x][y][z] == -1 && mp[x][y][z] != ‘#‘){ return true; } else return false; } void bfs(){ node ff, t; queue<node>que; ff.x = sx, ff.y = sy, ff.z = sz; que.push(ff); pre[sx][sy][sz] = 0; while(!que.empty()){ node v = que.front(); que.pop(); if (v.x == gx && v.y == gy && v.z == gz){ break; } for(int i = 0; i < 6; i++){ t.x = v.x + dir[i][0]; t.y = v.y + dir[i][1]; t.z = v.z + dir[i][2]; if (check(t.x, t.y, t.z)){ pre[t.x][t.y][t.z] = pre[v.x][v.y][v.z] + 1; que.push(t); } } } } int main() { while (~scanf("%d%d%d", &l, &n, &m) && l+n+m){ memset(pre, -1, sizeof(pre)); for(int i = 1; i <= l; i++){ for(int j = 1; j <= n; j++){ scanf("%s", mp[i][j]+1); } } for(int i = 1; i <=l ; i++){ for(int j = 1; j <= n; j++){ for(int k = 1; k <= m; k++){ if (mp[i][j][k] == ‘S‘) { sx = i; sy = j; sz = k; } if (mp[i][j][k] == ‘E‘){ gx = i; gy = j; gz = k; } } } } bfs(); if (pre[gx][gy][gz] != -1) printf("Escaped in %d minute(s).\n", pre[gx][gy][gz]); else { printf("Trapped!\n"); } } return 0; }
标签:character clu std making tac mem one als led
原文地址:http://www.cnblogs.com/ccut-ry/p/7726417.html