HDU 1043 Eight(哈希 + BFS)

问题

HDU 1043 Eight - https://acm.hdu.edu.cn/showproblem.php?pid=1043

分析

  • 题目有多个测试样例(预测在1000以上,不用hash思想,容易 5秒 TLE)
  • 主要思想是:哈希 + BFS(未考虑其它搜索方法)
  • 哈希算法是康托展开
int frac[11]={1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int toCantor(char *x){
	int ret = 0, cnt = 0;
	for(int i = 0; i < 9; ++i){
		for(int j = i+1; j < 9; ++j) cnt += x[i] > x[j];
		ret += cnt*frac[8-i], cnt = 0;
	}
	return ret+1;
}
  • 搜索方法:BFS,且考虑逆向搜索,即考虑 1 2 3 4 5 6 7 8 x 1\quad2\quad 3 \quad4\quad 5\quad 6\quad 7 \quad8\quad x 12345678x 可以变化到什么状态?
  • 数据结构:使用结构体,"X"滑块的位置 ( x , y ) (x,y) (xy),n是状态编号,pre是前继状态编号,m是状态转移动作,即:左、右、上、下
struct node{
	int x, y, n, pre;
	char s[3][3], m;
	void nxt(node *pnd){
		int tx, ty, tmp;
		for(int i = 0; i < 4; ++i){
			tx = x+di[i][0], ty = y+di[i][1];
			if(!isOk(tx, ty)) continue;
			swap(s[x][y], s[tx][ty]);
			tmp = toCantor((char*)s);			
			if(vis[tmp]){swap(s[x][y], s[tx][ty]);continue;}
			vis[tmp] = ++ndCnt;
			node &tn = pnd[ndCnt];
			memcpy(tn.s, s, 9*sizeof(char));
			swap(s[x][y], s[tx][ty]);
			tn.pre = n, tn.x = tx, tn.y = ty, tn.m = dm[i],	tn.n = ndCnt;
			q.push(ndCnt);
		}
	}
}nd[MXN];
  • 读入初始状态,查询是否是末态的变化之一

代码

/* hdu 1043 Eight */
#include<bits/stdc++.h>
using namespace std;
#define MXN 363000
int vis[MXN], ndCnt = 1;
int di[4][2] = {{1, 0},{0, -1},{-1, 0},{0, 1}};
char dm[4] = {'u', 'r', 'd', 'l'};
int frac[11]={1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
queue<int> q;
int toCantor(char *x){
	int ret = 0, cnt = 0;
	for(int i = 0; i < 9; ++i){
		for(int j = i+1; j < 9; ++j) cnt += x[i] > x[j];
		ret += cnt*frac[8-i], cnt = 0;
	}
	return ret+1;
}
void swap(char &x, char &y){ x ^= y, y ^= x, x ^= y;}
bool isOk(int x, int y){return x <= 2 && x >= 0 && y <= 2 && y >= 0;}
struct node{
	int x, y, n, pre;
	char s[3][3], m;
	void nxt(node *pnd){
		int tx, ty, tmp;
		for(int i = 0; i < 4; ++i){
			tx = x+di[i][0], ty = y+di[i][1];
			if(!isOk(tx, ty)) continue;
			swap(s[x][y], s[tx][ty]);
			tmp = toCantor((char*)s);			
			if(vis[tmp]){swap(s[x][y], s[tx][ty]);continue;}
			vis[tmp] = ++ndCnt;
			node &tn = pnd[ndCnt];
			memcpy(tn.s, s, 9*sizeof(char));
			swap(s[x][y], s[tx][ty]);
			tn.pre = n, tn.x = tx, tn.y = ty, tn.m = dm[i],	tn.n = ndCnt;
			q.push(ndCnt);
		}
	}
}nd[MXN];
void bfs(){
	ndCnt = 1;
	while(!q.empty()) q.pop();
	memset(vis, 0, sizeof vis);
	vis[toCantor((char*)nd[1].s)] = 1;
	q.push(1);
	int now;
	while(!q.empty()){
		now = q.front();
		q.pop();
		nd[now].nxt(nd);
	}
}
int main(){
	char str[10];
	int len = 0, tmp;
	nd[1].n = 1, nd[1].x = 2, nd[1].y = 2;
	for(int i = 0; i <= 8; ++i) ((char *)nd[1].s)[i] = (char)(i+'1');
	bfs();
	while(scanf("%c", str+len) == 1){
		if(str[len] == 'x' || str[len] == 'X') str[len++] = '9';
		else if(str[len] > '0' && str[len] < '9') ++len;
		if(len < 9) continue;
		tmp = toCantor(str);
		if(vis[tmp]){
			tmp = vis[tmp];
			while(tmp != 1){
				printf("%c",nd[tmp].m);
				tmp = nd[tmp].pre;
			}
			printf("\n");
		}
		else printf("unsolvable\n");
		len = 0;
	}	
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jpphy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值