栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

AcWing 1096. 地牢大师

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AcWing 1096. 地牢大师

题目:

1096. 地牢大师 - AcWing题库

分析:

三维数组bfs

知识:数组模拟队列,bfs。

Debug历程:

1. 结构体定义时,字母顺序颠倒。

2. 数组越界,数组开小了。

代码:
#include 
#include 
#include 
#include 

using namespace std;

const int N = 150;

int ans;
int l, n, m;
char road[N][N][N];
bool vis[N][N][N];
int dz[6] = {0, 0, 0, 0, 1, -1};
int dx[6] = {1, 0, -1, 0, 0, 0};
int dy[6] = {0, 1, 0, -1, 0 ,0};

struct Dat
{
	int z, x, y, s;
}q[1000000];

bool check(int zz, int xx, int yy)
{
	return zz >= 0 && zz < l && xx >= 0 && xx < n && yy >= 0 && yy < m
	&& road[zz][xx][yy] != '#' && !vis[zz][xx][yy];
}

int bfs(int a, int b, int c)
{
	int hh = 0, tt = 0;
	q[0] = {a, b, c, 0};
	
	
	while (hh <= tt)
	{
		auto t = q[hh ++ ];
		
		if (road[t.z][t.x][t.y] == 'E') return t.s;
		for (int i = 0; i < 6; i ++ )
		{
			int zz = t.z + dz[i];
			int xx = t.x + dx[i];
			int yy = t.y + dy[i];
			if (check(zz, xx, yy))
			{
				vis[zz][xx][yy] = true;
				q[++ tt] = {zz, xx, yy, t.s + 1};
			}
		}
	}
	
	return 0;
}

int main()
{
	
con:
	while (scanf("%d%d%d", &l, &n, &m))
	{
		if (l == 0 && n == 0 && m == 0) return 0;
		
		memset(vis, 0, sizeof(vis));
		
		for (int i = 0; i < l; i ++ )
			for (int j = 0; j < n; j ++)
				scanf("%s", road[i][j]);
			
		for (int i = 0; i < l; i ++ )
			for (int j = 0; j < n; j ++ )
				for (int k = 0; k < m; k ++ )
					if (road[i][j][k] == 'S')
					{
						vis[i][j][k] = true;
						ans = bfs(i, j, k);
						if (ans) printf("Escaped in %d minute(s).n", ans);
						else puts("Trapped!");
						goto con;
					} 
			
	}
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/702460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号