搜索专题训练hdu1043Eight

Oasys安装教程 在基环境下执行然后去 C:\Users.condarc打开 .condarc这个文件 删除干净里面的东西。 阅读详情

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as: 
 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15  x

where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle: 
 1  2  3  4     1  2  3  4     1  2  3  4     1  2  3  4
 5  6  7  8     5  6  7  8     5  6  7  8     5  6  7  8
 9  x 10 12     9 10  x 12     9 10 11 12     9 10 11 12
13 14 11 15    13 14 11 15    13 14  x 15    13 14 15  x
            r->            d->            r->

The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively. 

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and 
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course). 

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three 
arrangement. 
Input
You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle 

1 2 3 
x 4 6 
7 5 8 

is described by this list: 

1 2 3 x 4 6 7 5 8 
Output
You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line. Do not print a blank line between cases. 
Sample Input
2  3  4  1  5  x  7  6  8
Sample Output
ullddrurdllurdruldr

经典的八数码问题,类似于我们平时玩的拼图。在解题过程中遇到了许多问题,参考了别人的做法,总结整理一下。

首先对于一个图的状态,我们要将其对应为一个status,通过康托展开,将其映射到hash空间。 康托展开利用了从高位到低位,每一位中比其小并且在高位中没有出现过的数字的个数,乘以该数位的阶乘。(我们可以通过这种方式得到比他小的数有多少个)。

把每一种情况hash映射以后,我们就要对当前状态到目标状态进行搜索,为了使路径尽可能短并且搜索更高效,首先想到了bfs,即从给定的状态,每次将x移动一步进行广度搜索,结果一定是TLE

由于测试是special judge而且是多组数据,想到离线bfs,即从终点开始,反向搜索,找出所有的可能解,然后根据给定状态是否被访问,直接输出答案,结果内存超限,bfs的层数太多了。

同时通过参考得知,无解的情况是可以直接判断来剪枝的,由于目标街12345678x的逆序数是0,其中x看做空位,每次变换就是x和某一位数交换,由于是空位交换,可以看做两次普通的数字交换,我们知道一次任意数字变换必定改变序列的逆序奇偶性,所以x和数字交换一定不改变奇偶性,所以可以直接判断逆序奇偶性来判断是否有解。

最后采用A*的方法,其中f = g + h  g用起始到当前的步数决定,h由当前状态到目标状态的曼哈顿距离表示。在搜索过程中,路径以记录上一节点位置和移动方向来记录,同时得到的结果是反向的,反向输出结果即可。

#include<bits/stdc++.h>
using namespace std;

const int MAXN=1000000;//最多是9!
int fac[]={1,1,2,6,24,120,720,5040,40320,362880};//康拖展开判重

bool vis[MAXN];//标记
char path[MAXN];
int pre[MAXN];
int cantor(int s[])//康拖展开求该序列的hash值
{
    int sum=0;
    for(int i=0;i<9;i++)
    {
        int num=0;
        for(int j=i+1;j<9;j++)
          if(s[j]<s[i])num++;
        sum+=(num*fac[9-i-1]);
    }
    return sum+1;
}
struct Node
{
    int s[9];
    int f, g, h; //存储函数
    int loc;//“0”的位置
    int status;//康拖展开的hash值
    friend bool operator< (const Node &a,const Node &b)
    {
        if(a.f==b.f)
            return a.g<b.g;
        return a.f>b.f;
    }
};
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//u,d,l,r
char indexs[5]="udlr";
int aim=46234;//123456780对应的康拖展开的hash值
int cheak(int s[])
{
    int cnt = 0;
    for(int i = 0; i < 9; ++i)
    {
        for(int j = i+1; j < 9; ++j)
            if(s[j]<s[i] && s[j] && s[i]) cnt++;

    }
    if(cnt%2)
        return 0;
    return 1;

}
int get_h(const Node &x)
{

    int ans=0;
    for(int i=0;i<9;i++)
    {
        if(x.s[i] == 0) continue;
        ans += abs(i/3-(x.s[i]-1)/3)+abs(i%3-(x.s[i]-1)%3);
    }
    //cout << ans << endl;
    return ans;

}
void bfs(Node &cur)
{
    //cout << "ok" << endl;
    memset(vis,false,sizeof(vis));
    Node next;
    cur.g = 0;
    cur.h = get_h(cur);
    cur.f = cur.g + cur.h;

    priority_queue<Node> q;
    q.push(cur);

    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        int x=cur.loc/3;
        int y=cur.loc%3;
        for(int i=0;i<4;i++)
        {
            int tx=x+dir[i][0];
            int ty=y+dir[i][1];
            if(tx<0||tx>2||ty<0||ty>2)continue;
            next=cur;
            next.loc=tx*3+ty;
            next.s[cur.loc]=next.s[next.loc];
            next.s[next.loc]=0;
            next.status=cantor(next.s);
            next.g++;
            next.h = get_h(next);
            next.f = next.g + next.h;
            if(!vis[next.status])
            {
                vis[next.status]=true;
                pre[next.status] = cur.status;
                path[next.status]= indexs[i];
                q.push(next);
                if(next.status == aim)
                    return;
            }
        }
    }

}
int main()
{
    char ch;
    Node cur;

    while(cin>>ch)
    {
        if(ch=='x') {cur.s[0]=0;cur.loc=0;}
        else cur.s[0]=ch-'0';
        for(int i=1;i<9;i++)
        {
            cin>>ch;
            if(ch=='x')
            {
                cur.s[i]=0;
                cur.loc=i;
            }
            else cur.s[i]=ch-'0';
        }
        if(!cheak(cur.s))
        {
            cout << "unsolvable" << endl;
            continue;
        }

        cur.status=cantor(cur.s);
        int start = cur.status;
        bfs(cur);
        int now = aim;
        stack<char> st;
        while(start != now)
        {
            st.push(path[now]);
            now = pre[now];
        }
        while(!st.empty())
        {
            cout << st.top();
            st.pop();
        }
        cout << endl;
    }
    return 0;
}


