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

机械设计师必看:如何用Autodesk Inventor高效完成钣金设计(附实战案例) 本文为机械设计师详细解析了如何利用Autodesk Inventor Professional高效完成钣金设计。文章通过一个钣金防护罩的实战案例,系统介绍了从核心设计逻辑、建模流程、参数化技巧到工程图输出的完整工作流,旨在帮助设计师掌握智能化的设计方法,显著提升从概念到制造的设计效率与准确性。 阅读详情

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

华为od 面试八股文_Python语言_01_含答案 阅读详情

相关推荐

时序SDC约束set_data_check及常见应用场景

时序约束SDC之set_data_check命令及常见应用场景解析

weixin_37584728的博客 2286

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

Qt (高仿Visio)流程图组件开发(一) 效果展示及基本开发框架构思

最近因为工作原因需要使用Qt开发一个类似于Visio的流程图组件,需满足基本的流程图功能,图元类型可自定义扩展,不同图元存有各类自定义数据,可以保存为xml文件,可以读取xml文件生成流程图等等。

qq_43793182的博客 1万+

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

瑞萨RZN2L工业以太网开发避坑指南:EtherCAT从入门到通信调试全流程

本文详细解析了瑞萨RZN2L MPU在工业以太网开发中的EtherCAT从站配置与通信优化全流程。通过硬件设计关键点、协议栈选型、PDO映射策略及DC同步精度优化等实战技巧,帮助开发者快速搭建高效通信框架并解决调试中的常见问题,特别适合工业自动化领域的实时通信需求。

aitie1479的博客 461

AI 编程新范式:一文彻底搞懂 LLM、Agent、MCP、Skill 是怎么协作的

本文用通俗易懂的方式解析了AI编程新架构的核心概念。通过"三国演义"类比,文章将复杂的技术架构拆解为:Agent(军师/大脑)负责思考和决策,Skill(招式/手脚)执行具体操作,MCP(令箭/协议)作为通信标准,MCP Server(将军/身体)承载多个Skill。这套分工架构使AI从单纯聊天进化成能完成实际编程任务的全能助手,实现了关注点分离——Agent专注智能提升,Skill专注功能实现,MCP确保稳定连接。这正是Cursor、Claude等新一代AI开发工具采用的核心架构,标志

猫老板的豆 3077

HDU2567(字符串简单题)

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

代码艺术的道与术 1155

Android Binder——数据传递载体(二十一)

同一进程间的对象传递都是通过引用来做的,因而本质上就是传递了一个内存地址。而跨进程采用了虚拟内存机制,两个进程都有自己独立的内存地址空间,所以跨进程传递的地址值是无效的。进程间的数据传递是 Binder 机制中的重要一环,Android 系统中担负这一重任的就是 Parcel。Parcel 是一种数据的载体,用于承载希望通过 IBinder 发送的相关信息(包括数据和对象引用)。正是基于 Parcel 这种跨进程传输数据的能力,进程间的 IPC 通信才能更加平滑可靠。

小旭的专栏 576

HDU - 2567 寻梦

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

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

汽车ECU诊断刷写和OTA升级中的验签和校验

汽车ECU诊断刷写和OTA升级中的验签和校验

天赐好车的博客 617

Objective-C实现人工势场法(附完整源码)

Objective-C实现人工势场法(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 426

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的专栏 616

YOLOv8剪枝 稀疏训练+slim剪枝测试精度仅掉1.6个点,速度提高10.3fps!

摘要 本文介绍了YOLOv8剪枝方法Slim(Network Slimming),通过L1正则化作用于BN层的缩放因子(γ),自动识别并剪除冗余通道,实现模型轻量化。核心步骤包括:稀疏训练(引入L1正则化)、通道剪枝(按γ阈值裁剪)、微调恢复精度。实验表明,该方法能有效压缩模型大小、减少计算量,且支持通过调整speed_up等超参数控制剪枝强度。移植代码需修改C2F模块以适配剪枝,并配置稀疏训练参数(如reg、sl_epochs)。最终剪枝模型需移除优化器以减小体积,但不支持多卡训练。

FJN110的博客 861

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 2067

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

星空永恒 350

IDEA的使用指南

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

HDU—— 2567 寻梦

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

u014086857的专栏 709

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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的博客 502

HDU2567 寻梦【水题】

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

程序员充电站(itcharge) 1365

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 每个人的童年都可能梦想过自己成为一个英雄,尤其

保持对编程的热情! 716

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

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的博客 921

HDU - 3567 Eight II IDA*

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

WsMiracle的博客 459

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的专栏 828

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的专栏 710

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

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

IDrandom的专栏 610

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的专栏 458
上一篇: 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、付费专栏及课程。

余额充值