hdu2567 EightII(IDA*/逆向BFS打表)

dreamerv3复现教程 由于dreamerv3存在很多版本冲突问题,所以很难直接使用和复现。 阅读详情

Eight II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 5014 Accepted Submission(s): 1096

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 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

A*不太能处理字典序的问题,靠入队顺序没办法判,比如说一个字典序不靠前的移动序列过早的少估了,那么最后结果就是错的。。。相比起来IDA*有先天优势。同时有些大常数的剪枝没什么卵用
可以不连续的加深,需要维护一个估计的最小深度。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const LL mod=1e9+7;
const double eps=1e-9;
string Input;
char str[10];
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
char nxtdir[5]="dlru";
char predir[5]="urld";
int is[500000];
//int tt[500000];
int pos[10];
stack<char>ans;
struct node{
    char mp[10];
    int x,y,h,hsh;
}st;
int cantor(char *s){
    int ans=0;
    for(int i=0;i<8;++i){
        for(int j=i+1;j<9;++j)if(s[j]<s[i])ans++;
        ans*=(8-i);
    }
    return ans;
}
inline int trans(int x,int y){return 3*(x-1)+y-1;}
int get_h(char *s){
    int h=0;
    for(int i=0;i<9;i++){
        if(s[i]!=9)h+=(abs(i/3-pos[s[i]]/3)+abs(i%3-pos[s[i]]%3));
    }
    return h;
}
int debug(){
    for(int i=0;i<9;i++)cout<<(int)st.mp[i];
    cout<<' '<<st.h<<endl;
}
int IDA(int mxd,int d,int pre=-1){
    //debug();
    if(!st.h)return 1;
    if(d+st.h>mxd||is[st.hsh]<d)return 0;
    //if(tt[st.hsh]==mxd&&is[st.hsh]==d))return 0;
    //tt[st.hsh]=mxd;
    //is[st.hsh]=d;
    node tmp=st;
    for(int i=0;i<4;i++){
        if(i+pre==3)continue;
        st.x=tmp.x+dx[i];
        st.y=tmp.y+dy[i];
        if(st.x>3||st.x<1||st.y<1||st.y>3){st=tmp;continue;}
        swap(st.mp[trans(st.x,st.y)],st.mp[trans(tmp.x,tmp.y)]);
        //st.hsh=cantor(st.mp);
        st.h=get_h(st.mp);
        if(IDA(mxd,d+1,i)){ans.push(nxtdir[i]);return 1;}
        st=tmp;
    }
    //debug();
    return 0;
}
void store(string p,node &s){
    int k=0;
    for(int i=0;i<Input.length();i++){
        if(Input[i]=='X'){
            Input[i]='9';
            s.x=k/3+1;
            s.y=k%3+1;
        }
        if(Input[i]==' ')continue;
        s.mp[k++]=Input[i]-'0';
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    for(int cas=1;cas<=T;cas++){
        node ed;
        cin>>Input;
        store(Input,st);
        cin>>Input;
        store(Input,ed);
        for(int i=0;i<9;i++)pos[ed.mp[i]]=i;
        st.h=get_h(st.mp);
        st.hsh=cantor(st.mp);
        memset(is,INF,sizeof is);
        //memset(tt,-1,sizeof tt);
        for(int i=st.h;!IDA(i,0);i++){}
        cout<<"Case "<<cas<<": "<<ans.size()<<endl;
        while(!ans.empty()){cout<<ans.top();ans.pop();}
        cout<<endl;
    }
    return 0;
}

逆向bfs的思路主要是把原串和目标串重新映射一下,使目标串被归约到九种情况,用正常的逆向bfs就可以做了(写完以后反过来一想感觉逆向是没有必要的,规约初始串是一样的。。。)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const LL mod=1e9+7;
const double eps=1e-9;
string Input;
char str[10];
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
char nxtdir[5]="dlru";
char predir[5]="urld";
int nxt[10][500000];
int op[10][500000];
int vis[10][500000];
int pos[10];
struct node{
    char mp[10];
    int x,y,d;
};
int cantor(char *s){
    int ans=0;
    for(int i=0;i<8;++i){
        for(int j=i+1;j<9;++j)if(s[j]<s[i])ans++;
        ans*=(8-i);
    }
    return ans;
}
int trans(int x,int y){
    return 3*(x-1)+y-1;
}
void bfs(int id,node st){
    int sthsh=cantor(st.mp);
    nxt[id][sthsh]=-1;st.d=0;
    queue<node>mq;
    mq.push(st);vis[id][sthsh]=st.d;
    while(!mq.empty()){
        node nw=mq.front();
        mq.pop();int prehsh=cantor(nw.mp);
        for(int i=3;i>=0;i--){
            node pp=nw;
            int nxtx=pp.x+dx[i];
            int nxty=pp.y+dy[i];
            if(nxtx<1||nxtx>3||nxty<1||nxty>3)continue;
            swap(pp.mp[trans(pp.x,pp.y)],pp.mp[trans(nxtx,nxty)]);
            int hsh=cantor(pp.mp);
            pp.x=nxtx,pp.y=nxty;pp.d++;
            if(vis[id][hsh]==-1||(vis[id][hsh]==pp.d&&i>op[id][hsh])){op[id][hsh]=i;nxt[id][hsh]=prehsh;}
            if(vis[id][hsh]==-1){
                vis[id][hsh]=pp.d;
                mq.push(pp);
            }
        }
    }
}
node Createnode(int x){
    node p;int k=1;
    for(int i=0;i<9;i++){
        if(i!=x)p.mp[i]=k++;
    }p.mp[x]=k;
    p.x=x/3+1;
    p.y=x%3+1;
    return p;
}
void print(int id,int q){
    if(nxt[id][q]==-1){cout<<endl;return ;}
    cout<<predir[op[id][q]];
    print(id,nxt[id][q]);
}
void store(string p,node &s){
    int k=0;
    for(int i=0;i<Input.length();i++){
        if(Input[i]=='X'){
            Input[i]='9';
            s.x=k/3+1;
            s.y=k%3+1;
        }
        if(Input[i]==' ')continue;
        s.mp[k++]=Input[i]-'0';
    }
}
void init(){
    memset(vis,-1,sizeof vis);
    for(int i=0;i<9;i++){
        bfs(i,Createnode(i));
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    init();
    int T;
    cin>>T;
    node st,ed;
    for(int cas=1;cas<=T;cas++){
        cin>>Input;store(Input,st);
        cin>>Input;store(Input,ed);
        int pp=0,kk=1;
        for(int i=0;i<9;i++){
            if(ed.mp[i]==9){pp=i;continue;}
            pos[ed.mp[i]]=kk++;
        }
        for(int i=0;i<9;i++){
            if(st.mp[i]==9)continue;
            st.mp[i]=pos[st.mp[i]];
        }
        int hsh=cantor(st.mp);
        cout<<"Case "<<cas<<": "<<vis[pp][hsh]<<endl;
        print(pp,hsh);
    }
    return 0;
}

ROS2节点通信实战:用C++手写一个‘智能小车’控制模型(附完整代码) 本文详细介绍了如何使用ROS2和C++构建一个智能小车控制模型,涵盖运动控制节点、电池管理服务、参数动态配置和Rviz可视化等核心功能。通过实战项目演示ROS2节点通信机制,帮助开发者快速掌握机器人开发的关键技术,并提供了完整的代码实现和系统集成方案。 阅读详情

相关推荐

python:numpy分享(保姆级教程)

创建数组:array和ndarray、rand()、randn()、arange()、randint()、uniform()astype()基础函数:ceil()、floor()、rint()、argmax()、argmin()、cumsum()、cumprod()numpy 是数据分析必不可少的第三方库,具有高性能的特点,多用于做数组分析。内置函数:ceil()、floor()、rint()主要用于数组计算,支持多维数组。array和ndarray。比较函数:all、any。去重函数:unique。

sulin123123的博客 1752

HDU 2567 寻梦

HDU 2567 寻梦 2010-11-27  16:57:46  Accepted  2567  0MS  204K  401B  C  Y 代码 #include <stdio.h>#include <string.h>#define N 20int main(){ int cas, i, ...

weixin_30636089的博客 265

【UE5 C++开发指南】从零配置Visual Studio 2022开发环境

本文详细指导如何从零开始为UE5 C++开发配置Visual Studio 2022环境。文章强调了VS2022与UE5的深度兼容性,并逐步讲解了社区版下载、核心工作负载(如“使用C++的游戏开发”)与组件安装、界面优化、专属工具设置以及首个C++项目的创建与验证流程,帮助开发者高效搭建专业开发环境。

weixin_29016865的博客 411

HDU2567 寻梦【水题】

题目大意:给你两个字符串,将第二个字符串插入到第一个字符串的正中间之后,再输出第一个 字符串。 思路:先输出第一个字符串的前半段,再输出第二个字符串,最后再输出第一个字符串的后半段。

程序员充电站(itcharge) 1368

【Python机器学习】零基础掌握TweedieRegressor广义线性回归模型

本次文章主要介绍了如何使用Tweedie回归模型预测电动汽车的实际续航里程。基于车速、气温和驾驶习惯等多种因素,模型能够准确地预测出电动汽车的续航里程,从而为生产商和消费者提供有用的信息。优点总结,Tweedie回归模型在处理具有多元性和高度散布的数据方面表现出色,特别适用于需要考虑多种影响因素的预测任务。优点名称对应描述说明多元性强可以同时处理多种类型和维度的数据高度可定制允许用户自定义损失函数和正则项对异常值鲁棒能够较好地处理数据中的异常值易于解释参数和预测结果都相对容易解释。

Mr数据杨 1018

Cadence SPB16. 6 安装流程

1.首先下载CadenceAllegroSPBorCAD16.6安装包 下载之后运行其中的setup.exe,然后先安装第一项LicenseManager 一直按next或install, 直到被询问license时,单击cancel,然后选是和finish。 2,安装Product Installtion 直接点next跳过这一步 安装完成后,点Exit退出 3.按ctrl+alt+delete打开任务管...

wangjie36的博客 5172

HDU2567(字符串简单题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2567 解题思路: 先输出第一个串长度一半的字符,然后输出第二个串,再接着把第一个串输出完。 完整代码: #include #include #include #include #include #include #include #include #includ

代码艺术的道与术 1159

基于YOLOv8的水果新鲜程度检测系统设计与实现

数据采集与预处理模块:负责收集水果图像数据并进行标注和增强深度学习模型:基于YOLOv8的目标检测模型新鲜度评估模块:根据检测结果评估水果新鲜程度用户界面:提供友好的交互界面系统集成:将各模块整合为完整系统水果新鲜程度检测需要包含多种水果在不同新鲜度状态下的图像数据集。:包含5种常见水果(苹果、香蕉、橙子、葡萄、草莓)在不同新鲜度下的图像包含约8,000张图像,分为新鲜和腐烂两类AgriVision:大型农业图像数据集,包含水果检测和分类任务Fruit-360:包含120种水果的高质量图像。

YOLO 456

hdu-2567-寻梦

#include #include char a[55],b[55]; int main() {     int s,n,i;     scanf("%d",&s);     while(s--)     {         scanf("%s%s",a,b);         n=strlen(a);         for(i=0;i         printf("%

u011514451的专栏 617

XSS漏洞之PDF生成:从XSS到服务端任意文件读取

XSS是最为常见的Web漏洞之一,多年来连续入选OWASP TOP10,相信大家都耳熟能详。 它是一种代码注入类的攻击,是一种客户端侧的攻击,攻击者通过在Web应用中注入恶意JavaScript代码,通过点击URL,最终在受害者浏览器端执行的一种漏洞。主要有反射型XSS、存储型XSS、基于DOM的XSS三类。XSS一些比较常规的危害大概有:重定向浏览器、窃取Cookie、浏览器劫持等。本文章侧重于发现XSS后的进一步利用。 主要介绍在某次实际测试过程中,发现XSS漏洞之后,如何进一步的利用,最终通过XSS达

计算机毕业论文源码,学生个人网页制作html源码。贴近用户做网络推广和互联网优化。 3796

YOLO11实战优化:暗光环境检测 | 引入Retinex增强模块与YOLO11联合训练,解决夜间/低照度场景抓拍痛点

本文探讨了暗光环境下目标检测的技术挑战与解决方案。研究发现,传统CNN模型在低照度条件下性能显著下降,主要由于光子数减少导致信噪比降低、目标特征被噪声淹没。针对这一问题,文章提出将可微分光照增强模块嵌入YOLO检测器的技术路线,重点分析了Retinexformer+YOLO11联合方案的优势。 文章对比了YOLO系列模型在暗光场景的表现,指出YOLO11凭借其C3k2模块、无锚检测头和多任务统一骨干网络,在低对比度图像特征提取方面优于前代产品。同时详细介绍了Retinexformer的理论基础和技术创新,包

2601_95807009的博客 217

HDU 2567 寻梦(字符串,插入)

#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> using namespace std; int main() { int t; char a[55],b[55],r[120];//b插入a中央 scanf("%d"...

weixin_33701617的博客 215

BMS控制板硬件设计核心要点与工程避坑指南

电池管理系统(BMS)硬件设计是保障锂电安全与寿命的关键环节,其本质是模拟前端精度、数字控制可靠性、电源完整性及电磁兼容性四大要素的系统协同。理解AFE芯片(如BQ76920)的物理层约束、MCU与专用监控IC的异构分工逻辑,是构建高鲁棒BMS的基础;而晶振布局、I²C总线匹配、MOSFET体二极管方向、开尔文采样等细节,直接决定量产良率与EMC合规性。在电动工具、储能系统、两轮电动车等中低功耗BMS应用中,成本敏感型方案常采用STM32F0+AFE架构,此时电源滤波电容选型、复位电路ESR控制、TVS/齐

weixin_30591519的博客 271

HDU2567 寻梦【输入输出流+水题】

寻梦 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8776    Accepted Submission(s): 6543 Problem Description 每个人的童年都可能梦想过自己成为一个英雄,尤其是喜欢武侠的男生

海岛Blog 2068

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...

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

HDU - 2567 寻梦

OJ地址:http://acm.hdu.edu.cn/showproblem.php?pid=2567 每个人的童年都可能梦想过自己成为一个英雄,尤其是喜欢武侠的男生,Yifenfei也不例外。 童年的他常常梦想自己能成为一个绝世英雄,手拿一把灿灿发亮的宝剑,手挽一位长发飘逸的秀丽气质MM ^_^ ,散步于清幽的泉边小道,微风吹过,飘落两片枫叶。。。。。。 正由于成天陶醉于这种脱世的意境之中,导...

分享包括但不限于计算机基础知识、数据结构与算法、Golang技术栈。年与时驰,意与日去,遂成枯落,多不接世,悲守穷庐,将复何及! 1670

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

HDU—— 2567 寻梦

题意:中文题目。 解题思路:

u014086857的专栏 709

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

HDU-2567-寻梦

寻梦 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4952    Accepted Submission(s): 3822 Problem Description 每个人的童年都可能梦想过自己成为一个英雄,尤其

保持对编程的热情! 717

hdu 2567 寻梦

#include #include #include #include using namespace std; int main() { int t; char str[100],str1[100]; int k; int i; while(cin>>t) { while(t--) { cin>>str>>str1; k=strlen(str); for(i

星空永恒 355

hdu2567 寻梦

#include #include int main() {     int n, B;     char s1[100], s2[100], s3[200], s4[200];     scanf("%d", &n);     while(n--)     {         memset(s1, 0, sizeof(s1));         memset(s2

Iguodala的博客 503

Eight HDU - 1043 (八数码,BFS逆向打表+康托展开hash)

Eight 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 constr...

codeswarrior的博客 922

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

HDU - 3567 Eight II IDA*

典型的IDA*题目 估值函数:从当前状态移动到目标状态所需的最小步数(我们可以通过曼哈顿距离进行估值),用于剪枝 迭代:此处我们不再使深度每次加1,而是在搜索过程当中,记录大于len(len表示此次搜索的限制深度)的所有值中的最小值,作为下次迭代的限制深度#include <iostream> #include <cstdio> #include <algorithm> #include <cs

WsMiracle的博客 462

hdu1238Substrings

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238 这题枚举子串和它的反串暴力就能过#include <stdio.h> #include<iostream> #include<cstring> #include<cstdlib> #include<cmath> using namespace std; char p[105][105]; cha

IDrandom的专栏 829

hdu2328Corporate Identity

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2328 找一个字典序最小的公共最长子串,枚举子串暴力就能过,这里有个小优化就是选取最短的字符串枚举其子串。#include <stdio.h> #include<iostream> #include<cstring> #include<cstdlib> #include<cmath> using nam

IDrandom的专栏 712

hdu4300Clairewd’s message(kmp||exkmp)( 模板 )

????:http://acm.hdu.edu.cn/showproblem.php?pid=4300 ??????to??????,?????????,?????????????,???????????,??????????(??+??) ???????????????????????????????ex[], ???s[]???,t[]???????,ex[i]?s[i]?t???????

IDrandom的专栏 613

hdu1686Oulipo

问子串在母串中出现了几次,kmp裸题。 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686#include <stdio.h> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; int nxt[10010]; char a[1000010];

IDrandom的专栏 462
上一篇: HDU1043 Eight(逆向BFS/A*/双向bfs/IDA*)
下一篇: hdu2181哈密顿绕行世界问题(dfs)
IDrandom
博客等级 码龄12年 31粉丝 125原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值