练习1 - Dungeon Master(三维迷宫)
程序员文章站
2022-06-02 22:40:44
...
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int F=3,W=4,H=5; //H为现实中的第几列
char a[F][W][H]={
{
{'S','.','.','.','.'},
{'.','#','#','#','.'},
{'.','#','#','.','.'},
{'#','#','#','.','#'}
},
{
{'#','#','#','#','#'},
{'#','#','#','#','#'},
{'#','#','.','#','#'},
{'#','#','.','.','.'}
},
{
{'#','#','#','#','#'},
{'#','#','#','#','#'},
{'#','.','#','#','#'},
{'#','#','#','#','E'}
}
};
int sx=0,sy=0,sz=0;
int ex=3,ey=4,ez=2;
int x=sx,y=sy,z=sz; //记录当前xyz
//6个方向可以走
int mov[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int head=0,tail=1;
int t=0; //记录时间
struct line{
int x=0;
int y=0;
int z=0;
int t=0;
};
line b[101]; //作line
int g[F][W][H]={0}; //占位
int main()
{
g[0][0][0] = 1; //!!首个占位
while(head < tail){
head++;
for(int i=0;i<6;i++){
x = b[head-1].x + mov[i][0];
y = b[head-1].y + mov[i][1];
z = b[head-1].z + mov[i][2];
if(x>=0 && x<W && y>=0 && y<H && z>=0 && z<F && !g[z][x][y] && a[z][x][y] != '#'){
g[z][x][y] = 1; //到达后,先占领,再发展
b[tail].x = x;
b[tail].y = y;
b[tail].z = z;
b[tail].t = b[head-1].t + 1; //继承时间
tail++;
//在前进后判断目标
if(a[z][x][y] == 'E'){
cout << b[head].t+1; //找寻到了E的上一步,还要再加1步
head = tail; //跳出while
break; //跳出for
}
}
}
}
return 0;
}
//cout << "输入:"
结果:
上一篇: 人参蜂蜜的十大功效你必须要掌握