Dungeon Master POJ - 2251(三维bfs)

int dx[] = {1,0,0,-1,0,0};
int dy[] = {0,1,0,0,-1,0};
int dz[] = {0,0,1,0,0,-1};
char mp[35][35][35];
bool vis[35][35][35];
int sx, sy, sz, ex, ey, ez,ans;
int l, r, c;
struct node {
	int x, y, z, len;
};
bool check(node e)
{
	if (e.x<1 || e.y<1 || e.z<1 || e.x>l || e.y>r || e.z>c)return false;
	if (mp[e.x][e.y][e.z] == '#')return false;
	if (vis[e.x][e.y][e.z])return false;
	return true;
}
void bfs()
{
	queue<node> q;
	q.push(node{ sx,sy,sz,0 });vis[sx][sy][sz] = true;
	while (!q.empty())
	{
		node now = q.front();q.pop();
		f(i, 0, 5)
		{
			node nextnode = node{ now.x + dx[i],now.y + dy[i],now.z + dz[i],now.len+1 };
			if (check(nextnode))
			{
				q.push(nextnode);
				if (nextnode.x == ex && nextnode.y == ey && nextnode.z == ez) { ans = nextnode.len;return; }
				//cout << nextnode.x << " " << nextnode.y << " " << nextnode.z << endl;
				vis[nextnode.x][nextnode.y][nextnode.z] = true;
			}
		}
	}
	return;
}
int main() 
{
	while (cin >> l >> r >> c)
	{
		if (l == 0 && r == 0 && c == 0)break;
		memset(vis, false, sizeof vis);
		f(i, 1, l)
		{
			f(j, 1, r)
			{
				scanf("%s", mp[i][j] + 1);
				f(k, 1, c)if (mp[i][j][k] == 'S') { sx = i, sy = j, sz = k;break; }
				f(k, 1, c)if (mp[i][j][k] == 'E') { ex = i, ey = j, ez = k;break; }
			}
		}
		ans = 0;bfs();
		if (ans == 0) printf("Trapped!\n");
		else  printf("Escaped in %d minute(s).\n", ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值