POJ 2251 Dungeon Master(地牢)
2015-12-25 18:29
387 查看
题目链接:POJ 2251
题意:迷宫问题。
分析:
二维变三维,BFS。注意存储下标含义即可。
CODE:
题意:迷宫问题。
分析:
二维变三维,BFS。注意存储下标含义即可。
a[j][k][i] = s[k]; if (s[k] == 'S') { sx = j, sy = k, sz = i; //i相当于z轴,对应L;j相当于x轴,对应C;k相当于y轴,对应R } else if (s[k] == 'E') { ex = j, ey = k, ez = i; }
CODE:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int maxn = 31; char s[maxn], a[maxn][maxn][maxn]; int vis[maxn][maxn][maxn]; int dir[6][3] = { {0,0,1},{0,1,0},{1,0,0},{-1,0,0},{0,-1,0},{0,0,-1} }; int sx, sy, sz, ex, ey, ez, L, R, C; struct Node { int x, y, z; int step; }cur,nextnode; int valid(int x, int y, int z) { if (x < 0 || y < 0 || z < 0 || x >= R || y >= C || z >= L) return 0;//下标越界 if (vis[x][y][z] == 1) return 0;//访问过该点 if (a[x][y][z] == '#') return 0;//遇到墙 return 1; } int bfs() { queue<Node> q; cur.x = sx, cur.y = sy, cur.z = sz; cur.step = 0; q.push(cur); vis[sx][sy][sz] = 1; while (!q.empty()) { cur = q.front(); q.pop(); for (int i = 0;i < 6;i++) { nextnode.x = cur.x + dir[i][0]; nextnode.y = cur.y + dir[i][1]; nextnode.z = cur.z + dir[i][2]; nextnode.step = cur.step + 1; if (!valid(nextnode.x, nextnode.y, nextnode.z)) continue; if (nextnode.x == ex&&nextnode.y == ey&&nextnode.z == ez) return nextnode.step;//找到出口 vis[nextnode.x][nextnode.y][nextnode.z] = 1; //cout << nextnode.x << " " << nextnode.y << " " << nextnode.z << endl; q.push(nextnode); } } return -1; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif while (cin >> L >> R >> C) { if (L == 0 && R == 0 && C == 0) break; for (int i = 0;i < L;i++) { for (int j = 0;j < R;j++) { cin >> s; for (int k = 0;k < C;k++) { a[j][k][i] = s[k]; if (s[k] == 'S') { sx = j, sy = k, sz = i; //cout << sx << " " << sy << " " << sz << endl; } else if (s[k] == 'E') { ex = j, ey = k, ez = i; //cout << ex << " " << ey << " " << ez << endl; } } } } /* for (int i = 0;i < L;i++) { for (int j = 0;j < R;j++) puts(a[i][j]); putchar('\n'); } */ memset(vis, 0, sizeof(vis)); int ans = bfs(); if (ans == -1) cout<<"Trapped!" << endl; else printf("Escaped in %d minute(s).\n", ans); } return 0; }
相关文章推荐
- 使用宏批量将多个csv文件转成excel文件
- tcp 出现rst情况整理
- 带同时滚动小色条的banner轮播图jq
- Android studio 导入lib下的jar文件
- VBA传参之想哭的心
- npoi导出word-webform
- MFC界面相关源码
- dp
- traceroute
- IIS创建ftp服务器和ftp上传发布项目的步骤
- zabbix报PHP gettext extension missing
- MySQL常见错误代码及代码说明
- JavaScript 定时器使用
- MySQL常见错误代码(error code)及代码说明
- 浅谈字段和属性的区别
- 图像滤镜艺术---流行艺术风滤镜特效PS实现
- 从自我学习到深层网络——建立你的第1个深度网络分类器
- 第十七周 12.21---12.27
- java中IO流进行文件的创建和删除
- RedHat搭建IPA-Server