UVA532 Dungeon Master(三维BFS)
裸搜索,就摁搜。
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
char mp[31][31][31];
bool vis[31][31][31];
int dir[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};
int l, m, n;
struct node
{
int x, y, z, t;
};
node s, e;
int ans;
void bfs()
{
queue<node> q;
q.push(s);
vis[s.z][s.x][s.y] = 1;
while(q.size())
{
node now = q.front();
q.pop();
if(now.x == e.x && now.y == e.y && now.z == e.z)
{
ans = now.t;
break;
}
for(int i = 0; i < 6; i++)
{
int nx = now.x + dir[i][0], ny = now.y + dir[i][1], nz = now.z + dir[i][2];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && nz >= 1 && nz <= l && mp[nz][nx][ny] != '#' && !vis[nz][nx][ny])
{
node nxt;
nxt.x = nx, nxt.y = ny, nxt.z = nz, nxt.t = now.t + 1;
vis[nz][nx][ny] = 1;
q.push(nxt);
}
}
}
return;
}
int main()
{
freopen("data.txt", "r", stdin);
while(scanf("%d%d%d", &l, &n, &m) && l && m && n)
{
ans = 0x3f3f3f3f;
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= l; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%s", mp[i][j] + 1);
}
}
for(int i = 1; i <= l; i++)
{
for(int j = 1; j <= n; j++)
{
for(int k = 1; k <= m; k++)
{
if(mp[i][j][k] == 'S')
{
s.z = i, s.x = j, s.y = k;
s.t = 0;
}
else if(mp[i][j][k] == 'E')
{
e.z = i, e.x = j, e.y = k;
}
}
}
}
bfs();
if(ans != 0x3f3f3f3f) cout << "Escaped in " << ans << " minute(s).";
else cout << "Trapped!";
cout << endl;
}
return 0;
}
分类:
算法基础—搜索
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-02-22 2019CSP-S T1格雷码