8数码,无解情况为逆序数为奇数,用康托展开压缩成一个int来判重
这个是多组输入
所以我们不能来一组搜一次
这里给出bfs和A*
A*的话,你可以使用曼哈顿距离,当然,由于不用最少步数,可以用3倍曼哈顿距离加速
IDA*的话用曼哈顿距离就好了
bfs
我们从终点反向搜索所有的点,然后来一组搜一组
#include
#include
#include
#include
using namespace std;
const char direct[4] = { 'd','u','r','l' };
const int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} }, N = 362880;
vector path[N];
bool visit[N];
const int aim = 46233;
const int n = 9;
//阶乘
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[n - 1 - i];
}
}
//从终点逆向搜索,找到所有的可到达局面
void bfs() {
queue q;
q.push(aim);
visit[aim] = true;
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;
q.push(hash);
path[hash] = path[preHash];
path[hash].push_back(direct[i]);
}
state[tz] = state[space];
state[space] = 0;
}
}
}
int main() {
bfs();
char x;
int a[n];
while (~scanf(" %c", &x)) {
if (x == 'x')a[0] = 0;
else a[0] = x - '0';
for (int i = 1; i < n; ++i) {
scanf(" %c", &x);
if (x == 'x')a[i] = 0;
else a[i] = x - '0';
}
int hash = cantor(a);
if (visit[hash]) {
for (int i = path[hash].size() - 1; i >= 0; --i)printf("%c", path[hash][i]);
printf("\n");
}
else printf("unsolvable\n");
}
return 0;
}
A*
h(x)用的是曼哈顿距离
#include
#include
#include
#include
#include
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 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];
while (~scanf(" %c", &x)) {
if (x == 'x')a[0] = 0;
else a[0] = x - '0';
for (int i = 1; i < n; ++i) {
scanf(" %c", &x);
if (x == 'x')a[i] = 0;
else a[i] = x - '0';
}
if (getInv(a) == 1) {
printf("unsolvable\n");
continue;
}
int hash = cantor(a);
memset(visit, 0, sizeof(visit));
memset(g, 0, sizeof(g));
visit[hash] = true;
parent[hash] = -1;
g[hash] = 0;
A_star(hash, h(a));
}
return 0;
}
IDA*
#include
#include
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;
while (~scanf(" %c", &x)) {
if (x == 'x') {
a[0] = 0;
space = 0;
}
else a[0] = x - '0';
for (int i = 1; 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;
}