#Linux内存管理# 用一个案例详细介绍ARMv7-A架构 缺页中断处理的原理 跳转到异常向量:根据 异常向量表基地址(通常配置在 VBAR 寄存器中,例如 0xFFFF0000) + 数据中止异常的偏移量 0x10,跳转到数据中止异常的处理程序入口点 0xFFFF0010(实践中,这个入口通常是一条跳转到更复杂处理函数的指令)。malloc 通常不会立即分配物理内存页,它只是在内核为该进程维护的虚拟地址空间中找到一段足够大的空闲虚拟地址区域(比如 0x00008000 - 0x00008FFF),标记为已用,并返回起始地址(例如 0x00008000)给程序。 阅读详情

相关推荐

Windows本地部署Qwen3-14B实战指南:Ollama+Open WebUI零Docker方案

大语言模型本地部署是AI工程化落地的关键环节,其核心在于推理框架兼容性、硬件加速适配性与端到端稳定性。Ollama作为轻量级模型运行时,原生支持Windows DirectML与CUDA,规避了Docker/WSL2虚拟化开销和llama.cpp编译风险;Qwen3-14B则凭借128K上下文、Tool Calling原生支持及中文长文本结构化能力,成为政务、金融等场景首选。本文聚焦Windows平台真实约束——显存阈值、驱动版本匹配、路径编码兼容、离线知识库集成(RAG)等工程细节,提供经63台Windo

weixin_32136203的博客 412

hdu 1043 Eight(八数码问题 高级搜索: A* 搜索)

Problem Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 1

跟随内心的渴望 2781

Unity 游戏实例开发集合 之 Car Racing 2D (2D赛车) 休闲小游戏快速实现

Unity 游戏实例开发集合 之 Car Racing 2D (2D赛车) 休闲小游戏快速实现 目录 Unity 游戏实例开发集合 之 Car Racing 2D (2D赛车) 休闲小游戏快速实现 一、简单介绍 二、Car Racing 2D (2D赛车) 游戏内容与操作 三、相关说明 四、游戏代码框架 五、知识点 六、游戏效果预览 七、实现步骤 八、工程源码地址 九、延伸扩展 一、简单介绍 Unity 游戏实例开发集合,使用简单易懂的方式,讲解常见游戏的开发实现过程,方便

仙魁XAN 7701

hdu 1034 & poj 1077 Eight 传说中的八数码问题。真是一道神题,A*算法+康托展开

Problem Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile m

Lionel 5334

同花顺客户端数据采集:Hook注入与内存解密实战

在金融量化领域,客户端数据采集是获取低延迟、高精度行情的关键技术路径。其底层涉及进程通信、内存加密、反调试等典型Windows逆向工程问题。理解API Hook原理与AES-CBC动态密钥派生机制,有助于突破传统HTTP爬虫在国产金融软件中的失效困境。该技术不依赖网络劫持,而聚焦于本地进程内函数拦截与运行时内存解析,具备离线、可控、合规等工程优势,广泛应用于Level2行情、逐笔委托、分笔成交等高频数据场景,是量化基础设施中‘端侧数据主权’的重要实现方式。

weixin_30496999的博客 854

基于SOCKET的***网络软件的设计与实现

二、基于SOCKET的网络软件的需求。2、概要设计:画出软件系统结构图。3、详细设计:软件的详细设计,如类图、序列图、重要的算法流程图或伪代码,等等。4、实现:软件基本功能相关的截图和关键代码。

Recursions的博客 1371

Eight hdu 1043 八数码问题 双搜

Eight Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11226Accepted Submission(s): 3013Special Judge Problem Description The 15-puzzle h...

weixin_33676492的博客 376

用Python实现一个猜密码的程序

我们用Python实现猜密码

sk6584631_sz的博客 1260

八数码(双向广搜)POJ - 1077

八数码 The 15-puzzle has been around for over 100 years; even if you don’t know it by that name, you’ve seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all pa...

