HDU 1043——八数码的多种解题思路(持续更新)

八数码算是一个比较经典的搜索问题了。

hdu 1043: http://acm.hdu.edu.cn/showproblem.php?pid=1043

poj 1077:  http://poj.org/problem?id=1077

1.朴素广搜+哈希:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

struct Node {
	int s[9];
	int loc;
	int status;
};
queue q;
struct Path {
    int fa;
    char d;
};
Path path[500000];
string s,_ans;
int a[10],F[10];
int jud[500000];
int dir_jud[4][9] = {
	//rlud
	1,1,0,1,1,0,1,1,0,
	0,1,1,0,1,1,0,1,1,
	0,0,0,1,1,1,1,1,1,
	1,1,1,1,1,1,0,0,0
};
int dir[4] ={1,-1,-3,3};
char zifu[10] = {"rlud"};

int Cantor(int a[]) {
	int ans = 0;
	for (int i = 0; i < 9; i++) {
		int t = 0;
		for (int j = 0; j < i; j++) {
			if(a[j]='1'&&s[i]<='9') {
			a[j++] = s[i]-'0';
		}
		else if(s[i]=='x') {
			a[j++] = 9;
		}
	}
}
int check(int a[]) {
	int ans = 0;
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < i; j++) {
			if(a[i] ();
		memset(path,0,sizeof(path));
        memset(jud,0,sizeof(jud));
		read();cal();
		if(check(a)%2) {
			puts("unsolvable");
			continue;
		}    
		Node start;
		for(int i = 0; i < 9; i++) {
			start.s[i] = a[i];
			if(a[i]==9)start.loc = i;
		}
		start.status = Cantor(start.s);
		q.push(start);
		path[start.status].fa = -1;
        jud[start.status] = 1;        
		if(!bfs()) {
			puts("unsolvable");
			continue;
		}
        int i = 0;
        while (path[i].fa!=-1) {
            _ans.push_back(path[i].d);
            i = path[i].fa;
        }
        reverse(_ans.begin(),_ans.end());
        cout << _ans << endl;
	}
	return 0;
}
2.广搜打表+哈希
#include 
#include 
#include 
#include  
#include 
#include 
#define MAXN 362880
using namespace std;
int a[10];
char s[1000];
int F[10];
struct Node{
	int s[9];
	int zero;
	int status;
};
queue q;
struct Path{
	int fa;
	char d;
};
Path path[MAXN];
bool jud[MAXN];
//rlud
char z[10] = {"lrdu"};
int dir[4] = {1,-1,-3,3};
bool pre_d[4][9] = {
	1,1,0,1,1,0,1,1,0,
	0,1,1,0,1,1,0,1,1,
	0,0,0,1,1,1,1,1,1,
	1,1,1,1,1,1,0,0,0
};
int Cantor(int c[]) {
	int ans = 0;
	for (int i = 0;i < 9; i++) {
		int t = 0;
		for (int j = 0; j < i; j++) {
			if (c[j]='1') a[j++] = s[i]-'0';
			else if (s[i]=='x') a[j++] = 9;
		}
		int ans = Cantor(a);
		if(jud[ans]==0) {
			puts("unsolvable");
		}
		else{		
			while(path[ans].fa!=-1) {
				printf("%c",path[ans].d);
				ans=path[ans].fa;
			}
			printf("\n");
		}
	  }
	return 0;
}

3.双向广搜

// #include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
char s[1000];
string ANS[2];
int f[10];
int a[10];
char z[2][10] = {"lrud","rldu"};
int dir[4] = {1,-1,3,-3};
int p_dir[4][9] = {
	1,1,0,1,1,0,1,1,0,
	0,1,1,0,1,1,0,1,1,
	1,1,1,1,1,1,0,0,0,
	0,0,0,1,1,1,1,1,1
};
struct Node {
	int zero, status;
	int s[9];
	Node(){};
	Node(int Z,int S){
		zero = Z, status = S;
	}
};
struct Path {
	int fa; char c;
	Path(){};
	Path(int FA,int C) {
		fa = FA, c = C;
	}
};
Path path[400000];
queue q[2];
int jud[400000];

int Cantor(int a[]) {
	int ans = 0;
	for (int i = 0; i < 9; i++) {
		int t = 0;
		for (int j = 0; j < i; j++)
			if (a[i]>a[j]) t++;
			ans+=f[9-i-1]*(a[i]-1-t);
	}
	return ans;
}
int ca;
void dbfs(){
	while (!q[0].empty()&&!q[1].empty()) {
		for (int j = 0; j < 2; j++) {
			for (int i = 0; i < 4; i++) {
				if (p_dir[i][q[j].front().zero]) {
					Node t = q[j].front();
					swap(t.s[t.zero],t.s[t.zero+dir[i]]);
					t.status = Cantor(t.s);
					if (!jud[t.status]) {
						path[t.status].fa = q[j].front().status;
						path[t.status].c = z[j][i];
						jud[t.status] = j+1;
						t.zero += dir[i];
						q[j].push(t);
					}
					else{
						if (jud[t.status]==2-j) {
							int t1 = q[j].front().status; 
							int t2 = t.status;
							ANS[j]+=z[j][i];
							while (path[t1].fa!=-1) {
								ANS[j]+=path[t1].c;
								t1 = path[t1].fa;
							}
							while (path[t2].fa!=-1) {
								ANS[1-j]+=path[t2].c;
								t2 = path[t2].fa;
							}
							reverse(ANS[1].begin(),ANS[1].end());
							cout << ANS[1]<();
		ANS[0].clear();ANS[1].clear();
		int j = 0, t = 0;
 		for (int i = 0; i < strlen(s); ++i) {
			if (s[i]<='8'&&s[i]>='1') {
				a[j++] = s[i]-'0';
			}
			else if (s[i]=='x'){
				a[j++] = 9;
				t = j-1;
			}
		}
		if(check(a)%2) {
			puts("unsolvable");
			continue;
		}
		int ans = Cantor(a);
		q[1].push(Node(t,ans));
		q[0].push(Node(8,0));
		for (int i = 0 ; i < 9; i++) {
			q[0].front().s[i] = i+1;
			q[1].front().s[i] = a[i];
		}
		path[0].fa = path[ans].fa= -1;
		jud[0] = 1;
		jud[ans] = 2;
		dbfs();
	}
	return 0;
}


注:这题数据好像挺水的,poj无unslovable数据,另外以提交结果看,双向广搜并不快(才不是写的蠢) 


你可能感兴趣的:(HDU 1043——八数码的多种解题思路(持续更新))