主页面:http://blog.csdn.net/dolfamingo/article/details/77825569
代码一:像BFS那样,把棋盘数组放在结构体中。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;
#define AIM 1 //123456789的哈希值为1
struct node
{
int s[9];
int loc;
};
int fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};
int dir[4][2] = { -1,0, 1,0, 0,-1, 0,1 };
char op[4] = {'u', 'd', 'l', 'r' };
int cantor(int s[]) //获得哈希函数值
{
int sum = 0;
for(int i = 0; i<9; i++)
{
int num = 0;
for(int j = i+1; j<9; j++)
if(s[j]<s[i]) num++;
sum += num*fac[8-i];
}
return sum+1;
}
int dis_h(int s[]) //获得曼哈顿距离
{
int dis = 0;
for(int i = 0; i<9; i++)
if(s[i]!=9)
{
int x = i/3, y = i%3;
int xx = (s[i]-1)/3, yy = (s[i]-1)%3;
dis += abs(x-xx) + abs(y-yy);
}
return dis;
}
char path[100];
bool IDAstar(node now, int depth, int pre, int limit)
{
if(dis_h(now.s)==0) //搜到123456789
{
path[depth] = '\0';
puts(path);
return true;
}
int x = now.loc/3;
int y = now.loc%3;
for(int i = 0; i<4; i++)
{
if(i+pre==1 || i+pre==5) continue; //方向与上一步相反, 剪枝
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(xx>=0 && xx<=2 && yy>=0 && yy<=2)
{
node tmp = now;
tmp.s[x*3+y] = tmp.s[xx*3+yy];
tmp.s[xx*3+yy] = 9;
tmp.loc = xx*3+yy;
path[depth] = op[i];
if(depth+1+dis_h(tmp.s)<=limit) //在限制内
if(IDAstar(tmp, depth+1, i, limit))
return true;
}
}
return false;
}
int main()
{
char str[50];
while(gets(str))
{
node now;
int cnt = 0;
for(int i = 0; str[i]; i++)
{
if(str[i]==' ') continue;
if(str[i]=='x') now.s[cnt] = 9, now.loc = cnt++;
else now.s[cnt++] = str[i]-'0';
}
int limit;
for(limit = 0; limit<=100; limit++) //迭代加深搜
if(IDAstar(now, 0, INF, limit))
break;
if(limit>100)
puts("unsolvable");
}
}
代码二:根据DFS的特点,由于棋盘每次只交换一对,所以可以只开一个棋盘数组。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;
int M[100];
int fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};
int dir[4][2] = { -1,0, 1,0, 0,-1, 0,1 };
char op[4] = {'u', 'd', 'l', 'r' };
int cantor(int s[]) //获得哈希函数值
{
int sum = 0;
for(int i = 0; i<9; i++)
{
int num = 0;
for(int j = i+1; j<9; j++)
if(s[j]<s[i]) num++;
sum += num*fac[8-i];
}
return sum+1;
}
int dis_h(int s[]) //获得曼哈顿距离
{
int dis = 0;
for(int i = 0; i<9; i++)
if(s[i]!=9)
{
int x = i/3, y = i%3;
int xx = (s[i]-1)/3, yy = (s[i]-1)%3;
dis += abs(x-xx) + abs(y-yy);
}
return dis;
}
char path[100];
bool IDAstar(int loc, int depth, int pre, int limit)
{
int h = dis_h(M);
if(depth+h>limit)
return false;
if(h==0) //搜到123456789
{
path[depth] = '\0';
puts(path);
return true;
}
int x = loc/3;
int y = loc%3;
for(int i = 0; i<4; i++)
{
if(i+pre==1 || i+pre==5) continue; //方向与上一步相反, 剪枝
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(xx>=0 && xx<=2 && yy>=0 && yy<=2)
{
swap(M[loc], M[xx*3+yy]);
path[depth] = op[i];
if(IDAstar(xx*3+yy, depth+1, i, limit))
return true;
swap(M[loc], M[xx*3+yy]);
}
}
return false;
}
int main()
{
char str[50];
while(gets(str))
{
int cnt = 0, loc;
for(int i = 0; str[i]; i++)
{
if(str[i]==' ') continue;
if(str[i]=='x') M[cnt] = 9, loc = cnt++;
else M[cnt++] = str[i]-'0';
}
int limit;
for(limit = 0; limit<=100; limit++) //迭代加深搜
if(IDAstar(loc, 0, INF, limit))
break;
if(limit>100)
puts("unsolvable");
}
}