八数码与A*算法

八数码

状态搜索问题

八数码问题也称为九宫问题。在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同。棋盘上还有一个空格,与空格相邻的棋子可以移到空格中。要求解决的问题是:给出一个初始状态和一个目标状态,找出一种从初始转变成目标状态的移动棋子步数最少的移动步骤。

我们从空格处移动,也就是初始状态A,可以按上下左右顺序移动,之后再将这四个改变的棋盘再拓展,直到目标状态F。在此中间,我们还要判断这个状态是否与之前的状态重复,一是减少时间复杂度,二是使我们的代码更加完善。
这里要用到康托展开来快速判重。
康托展开
我们按某种规则将一个数的全排列排序,然后依次写上序号
比如三的全排列(按照字典序)
123 —1
132 —2
213 —3
231 —4
312 —5
321 —6
从上面可以看出 312是第5位

在八码数里,我们就可以利用康托展开来判重

拓展知识:
康托逆展开
输入一个数n,返回第n大的排列

下面是独立完成的八数码代码
(以后写程序会加上注释,不然回头自己看时候自己都不知道什么意思)

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long  ll;
#define pi acos(-1.0)
#define INF 0x7fffffff
#define mem(x) memset(x,INF,sizeof(x))
#define rep(m,n,h) for(int m=n;m<=h;m++)
#define fo(m,n,h) for(int m=n;m<h;m++)
#define cls(x) memset(x,0,sizeof(x))
#define IOS  std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int N =362880;//9的阶乘
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};//按上下左右的方向
int visited[N+50]={0};//每个状态的记录,查看是否用过
int factorial[]={1,1,2,6,24,120,720,5040,40320,362880};//从0到9的阶乘,在康托展开时要用到
struct node{
int state[9];
int dis;
};//记录一个状态和到初始状态的步数
bool Cantor(int *str,int n){//康托展开判重
    int result=0;
    for(int i=0;i<n;i++){
        int counted=0;
        for(int j=i+1;j<n;j++){
            if(str[i]>str[j]){
                counted++;
            }
        }
        result+=counted*factorial[n-i-1];
    }
    if(!visited[result]){
        visited[result]=1;
        return 1;
    }
    return 0;
}
int start[9],goal[9];
int BFS(){
    node head;
    head.dis=0;
    memcpy(head.state,start,sizeof(head.state));//复制初始状态的值
    Cantor(head.state,9);//判重,将初始状态的visited[]赋值为1
    queue<node> q;
    q.push(head);
    while(!q.empty()){
        head=q.front();
        if(memcmp(head.state,goal,sizeof(goal))==0)//判断是否与目标状态一致
            return head.dis;
        q.pop();
       int k;
       for(k=0;k<9;k++)//找到该状态0所处的位置
            if(head.state[k]==0)
            break;
       int x=k%3;//所在的行
       int y=k/3;//所在的列
       for(int i=0;i<4;i++){
           int newx=x+dir[i][0];
           int newy=y+dir[i][1];
           int newk=newx+3*newy;//转一维坐标
        if(newx>=0&&newx<3&&newy>=0&&newy<3){//未出界
            node newhead;
            memcpy(&newhead,&head,sizeof(struct node));//复制这个新的状态
            swap(newhead.state[k],newhead.state[newk]);
            newhead.dis++;
            if(Cantor(newhead.state,9))//康托展开来判重
                q.push(newhead);
        }
       }
    }
    return -1;
}
int main()
{
    //freopen("C:\\Users\\lenovo\\Desktop\\in.txt","r",stdin);
    IOS
    for(int i=0;i<9;i++)
        cin>>start[i];
    for(int i=0;i<9;i++)
        cin>>goal[i];
    int num=BFS();
    if(num!=-1)
        cout<<num<<endl;
    else
        cout<<"Impossible"<<endl;
    return 0;
}

这里还有一个之前没用过的函数
memcmp函数
memcmp(a, b):1 //字符串a>字符串b, 返回1
memcmp(a, c):-1 // 字符串a<字符串c, 返回-1
memcmp(a, d):0 //字符串a=字符串d, 返回0

A*算法

这个算法目前还是没太明白
是一个启发式搜索
用一个评估函数对当前情况进行评估,得到最好的状态,然后从这个状态进行搜索直到目标
f(x)=g(x)+h(x)
g(x)表示从初始状态到x的实际代价
h(x)表示x到终点最优路径的评价
可以说A*算法是BFS和贪心的结合

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值