博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-2251-Dungeon Master
阅读量:7052 次
发布时间:2019-06-28

本文共 2281 字,大约阅读时间需要 7 分钟。

题目大意:

三维迷宫,从'S'走到'E','.'可走,'#'不可走。

思路:

BFS 六个方向。

代码:

#include 
#include
#include
#include
using namespace std;const int MAXN = 35;struct Node{ int _l,_r,_c; int _step; Node(int l,int r,int c,int step) { _l = l; _r = r; _c = c; _step = step; }};char Map[MAXN][MAXN][MAXN];int vis[MAXN][MAXN][MAXN];const int Next[6][3] = {
{0,-1,0},{0,0,1},{0,1,0},{0,0,-1},{1,0,0},{-1,0,0}};int l,r,c;int start_l,start_r,start_c;int end_l,end_r,end_c;int main(){ //while (~scanf("%d%d%d",&l,&r,&c)&&l) while (cin >> l >> r >> c && l) { memset(vis,0,sizeof(vis)); for (int i = 1; i <= l; i++) for (int j = 1;j <= r;j++) for (int k = 1;k <= c;k++) { //scanf("%c", &Map[i][j][k]); cin >> Map[i][j][k]; if (Map[i][j][k] == 'S') start_l = i, start_r = j, start_c = k; if (Map[i][j][k] == 'E') end_l = i, end_r = j, end_c = k; } queue
Q; Q.push(Node(start_l,start_r,start_c,0)); vis[start_l][start_r][start_c] = 1; int flag = 0; while (!Q.empty()) { Node now = Q.front(); if (now._l == end_l && now._r == end_r && now._c == end_c) { flag = 1; break; } for (int i = 0;i<6;i++) { int next_l = now._l+Next[i][0]; int next_r = now._r+Next[i][1]; int next_c = now._c+Next[i][2]; if (next_l < 1||next_l > l||next_r < 1||next_r > r||next_c < 1||next_c > c) continue; if (Map[next_l][next_r][next_c] == '#'||vis[next_l][next_r][next_c] == 1) continue; vis[next_l][next_r][next_c] = 1; Q.push(Node(next_l,next_r,next_c,now._step+1)); } Q.pop(); } if (flag) printf("Escaped in %d minute(s).\n",Q.front()._step); else printf("Trapped!\n"); } return 0;}

  

转载于:https://www.cnblogs.com/YDDDD/p/10262663.html

你可能感兴趣的文章
IBM首家发布了公有云中的裸机Kubernetes
查看>>
火掌柜iOS端基于CocoaPods的组件二进制化实践
查看>>
Zabbix Agent端配置文件说明
查看>>
2.10环境变量PATH;2.11cp命令;2.12mv命令;2.13文档查看cat_more...
查看>>
mysql使用索引优化查询效率
查看>>
Salt Syndic配置
查看>>
优秀的开源系统恢复软件
查看>>
IE浏览器低版本判断及升级提示
查看>>
乳腺增生的早期症状?乳腺增生做什么检查最好
查看>>
java B2B2C springmvc mybatis仿淘宝电子商城系统-Hystrix服务容错
查看>>
android学习笔记2 单位
查看>>
[SQL Server][FILESTREAM] -- FileTable Feature in SQL Server 2012
查看>>
svn命令在linux下的使用
查看>>
dig 命令大全 linux
查看>>
Maven Dependencies - miss
查看>>
Mongo Collections
查看>>
Android MVVM开发——DataBinding基础
查看>>
php中file_get_content 和curl以及fopen
查看>>
基于 Pusher 驱动的 Laravel 事件广播(上)
查看>>
fuel部署openstack 打开fuel的UI界面出现白屏的情况
查看>>