poj1077 /hdoj 1043 Eight

博客详细探讨了ACM竞赛中的poj1077和hdoj1043两道题目,采用启发式搜索策略进行求解。在北大在线评测系统上实现通过,但在杭电评测系统中因数据规模导致超时,暗示杭电数据集可能更为严格。

这道题用启发式搜索,我在北大网站上可以过,杭电网站上超时,看来杭电的数据有点变态了。



#include <stdio.h>
#include <set>
#include <queue>
#include <string.h>
using namespace std;

int target_pos[10][2];

char buf[100];

struct state
{
	int pos_i, pos_j;
	unsigned char m[4][4];
	int step;
	int weight;
	char solution[100];
};
priority_queue<struct state> pq;
set<unsigned int> hash_table;

bool operator < (const struct state &a, const struct state &b)
{
	if(a.weight > b.weight)
		return true;
	else if(a.weight < b.weight)
		return false;
	else
	{
		if(a.step > b.step)
			return true;
		else
			return false;
	}
}


unsigned int _hash(const struct state &sta)
{
	unsigned int sum;
	int cifang, i, j;

	cifang = 1;
	sum = 0;
	for(i=1; i<=3; i++)
	for(j=1; j<=3; j++)
	{
		sum += cifang * sta.m[i][j];
		cifang *= 10;
	}

	return sum;
}

void init_target_pos()
{
	target_pos[1][0] = 1;
	target_pos[1][1] = 1;

	target_pos[2][0] = 1;
	target_pos[2][1] = 2;

	target_pos[3][0] = 1;
	target_pos[3][1] = 3;

	target_pos[4][0] = 2;
	target_pos[4][1] = 1;

	target_pos[5][0] = 2;
	target_pos[5][1] = 2;

	target_pos[6][0] = 2;
	target_pos[6][1] = 3;

	target_pos[7][0] = 3;
	target_pos[7][1] = 1;

	target_pos[8][0] = 3;
	target_pos[8][1] = 2;

	target_pos[9][0] = 3;
	target_pos[9][1] = 3;
}

int _abs(int x)
{
	return x>0 ? x : -1*x;
}

void get_weight(struct state &sta)
{
	int i, j;
	int sum;

	sum = 0;
	for(i=1; i<=3; i++)
	for(j=1; j<=3; j++)
	{
		if(sta.m[i][j] <= 8)
		{
			sum += _abs(i-target_pos[sta.m[i][j]][0]);
			sum += _abs(j-target_pos[sta.m[i][j]][1]);
		}
	}

	sta.weight = sum+sta.step;
}

bool find_solution;

void print_arr(const struct state &sta)
{
	int i, j;

	for(i=1; i<=3; i++)
	{
		for(j=1; j<=3; j++)
			printf("%d ", sta.m[i][j]);
		printf("\n");
	}
}

void bfs()
{
	struct state sta, sta_top;
	int hv;

	while(!pq.empty())
	{
		memcpy(&sta_top, &(pq.top()), sizeof(struct state));
		pq.pop();
		//printf("step=%d weight=%d\n", sta_top.step, sta_top.weight);
		//print_arr(sta_top);

		if(_hash(sta_top) == 987654321)
		{
			sta_top.solution[sta_top.step+1] = '\0';
			printf("%s\n", sta_top.solution+1);
			find_solution = true;
			break;
		}

		memcpy(&sta, &sta_top, sizeof(struct state));
		if(sta.pos_i >= 2)
		{
			sta.m[sta.pos_i][sta.pos_j] = sta.m[sta.pos_i-1][sta.pos_j];
			sta.m[sta.pos_i-1][sta.pos_j] = 9;
			hv = _hash(sta);
			if(hash_table.find(hv) == hash_table.end())
			{
				sta.pos_i--;
				sta.step++;
				sta.solution[sta.step] = 'u';
				get_weight(sta);
				hash_table.insert(hv);
				pq.push(sta);
			}
		}

		memcpy(&sta, &sta_top, sizeof(struct state));
		if(sta.pos_i <= 2)
		{
			sta.m[sta.pos_i][sta.pos_j] = sta.m[sta.pos_i+1][sta.pos_j];
			sta.m[sta.pos_i+1][sta.pos_j] = 9;
			hv = _hash(sta);
			if(hash_table.find(hv) == hash_table.end())
			{
				sta.pos_i++;
				sta.step++;
				sta.solution[sta.step] = 'd';
				get_weight(sta);
				hash_table.insert(hv);
				pq.push(sta);
			}
		}

		memcpy(&sta, &sta_top, sizeof(struct state));
		if(sta.pos_j >= 2)
		{
			sta.m[sta.pos_i][sta.pos_j] = sta.m[sta.pos_i][sta.pos_j-1];
			sta.m[sta.pos_i][sta.pos_j-1] = 9;
			hv = _hash(sta);
			if(hash_table.find(hv) == hash_table.end())
			{
				sta.pos_j--;
				sta.step++;
				sta.solution[sta.step] = 'l';
				get_weight(sta);
				hash_table.insert(hv);
				pq.push(sta);
			}
		}

		memcpy(&sta, &sta_top, sizeof(struct state));
		if(sta.pos_j <= 2)
		{
			sta.m[sta.pos_i][sta.pos_j] = sta.m[sta.pos_i][sta.pos_j+1];
			sta.m[sta.pos_i][sta.pos_j+1] = 9;
			hv = _hash(sta);
			if(hash_table.find(hv) == hash_table.end())
			{
				sta.pos_j++;
				sta.step++;
				sta.solution[sta.step] = 'r';
				get_weight(sta);
				hash_table.insert(hv);
				pq.push(sta);
			}
		}
	}
}


void func(struct state &sta)
{
	init_target_pos();
	hash_table.clear();
	while(!pq.empty())
		pq.pop();

	get_weight(sta);
	hash_table.insert(_hash(sta));
	pq.push(sta);

	find_solution = false;
	bfs();
	if(!find_solution)
		printf("unsolvable\n");
}

int main(void)
{
	int i;
	int ii, jj;
	struct state sta;

	//freopen("input.dat", "r", stdin);

	while(gets(buf))
	{
		if(buf[0] == '\0')
			break;

		ii = jj = 1;
		for(i=0; i<=strlen(buf); i++)
		{
			if(buf[i]>='1'&&buf[i]<='8')
			{
				sta.m[ii][jj] = buf[i]-'0';
				jj++;
				if(jj == 4)
				{
					ii++;
					jj = 1;
				}
			}
			else if(buf[i] == 'x')
			{
				sta.m[ii][jj] = 9;
				sta.pos_i = ii;
				sta.pos_j = jj;
				jj++;
				if(jj == 4)
				{
					ii++;
					jj = 1;
				}
			}


		}
		sta.step = 0;
		func(sta);
	}

	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、付费专栏及课程。

余额充值