蓝桥杯历届试题——九宫重排(启发式搜索)

由蓝桥杯的九宫重排题改进的A算法
原文章见> http://blog.csdn.net/blue_skyrim/article/details/62418334

用启发式搜索,评价函数公式为f=g+h,g是目前为止已经发生过的耗散值,h是预计到目标需要的耗散值,这里我是与结果位置不符的数字越多,h就越大,每次搜索只需搜索评价函数最小的九宫格状态即可

对比之前的广搜,效率明显有很大提升
bfs:
这里写图片描述

启发式搜索:
这里写图片描述

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
#define MAXN 100005
#define Mod 1000000007
using namespace std;
char hajime[5][5],owali[5][5];
set<string> hsh;
struct Node
{
    int x,y;
    int g,h,f;
    char pre;
    char map[5][5];
};
struct cmp
{
    bool operator()(Node a,Node b)
    {
        return a.f>b.f;
    }
};
bool check(Node a)
{
    for(int i=0; i<3; ++i)
        for(int j=0; j<3; ++j)
            if(a.map[i][j]!=owali[i][j])
                return false;
    return true;
}
bool getvis(Node a)
{
    string tmp="";
    for(int i=0; i<3; ++i)
        for(int j=0; j<3; ++j)
        {
            tmp+=a.map[i][j];
        }
    if(hsh.find(tmp)!=hsh.end())
        return false;
    hsh.insert(tmp);
    return true;
}
int geth(Node a)
{
    int ans=0;
    for(int i=0;i<3;++i)
        for(int j=0;j<3;++j)
            if(a.map[i][j]!=owali[i][j])
                ans++;
    return ans;
}
bool bfs(int x,int y)
{
    Node start;
    start.x=x,start.y=y;
    start.pre='X';
    for(int i=0; i<3; ++i)
        for(int j=0; j<3; ++j)
            start.map[i][j]=hajime[i][j];
    start.g=0;
    start.h=geth(start);
    start.f=start.g+start.h;
    priority_queue<Node,vector<Node>,cmp> open;
    open.push(start);
    while(!open.empty())
    {
        Node tmp=open.top(),tmp1;
        open.pop();
        if(check(tmp))
        {
            cout<<tmp.g<<endl;
            return true;
        }
        tmp1=tmp;
        if(tmp1.pre!='U'&&tmp1.x+1<3)
        {
            tmp1.g++;
            tmp1.pre='D';
            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x+1][tmp1.y]);
            tmp1.x++;
            if(getvis(tmp1))
            {
                tmp1.h=geth(tmp1);
                tmp1.f=tmp1.g+tmp1.h;
                open.push(tmp1);
            }
        }
        tmp1=tmp;
        if(tmp1.pre!='D'&&tmp1.x-1>=0)
        {
            tmp1.g++;
            tmp1.pre='U';
            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x-1][tmp1.y]);
            tmp1.x--;
            if(getvis(tmp1))
            {
                tmp1.h=geth(tmp1);
                tmp1.f=tmp1.g+tmp1.h;
                open.push(tmp1);
            }
        }
        tmp1=tmp;
        if(tmp1.pre!='L'&&tmp1.y+1<3)
        {
            tmp1.g++;
            tmp1.pre='R';
            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x][tmp1.y+1]);
            tmp1.y++;
            if(getvis(tmp1))
            {
                tmp1.h=geth(tmp1);
                tmp1.f=tmp1.g+tmp1.h;
                open.push(tmp1);
            }
        }
        tmp1=tmp;
        if(tmp1.pre!='R'&&tmp1.y-1>=0)
        {
            tmp1.g++;
            tmp1.pre='L';
            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x][tmp1.y-1]);
            tmp1.y--;
            if(getvis(tmp1))
            {
                tmp1.h=geth(tmp1);
                tmp1.f=tmp1.g+tmp1.h;
                open.push(tmp1);
            }
        }
    }
    return false;
}
int main()
{
    char tmp[100];
    scanf("%s",tmp);
    int x,y;
    for(int i=0; tmp[i]!='\0'; ++i)
    {
        hajime[i/3][i%3]=tmp[i];
        if(tmp[i]=='.')
        {
            x=i/3;
            y=i%3;
            hajime[i/3][i%3]='0';
        }
    }
    scanf("%s",tmp);
    for(int i=0; tmp[i]!='\0'; ++i)
    {
        owali[i/3][i%3]=tmp[i];
        if(tmp[i]=='.')
            owali[i/3][i%3]='0';
    }
    if(!bfs(x,y))
        cout<<-1<<endl;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值