K_Code_的博客 421

电子元器件选型与实战应用—01 电阻选型

大家好, 我是记得诚。这是《电子元器件选型与实战应用》专栏的第一篇文章,今天的主角是电阻,在每一个电子产品中,都少不了电阻的身影,其重要性不言而喻。

记得诚 3531

Sigrity System SI SerialLink模式进行Pcie3协议仿真分析操作指导-pcie3_client_single_pre

Sigrity System SI SerialLink模式提供了10个协议合规性检查工具模板,用户可以将根据实际应用替换模板中的SPICE文件,然后进行协议仿真分析,同时软件还提供了目标结果的模板MASK以及该协议需要检查的项目,通过协议分析视为满足设计要求,下面介绍pcie3_client_single_pre仿真分析,具体操作如下。Random(Rj):随机抖动,该抖动用高斯概率分布来描述,通常是由热噪声和其它随机噪声引起的,默认为1.24%UI RMS(平均值),即和标准值的偏差为1.24%UI。

weixin_54787054的博客 595

HDU-1043

Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21024    Accepted Submission(s): 5644 Special JudgeProblem Description The 15-puzzle has

kala0的博客 466

LVGL 拨盘菜单(Dial Menu)

h。

摸鱼专家 191

训练3.08(HDU 1043 Eight 八数码-BFS+康托)

题目速递:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意: 多case. 八数码问题,就是在一个九宫格中,有一个是空的,其他位置都有数字,数字自1到8不可重复。每次可将空格上下左右的数字(若这个邻居位置还在九宫格中的话)选一个,与原本空格的位置进行交换。目标状态(x代表空格)是: 1 2 3 4 5 6 7 8 x 现在给你一个3*3矩阵,问能否...

urp 213

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

HDU - 1043 Eight 八数码 境界二 (BFS+康托展开)

The 15-puzzle has been around for over 100 years; even if you don’t know it by that name, you’ve seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed...

henuzxy 232

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

一种变进制数及其应用(全排列之Hash实现)

 我们经常使用的数的进制为“常数进制”,即始终逢p进1。例如,p进制数K可表示为    K = a0*p^0 + a1*p^1 + a2*p^2 + ... + an*p^n (其中0 它可以表示任何一个自然数。对于这种常数进制表示法,以及各种进制之间的转换大家应该是很熟悉的了,但大家可能很少听说变进制数。这里我要介绍一种特殊的变进制数,它能够被用来实现全排列的Hash函数,并且该Hash函数能够

inter999的专栏 2296

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

2017蓝桥杯官方模拟题 排列序数(康托展开)

题目: 思路: 康托展开模板题 代码: #include #include #include #include #include #include #include #include #define mem(a,b) memset(a,b,sizeof(a)) #define N 500000 #define inf 0x3f3f3f3f #define M 1

riba2534的博客 1250

POJ 1077八数码问题(cantor展开+BFS)

Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28887   Accepted: 12584   Special Judge Description The 15-puzzle has been around for over 100 years; e

fans_ac的博客 543

POJ 1077 Eight, 八数码问题

题目链接: http://poj.org/problem?id=1077 题目类型: 隐式图搜索 原题: The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with

D_Double's Journey 3045

POJ 1077 Eight(神奇的八数码问题)

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed

tystrwor的博客 381

康托展开式

如我想知道321是{1,2,3}中第几个大的数可以这样考虑 : 第一位是3,当第一位的数小于3时,那排列数小于321 如 123、 213 ,小于3的数有1、2 。所以有2*2!个。再看小于第二位2的:小于2的数只有一个就是1 ,所以有1*1!=1 所以小于321的{1,2,3}排列数有2*2!+1*1!=5个。所以321是第6个大的数。 2*2!+1*1!+0*0!就是康托展开。 再举个例子

asure__cpp的专栏 723

关于poj1077-八数码问题的深入探究

作为一个刚入acm坑不久的小白,看到绿色的accept六个字母的总是格外激动。可最近几天笔者却迟迟无法AC这道不算太复杂的八数码问题。谨以此文记录我的解题经验和跌宕起伏的心路历程。同样是小白的同学可以参考一下,技术大牛就直接飘过吧~ 题目是这样的: Description The 15-puzzle has been around for over 100 years; even if

Q1751703305的博客 510

hdu 搜索专题

1:A strange lift    Kill the monster    Red and Black    Counting Sheep    Knight Moves    Oil Deposits 2:Square    Sum It Up   Catch That Cow    Robot Motion

MAKE_TROUBLE的专栏 981

八数码问题——HDU 1043

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed

大田二十 1717

HDU1043:Eigth(康托展开)

Problem Description   The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to

ACM!荣耀之路! 3046

hdu1043Eight

Eight Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 16   Accepted Submission(s) : 1 Special Judge Problem Description The 15-pu

skyline 1001
上一篇: 搜索专题训练hdu1455 Sticks
下一篇: c++派生类对象赋值给基类对象
Qer_
博客等级 码龄11年 34粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值