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

由蓝桥杯的九宫重排题改进的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
    评论
青蛙跳杯子是蓝桥杯历届试题中的一道典型题目。题目要求模拟一个青蛙跳杯子的游戏,给定n个杯子和m次操作,每次操作可以选择一个杯子,并将其放置在当前位置前面或者后面的位置。最后,要求输出经过m次操作后,杯子从左到右的顺序。 下面是一个可能的Java代码实现: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); // 杯子的数量 int m = scanner.nextInt(); // 操作的次数 int[] cups = new int[n]; // 初始化杯子的初始顺序 for (int i = 0; i < n; i++) { cups[i] = i+1; } // 执行m次操作 for (int i = 0; i < m; i++) { int index = scanner.nextInt(); // 选择的杯子的位置 int direction = scanner.nextInt(); // 移动的方向,1表示前面,2表示后面 // 将选择的杯子移动到指定位置 if (direction == 1) { for (int j = index-1; j > 0; j--) { int temp = cups[j]; cups[j] = cups[j-1]; cups[j-1] = temp; } } else if (direction == 2) { for (int j = index-1; j < n-1; j++) { int temp = cups[j]; cups[j] = cups[j+1]; cups[j+1] = temp; } } } // 输出最终的杯子顺序 for (int i = 0; i < n; i++) { System.out.print(cups[i] + " "); } } } ``` 该代码首先读取输入的杯子数量n和操作次数m,然后创建一个数组用来保存杯子的顺序。从1到n依次填充数组的初始顺序。接下来,执行m次操作,根据输入的位置和方向调整杯子的顺序,最后输出调整后的杯子顺序。 需要注意的是,该代码没有进行输入合法性的验证,实际应用中可能需要对输入进行适当的验证和处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值