POJ-2251 Dungeon Master
程序员文章站
2022-06-12 10:23:57
...
POJ-2251 Dungeon Master
题目链接:POJ-2251
题目大意:三维迷宫最短路径
解题思路:就是bfs 然后6个方向 注意走过的点变墙就行了
#include<iostream>
#include<queue>
using namespace std;
char arrA[35][35][35]={'\0'};
int l,r,c;
int sum = 0;
int xxx[6]={0,0,1,0,0,-1};
int yyy[6]={0,1,0,0,-1,0};
int zzz[6]={1,0,0,-1,0,0};
bool isU = false;
void bfs(int x, int y, int z);
typedef struct myNode{
int x;
int y;
int z;
int step;
} node;
int main(){
while(cin>>l>>r>>c){
if(l + r + c == 0) return 0;
int beginX, beginY, beginZ;
for(int i = 1; i <= l; i++){
for(int j = 1; j <= r; j++){
for(int k = 1; k <= c; k++){
cin>>arrA[i][j][k];
if(arrA[i][j][k] == 'S'){
beginX = i;
beginY = j;
beginZ = k;
}
}
}
}
arrA[beginX][beginY][beginZ] = '#';
bfs(beginX, beginY, beginZ);
if(isU)cout<<"Escaped in "<<sum<<" minute(s)."<<endl;
else cout<<"Trapped!"<<endl;
sum = 0;
isU = false;
}
return 0;
}
void bfs(int x, int y, int z){
queue<node> queueA;
node n1;
n1.x = x;
n1.y = y;
n1.z = z;
n1.step = 0;
queueA.push(n1);
while(queueA.size()){
if(arrA[queueA.front().x][queueA.front().y][queueA.front().z] == 'E'){
sum = queueA.front().step;
isU = true;
break;
}
sum++;
for(int i = 0; i < 6; i++){
int xx = queueA.front().x + xxx[i];
int yy = queueA.front().y + yyy[i];
int zz = queueA.front().z + zzz[i];
if(xx >= 1 && xx <= l && yy >= 1 && yy <= r && zz >= 1 && zz <= c && arrA[xx][yy][zz] != '#'){
if(arrA[xx][yy][zz] == '.') arrA[xx][yy][zz] = '#';
n1.x = xx;
n1.y = yy;
n1.z = zz;
n1.step = queueA.front().step + 1;
queueA.push(n1);
}
}
queueA.pop();
}
}
推荐阅读
-
罗技发布旗舰无线鼠标MX Master 3:全新人体工学外形、电磁不锈钢滚轮
-
sqlserver2005 master与msdb数据库备份恢复过程
-
master.exe是什么进程 有什么用 master进程查询
-
MongoDb的"not master and slaveok=false"错误及解决方法
-
Angularjs ocLazyLoad-master应用
-
MYSQL5.6.33数据库主从(Master/Slave)同步安装与配置详解(Master-Linux Slave-windows7)
-
java 工作流项目源码 SSM 框架 Activiti-master springmvc 有手机端功能
-
顶配17999元!雷神推水冷主机Master:i7-9700K+2080Ti
-
SQL server无法禁用xx已将数据库存上下文更改成为master2002错误解决方法
-
Dungeon Master POJ - 2251 (搜索)