poj1077

8数码,无解情况为逆序数为奇数,用康托展开压缩成一个int来判重

这个是单组输入

所以我们来一组搜一次

这里给出bfs和A*

A*的话,你可以使用曼哈顿距离,当然,由于不用最少步数,可以用3倍曼哈顿距离加速

IDA*的话,用曼哈顿距离

bfs

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const char direct[4] = { 'u','d','l','r' };
const int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} }, N = 362880;
const int aim = 46233;
const int n = 9;
bool visit[N];
char step[N];
int parent[N];
//阶乘
const int fac[n] = { 1,1,2,6,24,120,720,5040,40320 };
//康托展开
int cantor(int s[]) {
	int result = 0, cnt = 0;
	for (int i = 0; i < n - 1; ++i) {
		cnt = 0;
		for (int j = i + 1; j < n; ++j) {
			if (s[i] > s[j])++cnt;
		}
		result += fac[n - 1 - i] * cnt;
	}
	return result;
}
//逆康托展开
void reverseCantor(int hash, int s[], int &space) {
	bool visited[n] = {};
	int temp;
	for (int i = 0; i < n; ++i) {
		temp = hash / fac[n - 1 - i];
		for (int j = 0; j < n; ++j) {
			if (!visited[j]) {
				if (temp == 0) {
					s[i] = j;
					if (j == 0)space = i;
					visited[j] = true;
					break;
				}
				--temp;
			}
		}
		hash %= fac[8 - i];
	}
}
void bfs(int start) {
	queue<int> q;
	q.push(start);
	visit[start] = true;
	parent[start] = -1;
	int state[n], space;
	while (!q.empty()) {
		int preHash = q.front();
		q.pop();
		reverseCantor(preHash, state, space);
		for (int i = 0; i < 4; ++i) {
			int tx = space / 3 + dir[i][0];
			int ty = space % 3 + dir[i][1];
			if (tx < 0 || tx>2 || ty < 0 || ty>2)continue;
			int tz = tx * 3 + ty;
			state[space] = state[tz];
			state[tz] = 0;
			int hash = cantor(state);
			if (!visit[hash]) {
				visit[hash] = true;
				parent[hash] = preHash;
				step[hash] = direct[i];
				if (hash == aim)return;
				q.push(hash);
			}
			state[tz] = state[space];
			state[space] = 0;
		}
	}
}
void printPath() {
	char queue[N];
	int t = 0;
	int c = aim;
	while (parent[c] != -1) {
		queue[t] = step[c];
		++t;
		c = parent[c];
	}
	for (int i = t - 1; i >= 0; --i)printf("%c", queue[i]);
	printf("\n");
}
int main() {
	char x;
	int a[n];
	for (int i = 0; i < n; ++i) {
		scanf(" %c", &x);
		if (x == 'x')a[i] = 0;
		else a[i] = x - '0';
	}
	int hash = cantor(a);
	bfs(hash);
	if (!visit[aim])printf("unsolvable\n");
	else printPath();
	return 0;
}

A*

