八数码问题——A*大法好

【描述】
在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字。棋盘中留有一个空格,空格用0来表示。

空格周围的棋子可以移到空格中。

要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。

【题解】

搜索经典题目,网上各种搞法,于是我就写个A*水一下。。。

主要说一下启发函数的写法:

首先定义一个dis[10][10]数组,记录偏移距离,我们把地图中的点按行标号,则dis[i][j]表示从第i个点到第j个点至少移动的步数。

举个栗子:

点的编号:1 2 3

       4 5 6

       7 8 9

那么显然dis[1][2]=1,dis[2][9]=3,dis[3][4]=3

这个dis数组可通过预处理完成,应该是这个样子:

0 1 2 1 2 3 2 3 4
1 0 1 2 1 2 3 2 3
2 1 0 3 2 1 4 3 2
1 2 3 0 1 2 1 2 3
2 1 2 1 0 1 2 1 2
3 2 1 2 1 0 3 2 1
2 3 4 1 2 3 0 1 2
3 2 3 2 1 2 1 0 1
4 3 2 3 2 1 2 1 0

接着定义一个p[10]数组,p[i]表示数字i在目标状态中的位置,也可以通过预处理完成,

const int p[10]={5,1,2,3,6,9,8,7,4};

然后定义一个r[10]数组,r[i]表示数字i在当前状态中的位置,这个需要动态维护。

那么估价函数的值当然就是数字0-8 的偏移量之和了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<algorithm>
 8 using namespace std;
 9 const int dx[5]={1,0,-1,0};
10 const int dy[5]={0,1,0,-1};
11 const int p[10]={5,1,2,3,6,9,8,7,4};
12 int s,flag,r[10],a[5][5],map[5][5],dis[10][10];
13 bool check()  {for(int i=1;i<=9;i++)if(r[i]!=p[i])return 0;return 1;}
14 int get()  {int t=0;for(int i=1;i<=9;i++)t+=dis[r[i]][p[i]];return t;}
15 int jue(int a,int b){int t=a-b;  return t<0?-t:t;}
16 int calx(int i){return (i-1)/3+1;}
17 int caly(int i){return i%3==0?3:i%3;}
18 void dfs(int depth,int x,int y)
19 {
20     if(depth+get()>s)  return; 
21     if(check())  {flag=1;  return;}
22     for(int i=0;i<4;i++)
23     {
24         int xx=x+dx[i],yy=y+dy[i];
25         if(xx<1||yy<1||xx>3||yy>3)  continue;
26         swap(a[x][y],a[xx][yy]);  swap(r[a[x][y]],r[a[xx][yy]]);
27         dfs(depth+1,xx,yy);
28         swap(a[x][y],a[xx][yy]);  swap(r[a[x][y]],r[a[xx][yy]]);
29     }
30 }
31 void pre()
32 {
33     for(int i=1;i<=9;i++)
34         for(int j=i+1;j<=9;j++)
35             dis[i][j]=dis[j][i]=calx(j)-calx(i)+jue(caly(i),caly(j));
36 }
37 int main()
38 {
39     //freopen("cin.in","r",stdin);
40     //freopen("cout.out","w",stdout);
41     pre();
42     int sx,sy;
43     for(int i=1;i<=9;i++)
44     {
45         char ch=getchar();  int x=calx(i),y=caly(i);
46         map[x][y]=ch-'0';  r[ch-'0']=i;
47         if(ch=='0')  sx=x,sy=y;
48     }
49     for(s=0;;s++)  
50     {
51         memcpy(a,map,sizeof(map));  dfs(0,sx,sy);  
52         if(flag)  {printf("%d
",s);  break;}
53     }
54     return 0;
55 }
原文地址:https://www.cnblogs.com/chty/p/5953493.html