A*解法:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define maxn 100100
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int N = 400000;
int vis[N], pre[N];
int fac[] = {1,1,2,6,24,120,720,5040,40320,362880};
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
int goal = 322560;
inline bool inside(int x, int y)
{
return x >= 0 && x < 3 && y >= 0 && y < 3;
}
struct Node
{
int s[3][3];
int h, g, x, y, hash;
bool operator < (const Node& rhs) const
{
return h > rhs.h || (h == rhs.h && g > rhs.g);
}
void get_h()
{
h = 0;
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3; j ++) if(s[i][j])
h += abs((s[i][j] - 1) / 3 - i) + abs((s[i][j] - 1) % 3 - j);
}
void get_hash()
{
hash = 0;
int tmp[10];
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3; j ++)
tmp[i * 3 + j] = s[i][j];
for(int i = 0; i < 9; i ++)
{
int num = 0;
for(int j = 0; j < i; j ++)
if(tmp[j] > tmp[i]) num ++;
hash += fac[i] * num;
}
}
bool isok()
{
int tmp[10], num = 0;
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3; j ++)
tmp[i * 3 + j] = s[i][j];
for(int i = 0; i < 9; i ++)
for(int j = 0; j < i; j ++)
if(tmp[i] && tmp[j] && tmp[j] > tmp[i]) num ++;
return !(num & 1);
}
} b;
void astar(Node st)
{
CLR(vis, -1);
priority_queue<Node> Q;
Node n1, n2;
Q.push(st);
vis[st.hash] = 4;
while(!Q.empty())
{
n1 = Q.top();
Q.pop();
for(int i = 0; i < 4; i ++)
{
n2 = n1;
n2.x += dx[i]; n2.y += dy[i];
if(!inside(n2.x, n2.y)) continue;
swap(n2.s[n1.x][n1.y], n2.s[n2.x][n2.y]);
n2.get_h(); n2.get_hash();n2.g ++;
if(vis[n2.hash] != -1) continue;
Q.push(n2);
vis[n2.hash] = i;
pre[n2.hash] = n1.hash;
if(n2.hash == goal)
return ;
}
}
}
void print_path(int now)
{
if(now == b.hash) return;
print_path(pre[now]);
switch(vis[now])
{
case 0: putchar('l'); break;
case 1: putchar('u'); break;
case 2: putchar('r'); break;
case 3: putchar('d'); break;
}
}
char c[100];
int main()
{
while(gets(c) != NULL)
{
int len = strlen(c);
for(int i = 0, j = 0; i < len; i ++) if(c[i] != ' ')
{
if(c[i] == 'x') b.s[j/3][j%3] = 0, b.x = j/3, b.y = j%3;
else b.s[j/3][j%3] = c[i] - '0';
j ++;
}
b.get_h(); b.get_hash();b.g = 0;
if(!b.isok()) puts("unsolvable");
else
{
astar(b);
print_path(goal);
puts("");
}
}
}
IDA*解法:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define maxn 100
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
int dx[] = {0, -1, 1, 0};
int dy[] = {-1, 0, 0, 1};
int path[10000];
inline bool inside(int x, int y)
{
return x >= 0 && x < 3 && y >= 0 && y < 3;
}
struct Node
{
int s[3][3];
int x, y, dep, h;
bool isok()
{
int tmp[10];
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3; j ++)
tmp[i * 3 + j] = s[i][j];
int num = 0;
for(int i = 0; i < 9; i ++)
for(int j = 0; j < i; j ++)
if(tmp[i] && tmp[j] && tmp[i] < tmp[j]) num ++;
return !(num & 1);
}
void get_h()
{
h = 0;
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3; j ++) if(s[i][j])
h += abs((s[i][j] - 1) / 3 - i) + abs((s[i][j] - 1) % 3 - j);
}
} b;
bool dfs(Node n, int depth)
{
if(n.dep + n.h > depth) return false;
if(n.h == 0) return true;
for(int i = 0; i < 4; i ++)
{
Node m = n;
m.x += dx[i];m.y += dy[i];
if(!inside(m.x, m.y) || (n.dep && path[n.dep - 1] + i == 3)) continue;
path[m.dep ++] = i;
swap(m.s[m.x][m.y], m.s[n.x][n.y]);
m.get_h();
if(dfs(m, depth)) return true;
}
return false;
}
int IDAstar(Node st)
{
int depth = 0;
while(!dfs(st, depth))
depth ++;
return depth;
}
void print_path(int d)
{
for(int i = 0; i < d; i ++)
{
switch(path[i])
{
case 0: putchar('l'); break;
case 1: putchar('u'); break;
case 2: putchar('d'); break;
case 3: putchar('r'); break;
}
}
}
char c[50];
int main()
{
while(gets(c) != NULL)
{
int len = strlen(c);
for(int i = 0, j = 0; i < len; i ++) if(c[i] != ' ')
{
if(c[i] == 'x') b.s[j/3][j%3] = 0, b.x = j/3, b.y = j%3;
else b.s[j/3][j%3] = c[i] - '0';
e.s[j/3][j%3] = (j + 1) % 9;
j ++;
}
if(!b.isok()) puts("unsolvable");
else
{
b.dep = 0;b.get_h();
print_path(IDAstar(b));
puts("");
}
}
}