Eight II HDU - 3567 (bfs打表+hash映射+康托展开)

HDU(3567):八数码问题(升级版)——双BFS 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567   有了HDU 1043的基础,此题就变得比较容易了,如果用A*算法,感觉路径不太好打印,所以就放弃了,看到有一种好像和1043一样的打表法,但是没怎么能理解,也就放弃了,虽然要打印最小字典序,单向BFS显然会TLE,所以我只能用双向BFS了。   好在,此题的输入保证有解,否则还得用一 阅读详情

Eight II

HDU - 3567

Eight-puzzle, which is also called "Nine grids", comes from an old game.

In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.

We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.



A state of the board can be represented by a string S using the rule showed below.



The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.

Input The first line is T (T <= 200), which means the number of test cases of this problem.

The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.
Output For each test case two lines are expected.

The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
Sample Input
2
12X453786
12345678X
564178X23
7568X4123
Sample Output
Case 1: 2
dd
Case 2: 8
urrulldr

这道题是上一道题Eight I的一道升级版,这道题首先需要从指定的初始状态到指定的目标状态,但是我们可以发现的是总的大情况无外乎就一下九种

    char eight_num[9][10] = {"X12345678" , "1X2345678" ,  "12X345678" , "123X45678" ,  
                            "1234X5678" , "12345X678" , "123456X78" , "1234567X8" , "12345678X" };  
也就是按照X的位置分成了九种大类,那么其他情况该怎么办呢,我们可以通过映射,把初始状态根据X的位置映射到上面其中的一种(一定可以找到一种),然后根据初始数组每个数字所映射的数字,去更新目标数组的数字这样就可以从以上九种状态中找了,比如说示例1中的
12X453786
我们就可以做这样的一个映射“12X345678”

分别是:1 → 1,2 → 2,X → X,4 → 3,5 → 4,3 → 5,7 → 6,8 → 7,6 → 8,

然后目标状态一样要映射掉。

这样我们就可以通过预处理,将以每种状态为起点状态开始bfs存下它所能到达的所有状态的路径,但是这个题还有个特殊要求要求长度最短,字典序最小,那么记录路径就不能想上个题一样直接方向和字符相反对应了。

为了使得字典序最小我们必须要让方向以dlru(down,left,right,up)的顺序进行bfs(字母顺序),这样我们预处理完记录下路径后,我们只能从得到的目标状态往前找到初始状态,然后在逆序输出。

具体代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 1e8;
const int MAXN = 4e5;
int n,m;
char eight_num[9][10] = {"X12345678","1X2345678","12X345678","123X45678","1234X5678","12345X678",
                         "123456X78","1234567X8","12345678X"};
