八数码的启发式搜索

虽然在上学期就看过了有关启发式搜索的资料,可是一直没自己亲自动手写过。。。今天借着hiho的机会写了一个关于八数码的启发式搜索程序

#include <stdio.h>
#include <iostream>
#include<math.h>
#include<ctime>
#include<stack>
#include<algorithm>
#include<queue>
#include<set>
#include <string.h>
using namespace std;
typedef long long LL;
int jiecheng[9]= {0,1},pos[9][2]= {2,2,0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1},to[4][2]= {0,-1,0,1,1,0,-1,0};
const int ans=46233;//最终状态的序号
bool closelist[400000];
struct node {
    int num[3][3],x,y,f,step;//与bgs的第一个不同点————拥有f值
    friend bool operator < (const node &a,const node &b) {
        return a.f>b.f;
    }
} root;
void Jiecheng() {
    for(int i=2; i<=8; i++) jiecheng[i]=jiecheng[i-1]*i;
}
int cantor(int num[][3]) {//康拓展开,计算这个排列是全排列的第几个数。在这里相当于一个完美hash
    int x=0,t;
    for(int i=0; i<3; i++) {
        for(int j=0; j<3; j++) {
            t=0;
            for(int k=i*3+j+1; k<=8; k++)if(num[i][j]>num[k/3][k%3]) t++;
            x+=jiecheng[8-i*3-j]*t;
        }
    }
    return x;
}
bool check(int num[][3]) { //用逆序数奇偶性快速判断不可能的状况
    int x=0;
    for(int i=0; i<3; i++) {
        for(int j=0; j<3; j++) {
            if(num[i][j]==0) continue;
            for(int k=i*3+j+1; k<=8; k++) {
                if(num[i][j]>num[k/3][k%3]&&num[k/3][k%3]) x++;
            }
        }
    }
    return x%2;
}
int val(int num[][3]) {//计算启发式搜索的一个关键函数————评估函数
    int tot=0;
    for(int i=0; i<3; i++)
        for(int j=0; j<3; j++)
            tot+=abs(pos[num[i][j]][0]-i)+abs(pos[num[i][j]][1]-j);
    return tot;
}
int bfs() {
    priority_queue<node> Q;//与bfs的第二个不同点,需要用优先队列。f值最小的先取出
    Q.push(root);
    if(cantor(root.num)==ans) return 0;
    while(!Q.empty()) {
        node p=Q.top();
        closelist[cantor(p.num)]=1;//这是和bfs的第三个不同点,在这里标记已经走过的状态
        Q.pop();
        p.step++;
        int x=p.x,y=p.y;
        for(int i=0; i<4; i++) {
            int xx=x+to[i][0],yy=y+to[i][1];
            if(xx<0||xx>2||yy<0||yy>2) continue;
            swap(p.num[x][y],p.num[xx][yy]);
            int  id=cantor(p.num);
            if(id==ans)return p.step;
            if(closelist[id]==0) {
                p.x=xx;
                p.y=yy;
                p.f=p.step+val(p.num);
                Q.push(p);
            }
            //将改变还原
            p.x=x;
            p.y=y;
            swap(p.num[p.x][p.y],p.num[xx][yy]);
        }
    }
}
int main() {
    freopen("input.txt","r",stdin);
    Jiecheng();//阶乘打表
    int t;//数据组数
    cin>>t;
    while(t--) {
        memset(closelist,false,sizeof(closelist));
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++) {
                cin>>root.num[i][j];
                if(root.num[i][j]==0) {
                    root.x=i;
                    root.y=j;
                }
            }
        //初始化初试状态
        root.step=0;
        root.f=root.step+val(root.num);
        if(check(root.num)) cout<<"No Solution!"<<endl;
        else cout<<bfs()<<endl;
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值