HDU 1043 Eight(哈希 + BFS)

Posted jpphy0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

以上是关于HDU 1043 Eight(哈希 + BFS)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1043 Eight BFS

hdu 1043 Eight(康托展开 + BFS)

Eight HDU-1043 (bfs)

HDU 1043 Eight(双向BFS+康托展开)

HDU 1043 Eight(反向BFS+打表+康托展开)

Eight HDU - 1043 (双向BFS)