char step[] = "dlru";
int dx[4] = {1,0,0,-1};
int dy[4] = {0,-1,1,0};
int fac[9] = {1,1,2,6,24,120,720,5040,40320};
int vis[9][MAXN];
int pre[9][MAXN];
struct node{
    int num[9];//八数码
    int state;//状态(康托展开值)
    int pos;//x的位置
};
//得到状态
int Cantor(int *s){
    int sum = 0;
    for(int i = 0; i < 9; i++){
        int num = 0;
        for(int j = i+1; j < 9; j++){
            if(s[i] > s[j]) num++;
        }
        sum += num * fac[8-i];
    }
    return sum;
}
void printAns(int state,int kind){
    string ans;
    while(state != -1){
        ans += step[vis[kind][state]];
        state = pre[kind][state];
    }
    printf("%d\n",ans.size()-1);
    for(int i = ans.size() - 2; i >= 0; i--){
        printf("%c",ans[i]);
    }
    puts("");
}
void bfs(node x,int kind){
    queue<node> q;
    q.push(x);
    vis[kind][x.state] = 1;//这个是初始化的值并不是真正走的值所以上面输出的时候是从倒数第二个开始输出
    while(!q.empty()){
        node u = q.front();
        q.pop();
        //4种交换
        int x = u.pos / 3;
        int y = u.pos % 3;
        for(int i = 0; i < 4; i++){
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(xx >= 0 && xx < 3 && yy >= 0 && yy < 3){
                node v = u;
                swap(v.num[u.pos],v.num[xx*3+yy]);
                v.state = Cantor(v.num);
                if(vis[kind][v.state] == -1){
                    v.pos = xx * 3 + yy;
                    vis[kind][v.state] = i;//代表通过i方向的移动到达kind这类中的v.state状态
                    pre[kind][v.state] = u.state;//记录这种状态的前一种状态
                    q.push(v);
                }
            }
        }
    }
}
void init(char *s,int kind){
    node u;
    for(int i = 0; i < 9; i++){
        if(s[i] == 'X'){
            u.num[i] = 0;
            u.pos = i;
        }
        else u.num[i] = s[i] - '0';
    }
    u.state = Cantor(u.num);
    bfs(u,kind);
}
int main(){
    int t,cas = 0;
    char s[15],c[15];
    int kind;
    int temp[MAXN];
    memset(vis,-1,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    for(int i = 0; i < 9; i++){
        init(eight_num[i],i);
    }
    scanf("%d",&t);
    while(t--){
        scanf("%s",s);
        int cnt = 1;
        //映射初始状态的八数码
        for(int i = 0; i < 9; i++){
            if(s[i] == 'X'){
                c[0] = 0;
                kind = i;
            }
            else c[s[i]-'0'] = cnt++;
        }
        //映射目标状态八数码
        scanf("%s",s);
        for(int i = 0; i < 9; i++){
            if(s[i] == 'X') temp[i] = c[0];
            else temp[i] = c[s[i]-'0'];
        }
        int state = Cantor(temp);
        printf("Case %d: ",++cas);
        printAns(state,kind);
    }
    return 0;
}

[kuangbin]专题二 搜索进阶 Eight II HDU - 3567【反向BFS】【康托展开】【哈希】 【题目描述】 Eight-puzzle,which is also called “Nine grids”,comes from an old game. In this game,you are given a 3 by 3 board and 8 tiles.The tiles are numbered from 1 to 8 and each covers a grid.As you see... 阅读详情

相关推荐

避坑指南:ZYNQ MPSOC QSPI固化时MT25QU256闪存的特殊配置

本文针对Xilinx ZYNQ UltraScale+ MPSoC平台,详细解析了使用Vitis工具将BOOT.bin固化至Micron MT25QU256ABA QSPI NOR Flash时的特殊配置与避坑指南。重点阐述了Init文件的真实作用、电压与频率的精确匹配策略,以及针对该闪存型号的初始化序列,帮助工程师解决烧录失败、启动异常等常见问题,确保系统可靠启动。

weixin_29175469的博客 270

HDU3567:Eight II(康拓展开+预处理) (B)

Problem Description Eight-puzzle, which is also called "Nine grids", comes from an old game.  In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each

qq_36782366的博客 469

pcb印刷电路缺陷检测----数据集准备

PKU-Market-PCB 印刷电路板 (PCB) 瑕疵数据集是一个公共合成 PCB 数据集,包含 1,386 张图像,具有 6 种缺陷(漏孔、鼠咬、开路、短路、杂散、杂铜),可用于图像检测、分类和配准任务。

qq_30190551的博客 4421

HDU 3567 Eight II

//HDU 3567 Eight II //Eight的进阶版 //1.划归思想,将所有问题归为九种以X的位置进行讨论 //2.路径的记录与每个节点的特殊标记关联,该题便通过vis[][]起到记录路径的作用 //3.有顺序的广搜 //4.康托展开hash构造 #include<bits/stdc++.h> using namespace std; const int maxn = 4e5; //大于9! const int fac[10]={1,1,2,6,24,120,720,5040,

qq_46194023的博客 198

使用kmplot在线进行生存分析

欢迎关注”生信修炼手册”!kmplot是一个生存分析的在线工具,最初是设计用来对肝癌中的miRNA进行生存分析,对应的文章发表在scientific reports上,链接如下https...

庐州月光的博客 1万+

UOS的ARM机编译QT5.14.2记录

我需要的qt版本是Qt5.14.2,所以选择编译Qt5.14.2源码;几个月前尝试了编译一次,当时编译过程中出现了报错,当时时间很紧最后安装的默认版本来使用,最近有时间重新进行了一次编译,历经3个星期(4G的内存)终于解决了各种坑,编译成功了,现在将编译过程中遇见的一些坑记录一下,方便日后能记得这一次可恶的编译过程,如有描述欠缺的地方,还希望各位大佬给点意见指导指导🙏。

NiF693zzz的博客 1616

hdu 3567(八码数 + 双向bfs)

题目连接:https://vjudge.net/contest/353606#problem/B 参考文章:https://blog.youkuaiyun.com/laaahu/article/details/96648344 思路: 广搜,记录路径,因为最多的交换次数不会太多,所以可以用int类型的4进制数记录路径;因为要求字典序最小,所以走得方向是dlru,当每次找到合法的路径时判断是否是字典...

积少成多,水滴石穿 658

Python 爬虫实战:爬取 B 站 UP 主的全部视频数据

本文详细介绍了基于Python开发B站UP主视频数据爬虫的技术方案。通过分析B站API接口,使用requests库发送HTTP请求并处理JSON数据,结合pandas进行结构化存储。文章重点解决了接口鉴权、分页爬取、反爬策略等核心难点,实现获取视频标题、播放量、点赞数等关键指标。同时强调数据爬取需遵守平台规范,仅限技术研究使用。该方案为B站视频内容分析提供了可靠的数据采集基础,具有实际应用价值。

2503_91057718的博客 2492

【HDU No. 3567】八数码 II Eight II

【HDU No. 3567】八数码 II Eight II

代码改变世界 737

61、神经科学中的分岔与双分子反应研究

本文探讨了神经科学中的分岔现象与双分子反应建模。在神经元网络中,通过分析四细胞网络的呼吸节律振荡,揭示了分岔对振荡频率和占空比的影响,并区分了单神经元与网络层面的分岔特性。在神经群体模型中,系统参数变化引发的分岔可导致振荡、混沌等动态行为转变,文中详细介绍了局部分岔、全局分岔和高余维分岔的机制,并以Jansen-Rit模型为例展示了输入水平变化下的分岔过程。在分子层面,重点介绍了MWC模型在描述配体与多位点蛋白质结合中的应用,涵盖其构象状态、平衡常数及统计因子。研究表明,分岔理论和双分子反应建模为理解神经系

yog99的博客 80

音频链路分析

音频链路分析摘要 本文分析了蓝牙音频设备(BES2700)的音频传输链路,主要包括通话和音乐播放两种场景: 通话链路: 下行:蓝牙接收数据→解码→通过Codec输出到扬声器 上行:麦克风采集→Codec处理→蓝牙编码发送 音乐播放链路: 通过A2DP协议接收高质量音频数据 经过解码、EQ调节等音频处理后输出 系统采用多线程消息机制处理音频任务,通过mailbox传递控制消息,由回调函数处理具体音频数据流。不同应用场景使用不同的蓝牙协议栈(HFP/HSP用于通话,A2DP用于媒体)和编码方式(mSBC/SBC

云淡风轻 85

HDU 3567 Eight II (BFS+映射+康托展开)

题面 Eight-puzzle, which is also called "Nine grids", comes from an old game. In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you s...

Yi_Qing_Z的博客 1028

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的农作物害虫检测系统(深度学习模型+UI界面+训练数据集)

随着人工智能技术在农业领域的深入应用,基于深度学习的害虫检测系统已成为智慧农业的重要组成部分。本文详细介绍了一种基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的农作物害虫检测系统,涵盖算法原理、数据集构建、模型训练、系统实现及完整代码。该系统能够实时检测并识别多种农作物害虫,为农业生产提供精准的病虫害监测解决方案。

YOLO 273

HDU - 3567 Eight II 【bfs打表+映射+康托展开】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567 这道题比较容易想到的就是双向bfs,在时间上是比较理想的,但是在内存上就比较紧张。 网上看到有人用映射的技巧做这题。 假设我知道起点是 12345678x这个状态到其他状态的所有路径。对于所有起点是 ********x这个状态到其他状态的所有路径我也能够知道。 假设我知道12x34567...

Y390d的博客 358

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

HDU3567(Eight II)

Eight II 题目传送门 Problem Description Eight-puzzle, which is also called “Nine grids”, comes from an old game. In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid

weixin_43808717的博客 285

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Eight II HDU - 3567

Eight II HDU - 3567 知识点: bfs 打表, 康托展开 思路: 先预处理"X12345678",“1X2345678”,“12X345678”,“123X45678”,“1234X5678”,“12345X678”, “123456X78”,“1234567X8”,"12345678X"等状态 每次将给定的s1映射为9个状态中的一个x1,将s2通过s1到x1的映射得到目标状态x2,再根据打表得到答案即可 使用康托展开为了压缩空间 #include <iostream&gt

weixin_45535709的博客 219

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

HDU 3567 - Eight II 这该死的题

关于这道题 我真的想骂娘(好吧是自己太菜了) 我很早其实自己推出来了,就是还是bfs+康托但是列举九种不同的X位置,同时利用映射,注意要映射第一个输入的模板,然后倒序输出路径!!很重要 我一开始天真的想用路径urld然后顺序输出 然后- - 也就几个小时没了吧小问题 但是我卡在了两个莫名其妙的问题上 ** 康拓展开后面return忘记+1**,导致字典序在实例 5742631X8 1568...

Leslie5205912的博客 382

hdu 3567 Eight II 八数码 双向BFS

代码:

冷月之殇 3390

hdu 3567(八数码问题 双向BFS)

题意:八数码问题,给了状态A,B,求A-&gt;B的最少变化次数,有多组最少则输出字典序最小的。可以先做这道http://acm.hdu.edu.cn/showproblem.php?pid=1043。这道简单些。 思路:双向BFS,同时从起点,终点进行搜索。这里以一个整数来表示路径。 从起点搜索,路径表示为 从终点搜索,路径表示为 这里i的取值为0~3,即空白点的不同移动所代表的数字,s...

m0_37514283的博客 1594

HDU3567:Eight II(康拓展开+预处理)

Problem Description Eight-puzzle, which is also called "Nine grids", comes from an old game.  In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each

ACM!荣耀之路! 4944

HDU 3567

Eight II Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 130000/65536K (Java/Other) Total Submission(s) : 9   Accepted Submission(s) : 3 Problem Description Eight-puzzle, which is a

Dream_M 2200

HDU 3567 Eight II 八数码(2)

Eight-puzzle, which is also called "Nine grids", comes from an old game.  In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you s

樱花满地集于我心,楪舞纷飞祈愿相随 792

HDU 3567 Eight II 预处理+bfs+hash

传送门:HDU3567 题意:经典八数码问题,将八数码从状态A转化到状态B,问最短且字典序最小的解法。 思路:先说说我自己吧,我是刚做完了POJ1077就来做这个题,当时感觉这不是一样的么,改个bfs结束条件不就完事了,然后就一发wa。。看题解才发现A*算法不能保证输出结果是字典序最小的,仔细想想,好像确实是,因为A*中F=G+H的等式是为了更快的求得解,我们每次从优先队列中取出来的状态是F值

WA是一笔财富 1287
上一篇: Eight HDU - 1043 (八数码,BFS逆向打表+康托展开hash)
下一篇: Destruction of a Tree (dfs+思维)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值