Dungeon Master POJ - 2251 详解(bfs 广搜)

探讨了3D迷宫中寻找从起点到终点最短路径的问题,采用BFS算法解决,通过修改方向数组实现六向移动,适用于游戏开发、算法优化等场景。

Dungeon Master POJ - 2251

题目

Dungeon Master POJ - 2251
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

分析

理解题意就好做了,就是我们常做的迷宫最短路问题,不同的是这次会有六个方向,不光有前后左右,还要有上下。嗯,没错,只需要改一改方向数组,处理下地图(找到起点和终点),剩下的就是正常的bfs套路了。

具体的解题思路可以看下面的代码,不清楚题意的可以留言~

代码

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

const int maxn = 35;
typedef struct{
	int x,y,z,d;
} P;
int l,r,c;  
int map[maxn][maxn][maxn]; 
int vis[maxn][maxn][maxn]; //访问数组,访问过的就不要再访问啦 
int d[6][3] = {{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}}; //六个方向 
P s,e; //起点,终点 

bool bfs(){
	queue<P> q;
	vis[s.x][s.y][s.z] = 1;
	q.push(s);
	
	while(!q.empty()){
		P p = q.front();
		q.pop();
		
		if(p.x == e.x && p.y == e.y && p.z == e.z){ //终点判定 
			cout<<"Escaped in "<<p.d<<" minute(s)."<<endl;
			return true;
		}
		for(int i = 0;i < 6;i++){ //遍历六个方向 
			int x = p.x + d[i][0]; //新位置 
			int y = p.y + d[i][1];
			int z = p.z + d[i][2];
			int d = p.d + 1; //永远比前一步多1 

			if(x < 1 || x > l || y < 1 || y > r || z < 1 || z > c || !map[x][y][z] || vis[x][y][z]) //边界判定+墙判定+访问判定 
				continue;
			
			vis[x][y][z] = 1; //先到的一定比后到的快啦,所以后面的再访问就没有意义了 
			
			P pp;
			pp.x= x; pp.y = y; pp.z = z; pp.d = d;
			q.push(pp); //新点入队 
		}
		
	}
	return false;
}

int main(){
	
	cin>>l>>r>>c;
	while(l && r && c){
		memset(map,0,sizeof(map)); //初始化 
		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++){
					char c;
					cin>>c;
					if(c == 'S'){ //处理地图,标识障碍物,取得起点和终点 
						s.x = i, s.y = j, s.z = k;
					}else if(c == 'E'){
						map[i][j][k] = 1;
						e.x = i, e.y = j, e.z = k;
					}else if(c == '.'){
						map[i][j][k] = 1;
					}
				}
			}
		}

		if(!bfs()){
			cout<<"Trapped!"<<endl;
		}
		
		cin>>l>>r>>c;
	}	
	return 0;
}
一、数据采集层:多源人脸数据获取 该层负责从不同设备 / 渠道采集人脸原始数据,为后续模型训练与识别提供基础样本,核心功能包括: 1. 多设备适配采集 实时摄像头采集: 调用计算机内置摄像头(或外接 USB 摄像头),通过OpenCV的VideoCapture接口实时捕获视频流,支持手动触发 “拍照”(按指定快捷键如Space)或自动定时采集(如每 2 秒采集 1 张),采集时自动框选人脸区域(通过Haar级联分类器初步定位),确保样本聚焦人脸。 支持采集参数配置:可设置采集分辨率(如 640×480、1280×720)、图像格式(JPG/PNG)、单用户采集数量(如默认采集 20 张,确保样本多样性),采集过程中实时显示 “已采集数量 / 目标数量”,避免样本不足。 本地图像 / 视频导入: 支持批量导入本地人脸图像文件(支持 JPG、PNG、BMP 格式),自动过滤非图像文件;导入视频文件(MP4、AVI 格式)时,可按 “固定帧间隔”(如每 10 帧提取 1 张图像)或 “手动选择帧” 提取人脸样本,适用于无实时摄像头场景。 数据集对接: 支持接入公开人脸数据集(如 LFW、ORL),通过预设脚本自动读取数据集目录结构(按 “用户 ID - 样本图像” 分类),快速构建训练样本库,无需手动采集,降低系统开发与测试成本。 2. 采集过程辅助功能 人脸有效性校验:采集时通过OpenCV的Haar级联分类器(或MTCNN轻量级模型)实时检测图像中是否包含人脸,若未检测到人脸(如遮挡、侧脸角度过大),则弹窗提示 “未识别到人脸,请调整姿态”,避免无效样本存入。 样本标签管理:采集时需为每个样本绑定 “用户标签”(如姓名、ID 号),支持手动输入标签或从 Excel 名单批量导入标签(按 “标签 - 采集数量” 对应),采集完成后自动按 “标签 - 序号” 命名文件(如 “张三
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值