[POJ - 2251Dungeon Master ]
还是迷宫只是从之前的二维变成了三维而已。
#include <cstring> #include <cstdio> #include <queue> #include <iostream> using namespace std; struct test { int x,y,z; int step; }; int direction[6][3]={1,0,0, 0,1,0, 0,0,1, -1,0,0, 0,-1,0, 0,0,-1}; int main() { int xi, yi, zi; queue<test> q; test temp, start, end; while(~scanf("%d%d%d", &zi,&yi,&xi) && zi+yi+xi) { while(!q.empty()) q.pop(); char s[31][31][31]; int vis[31][31][31]; memset(vis, 0, sizeof(vis)); for(int i = 0;i < zi;i++) for(int j = 0; j< yi;j++) for(int k = 0; k<xi;k++) { cin>>s[i][j][k]; if(s[i][j][k] == 'S') { start.x = k; start.y = j; start.z = i; } else if(s[i][j][k] == 'E') { end.x = k; end.y = j; end.z = i; } } // printf("start: %d %d %d\nend: %d %d %d", start.z, start.y, start.x, end.z, end.y, end.x); start.step = 0; q.push(start); vis[start.z][start.y][start.x] = 1; int ans = 0; while(!q.empty()) { start = q.front(); if(start.z == end.z && start.x == end.x && start.y == end.y) {ans = start.step; break; } q.pop(); for(int i = 0; i< 6; i++) { temp.z = start.z + direction[i][0]; temp.y = start.y + direction[i][1]; temp.x = start.x + direction[i][2]; if(temp.x <xi&&temp.x>=0&&temp.y <yi&&temp.y>=0&&temp.z>=0&&temp.z<zi&&!vis[temp.z][temp.y][temp.x]&&s[temp.z][temp.y][temp.x] != '#') { temp.step = start.step+1; // printf("temp: %d %d %d %d\n", temp.z, temp.y, temp.x, start.step); q.push(temp); vis[temp.z][temp.y][temp.x] = 1; } } } if(ans) printf("Escaped in %d minute(s).\n", ans); else printf("Trapped!\n"); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人