标签:rap print std stream dungeon scan memset size sizeof
三维迷宫,从‘S‘走到‘E‘,‘.‘可走,‘#‘不可走。
BFS 六个方向。
#include <iostream>
#include <string>
#include <queue>
#include <memory.h>
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<Node> 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;
}
标签:rap print std stream dungeon scan memset size sizeof
原文地址:https://www.cnblogs.com/YDDDD/p/10262663.html