曼哈顿距离

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
const char direct[4] = { 'u','d','l','r' };
const int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} }, N = 362880;
const int aim = 46233;
const int n = 9;
bool visit[N];
int parent[N];
char step[N];
int goal_state[9][2] = {
	{0, 0}, {0, 1}, {0, 2},
	{1, 0}, {1, 1}, {1, 2},
	{2, 0}, {2, 1}, {2, 2}
};
//阶乘
const int fac[n] = { 1,1,2,6,24,120,720,5040,40320 };
//康托展开
int cantor(int s[]) {
	int result = 0, cnt = 0;
	for (int i = 0; i < n - 1; ++i) {
		cnt = 0;
		for (int j = i + 1; j < n; ++j) {
			if (s[i] > s[j])++cnt;
		}
		result += fac[n - 1 - i] * cnt;
	}
	return result;
}
//逆康托展开
void reverseCantor(int hash, int s[], int &space) {
	bool visited[n] = {};
	int temp;
	for (int i = 0; i < n; ++i) {
		temp = hash / fac[n - 1 - i];
		for (int j = 0; j < n; ++j) {
			if (!visited[j]) {
				if (temp == 0) {
					s[i] = j;
					if (j == 0)space = i;
					visited[j] = true;
					break;
				}
				--temp;
			}
		}
		hash %= fac[8 - i];
	}
}
//f=g+h
int g[N];
int h(int s[]) {
	int k, result = 0;
	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 3; ++j) {
			k = i * 3 + j;
			if (s[k] == 0)continue;
			result += abs(1.0*(i - goal_state[s[k] - 1][0])) + abs(1.0*(j - goal_state[s[k] - 1][1]));
		}
	}
	return result;
}
void printPath() {
	char queue[31];
	int t = 0;
	int c = aim;
	while (parent[c] != -1) {
		queue[t] = step[c];
		++t;
		c = parent[c];
	}
	for (int i = t - 1; i >= 0; --i)printf("%c", queue[i]);
	printf("\n");
}
class Chess {
public:
	int hash, f;
	Chess(const int &hash, const int &f) :hash(hash), f(f) {}
	bool operator <(const Chess &t)const {
		if (f == t.f)return g[hash] > g[t.hash];
		return f > t.f;
	}
};
//A*算法
void A_star(int start, int f) {
	priority_queue<Chess> q;
	q.push(Chess(start, f));
	int preHash, hash, state[n], space, preH;
	while (!q.empty()) {
		//取出节点
		Chess preChess = q.top();
		preHash = preChess.hash;
		if (preHash == aim) {
			printPath();
			return;
		}
		q.pop();
		preH = preChess.f - g[preHash];
		reverseCantor(preHash, state, space);
		for (int i = 0; i < 4; ++i) {
			int tx = space / 3 + dir[i][0];
			int ty = space % 3 + dir[i][1];
			if (tx < 0 || tx>2 || ty < 0 || ty>2)continue;
			int tz = 3 * tx + ty;
			state[space] = state[tz];
			state[tz] = 0;
			hash = cantor(state);
			//没访问过
			if (!visit[hash]) {
				step[hash] = direct[i];
				g[hash] = g[preHash] + 1;
				visit[hash] = true;
				parent[hash] = preHash;
				q.push(Chess(hash, g[hash] + h(state)));
			}
			//访问过了,但是从preHash走一步到hash比直接走到hash更短
			//else if (g[preHash] + 1 < g[hash]) {
			//	step[hash] = direct[i];
			//	g[hash] = g[preHash] + 1;
			//	parent[hash] = preHash;
			//	q.push(Chess(hash, g[hash] + preH));
			//}
			state[tz] = state[space];
			state[space] = 0;
		}
	}
	printf("unsolvable\n");
}
int getInv(int s[]) {
	int cnt = 0;
	for (int i = 1; i < n; ++i) {
		if (s[i] == 0)continue;
		for (int j = 0; j < i; ++j) {
			if (s[j] == 0)continue;
			if (s[i] < s[j])
				++cnt;
		}
	}
	return cnt & 1;
}
int main() {
	char x;
	int a[n];
	for (int i = 0; i < n; ++i) {
		scanf(" %c", &x);
		if (x == 'x')a[i] = 0;
		else a[i] = x - '0';
	}
	if (getInv(a) == 1) {
		printf("unsolvable\n");
	}
	else {
		int hash = cantor(a);
		visit[hash] = true;
		parent[hash] = -1;
		g[hash] = 0;
		A_star(hash, h(a));
	}
	return 0;
}

IDA*

#include<iostream>
#include<cstdio>
using namespace std;
const char direct[4] = { 'u','d','l','r' };
const int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
const int n = 9;
bool flag;
int a[n];
char step[32];
int maxDeep;
int goal_state[9][2] = {
	{0, 0}, {0, 1}, {0, 2},
	{1, 0}, {1, 1}, {1, 2},
	{2, 0}, {2, 1}, {2, 2}
};
int abs(int a, int b) {
	if (a > b)return a - b;
	return b - a;
}
int h() {
	int k, result = 0;
	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 3; ++j) {
			k = i * 3 + j;
			if (a[k] == 0)continue;
			result += abs(i, goal_state[a[k] - 1][0]) + abs(j, goal_state[a[k] - 1][1]);
		}
	}
	return result;
}
int getInv() {
	int cnt = 0;
	for (int i = 1; i < n; ++i) {
		if (a[i] == 0)continue;
		for (int j = 0; j < i; ++j) {
			if (a[j] == 0)continue;
			if (a[i] < a[j])
				++cnt;
		}
	}
	return cnt & 1;
}
bool reverse(char a, char b) {
	return a == 'u'&& b == 'd'
		|| a == 'd'&& b == 'u'
		|| a == 'l'&& b == 'r'
		|| a == 'r'&&b == 'l';
}
int dfs(int deep, int space) {
	int t = h(), f = deep + t;
	if (f > maxDeep)return f;
	if (t == 0) {
		flag = true;
		return f;
	}
	int nextBound = 32;
	for (int i = 0; i < 4; ++i) {
		if (deep > 0 && reverse(step[deep - 1], direct[i]))continue;
		int tx = space / 3 + dir[i][0];
		int ty = space % 3 + dir[i][1];
		if (tx < 0 || tx>2 || ty < 0 || ty>2)continue;
		int tz = 3 * tx + ty;
		a[space] = a[tz];
		a[tz] = 0;
		step[deep] = direct[i];
		f = dfs(deep + 1, tz);
		if (flag)return f;
		if (f < nextBound)nextBound = f;
		a[tz] = a[space];
		a[space] = 0;
	}
	return nextBound;
}
int main() {
	char x;
	int space;
	for (int i = 0; i < n; ++i) {
		scanf(" %c", &x);
		if (x == 'x') {
			a[i] = 0;
			space = i;
		}
		else a[i] = x - '0';
	}
	if (getInv() == 1) {
		printf("unsolvable\n");
	}
	else {
		maxDeep = h();
		while (true) {
			flag = false;
			maxDeep = dfs(0, space);
			if (flag) {
				step[maxDeep] = '\0';
				printf("%s\n", step);
				break;
			}
		}
	}
	return 0;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Nightmare004

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

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

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

打赏作者

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

抵扣说明:

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

余额充值