八数码问题

题目描述
在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字。棋盘中留有一个空格,空格用0来表示。空格周围的棋子可以移到空格中。要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。
输入
输入初试状态,一行九个数字,空格用0表示。
输出
只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数(测试数据中无特殊无法到达目标状态数据)。
样例输入
283104765
样例输出
4
BFS+康拓展开:
dir:每个数字交换的方向(位置)。
zeropos:0的位置,枚举了0的位置再根据交换的方向确定与0交换的那个数字的位置。
numpos:读入的一维数字的二维坐标,也是方面zeropos好计算而设定的。

#include<cstring>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
const int zeropos[9][2]={{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};
const int numpos[3][3]={{0,1,2},{3,4,5},{6,7,8}};
struct node
{
    int dep;        //记录深度 
    char s[10];
} list[100010];
bool hash[362890];
int F[9];
int hashs;
char st[10],goal[10];
int kang(char *str)       //康拓展开 
{
    int a[10],bo[10];
    int k,len,p,s;
    len=strlen(str);
    for (int i=0;i<len;i++) a[i+1]=(str[i]-'0')+1;    //第i个位置表示的数字 
    memset(bo,false,sizeof(bo));
    s=1; k=8;
    for (int i=1;i<=9;i++,k--)
    {
        bo[a[i]]=true;
        p=0;
        for (int j=1;j<=a[i]-1;j++)
            if (!bo[j]) p++;     //找出在i之前比a[i]小的数 
        s+=F[k]*p;    //累乘 
    }
    return s;
}
void bfs()
{
    int head,tail,xx,yy,x,y,zero,temp,len;
    char ch,st2[10];
    head=tail=1;
    strcpy(list[1].s,st); list[1].dep=0;
    while (head<=tail)
    {
        len=strlen(list[head].s);
        for (int i=0;i<len;i++)
            if (list[head].s[i]=='0')
            {
                zero=i; break;       //记录空格 
            }
        x=zeropos[zero][0]; y=zeropos[zero][1];
        for (int i=0;i<=3;i++)
        {
            xx=x+dir[i][0]; yy=y+dir[i][1];   //上下左右移动 
            if (xx>=0 && xx<=2 && yy>=0 && yy<=2)
            {
                temp=numpos[xx][yy];      //所在的坐标 
                strcpy(st2,list[head].s);
                ch=st2[temp]; st2[temp]=st2[zero]; st2[zero]=ch;      //0和temp交换位置 
                temp=kang(st2);
                if (hash[temp]==false)
                {
                    hash[temp]=true;
                    tail++;
                    strcpy(list[tail].s,st2);
                    list[tail].dep=list[head].dep+1;
                }
                if (temp==hashs)
                {
                    tail++;
                    strcpy(list[tail].s,st2);
                    list[tail].dep=list[head].dep+1;
                    printf("%d\n",list[tail].dep);
                    return ;
                }
            }
        }
        head++;
    }
}
int main()
{
    F[0]=1;
    for (int i=1;i<=8;i++) F[i]=F[i-1]*i;     //阶乘 
    scanf("%s",st);
    memset(hash,false,sizeof(hash));
    strcpy(goal,"123804765");       //初始状态 
    hashs=kang(goal);       //康拓展开 
    hash[hashs]=true;
    hash[kang(st)]=true;
    bfs();
    return 0;
}

DFS+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 的偏移量之和了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int dx[5]= {1,0,-1,0};
const int dy[5]= {0,1,0,-1};
const int p[10]= {5,1,2,3,6,9,8,7,4};      //p[i]表示数字i在目标状态中的位置
int s,flag,r[10],a[5][5],map[5][5],dis[10][10];    //r[i]表示数字i在当前状态中的位置
bool check()  {
	for(int i=1; i<=9; i++) if(r[i]!=p[i])return 0;     //没有到达目标状态 
	return 1;
}
int get()  {
	int t=0;
	for(int i=1; i<=9; i++) t+=dis[r[i]][p[i]];      //当前状态到目标状态共需要移动的次数 
	return t;
}
int jue(int a,int b) {
	int t=a-b;
	return t<0?-t:t;
}
int calx(int i) {
	return (i-1)/3+1;
}
int caly(int i) {
	return i%3==0?3:i%3;
}
void dfs(int depth,int x,int y) {   //深搜 
	if(depth+get()>s)  return;
	if(check())  {
		flag=1;
		return;
	}
	for(int i=0; i<4; i++) {
		int xx=x+dx[i],yy=y+dy[i];
		if(xx<1||yy<1||xx>3||yy>3)  continue;
		swap(a[x][y],a[xx][yy]);      //交换两个数 
		swap(r[a[x][y]],r[a[xx][yy]]);     //坐标也交换 
		dfs(depth+1,xx,yy);
		swap(a[x][y],a[xx][yy]);
		swap(r[a[x][y]],r[a[xx][yy]]);
	}
}
void pre() {
	for(int i=1; i<=9; i++)
		for(int j=i+1; j<=9; j++)        //dis[i][j]表示从第i个点到第j个点至少移动的步数
			dis[i][j]=dis[j][i]=calx(j)-calx(i)+jue(caly(i),caly(j));
}
int main() {
	pre();
	int sx,sy;
	for(int i=1; i<=9; i++) {
		char ch=getchar();
		int x=calx(i),y=caly(i);    //坐标 
		map[x][y]=ch-'0';
		r[ch-'0']=i;        //数字在初始的位置 i 
		if(ch=='0')  sx=x,sy=y;     //空格的位置 
	}
	for(s=0;; s++) {
		memcpy(a,map,sizeof(map));
		dfs(0,sx,sy);
		if(flag)  {
			printf("%d\n",s);
			break;
		}
	}
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值