HDU 1043 Eight(bfs+康托展开)

AD(AltiumDesigner)封装导入allegro生成allegro封装 AD(AltiumDesigner)封装导入allegro生成allegro封装 貌似PCB的封装问题是一个无尽话题,为了简单我总是想找捷径(主要还是太懒了),因为也是近期才开始学习使用allegro进行PCB设计,在最近的一个设计中,很多器件封装allegro中都没有,一个一个画感觉很费时间,但是在AD(AltiumDesigner)的封装库中是有的,于是便想着AD的封装库能不能倒入到alleg... 阅读详情

Eight

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15216    Accepted Submission(s): 4235
Special Judge


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

Source


/*

对于一个9的序列,变成3*3宫格,x表示可以移动的,求这样使他回到有序
123
456
780

思路:康托hash,然后path就录当前状态是由哪一个状态变来,
                然后di[][]表示当前状态由前一个状态由哪一个方向变来

*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;
#define bug printf("hi\n");
using namespace std;

#define INF 0x3f3f3f3f
#define N  400000

int fac[10];
int di[N],pre[N];
int vis[N];

struct stud{
 int a[10];
}f[N];

int step[4][2]={1,0,-1,0,0,1,0,-1};
char c[]="udlr";       //因为要逆着走,所以方向相反


int start;

void inint()
{
    int i,j;
    fac[0]=1;
    for(i=1;i<9;i++)
        fac[i]=fac[i-1]*i;
}

int cantor(int a[])
{
    int temp,i,j;
    temp=0;
    int cnt;
    for(i=0;i<9;i++)
    {
       cnt=0;
       for(j=i+1;j<9;j++)
            if(a[i]>a[j]) cnt++;
       temp+=cnt*fac[9-i-1];
    }
  return temp;
}

void bfs()
{
    int i,j;
    int head,tail;
    stud cur,next;

    for(i=0;i<8;i++)
        cur.a[i]=i+1;
    cur.a[8]=0;

    start=cantor(cur.a);

    memset(vis,0,sizeof(vis));
    vis[start]=1;

    head=tail=0;
    f[tail++]=cur;
    while(head<tail)   //这里最好不要用queue,不然会超时
    {
        cur=f[head];
        head++;
        for(i=0;i<9;i++)
            if(cur.a[i]==0) break;

        int x=i/3;
        int y=i%3;

        for(i=0;i<4;i++)
        {
            int xx=x+step[i][0];
            int yy=y+step[i][1];
            int id=cantor(cur.a);
            if(xx<0||xx>=3||yy<0||yy>=3) continue;
            next=cur;
            swap(next.a[x*3+y],next.a[xx*3+yy]);
            int temp=cantor(next.a);
            if(!vis[temp])
            {
                vis[temp]=1;
                pre[temp]=id;
                di[temp]=i;
                f[tail++]=next;
            }
            swap(next.a[x*3+y],next.a[xx*3+yy]);
        }
    }
}

int main()
{
    inint();
    int i,j;
    bfs();

    int a[10];
    char s[10];
    while(~scanf("%1s",s))
    {
        if(s[0]=='x') a[0]=0;
        else a[0]=s[0]-'0';

        for(i=1;i<9;i++)
        {
            scanf("%1s",s);
            if(s[0]=='x') a[i]=0;
            else a[i]=s[0]-'0';
        }

        int temp=cantor(a);
        if(!vis[temp])
            printf("unsolvable\n");
        else
        {
           while(temp!=start)
           {
               printf("%c",c[di[temp]]);
               temp=pre[temp];
           }
           printf("\n");
        }
    }
     return 0;
}





IEEE Transactions on Industrial Informatics(TII) 投稿须知- Initial Submission 请注意,您可以在作者中心查看稿件的当前状态,如:在EIC办公室、分配给AE、AE邀请审稿人、AE指定审稿人、正在审稿、等待AE决定、等待EIC决定。为了确保公正的评审过程,我们要求在稿件中没有任何作者的身份(不包括作者的姓名、传记、隶属关系、求职信、签署的版权表格、识别脚注、致谢等)。如果你的手稿有很大一部分已经在会议上发表过,论文必须隐藏所有作者的身份,这样会议论文就可以被引用,就好像它是由不同的作者写的一样。请注意,通常你的技术成就是根据被引用的次数来评估的,而不是根据发表的论文数量。 阅读详情

相关推荐

ESP32水墨屏日历实战:低功耗自动更新与HTTPS数据解析

本文详细介绍了ESP32水墨屏日历项目的开发实践,重点讲解了低功耗自动更新与HTTPS数据解析的实现方法。通过ESP32的自动休眠功能和水墨屏的零功耗特性,结合HTTPS请求安全获取网络数据,实现了高效节能的桌面日历应用。项目代码开源,适合物联网开发者参考。

xx56789的博客 777

八数码问题

题目描述 在一个3*3的棋盘上放置编号为1-8的8个方块,每个占一格,另外还有一个空格。与空格相邻的数字方块可以移动到空格里。任务1:指定初始棋局和目标棋局,计算出最少的移动步数;任务2:输出数码的移动序列。 把空格看成0,一共有9个数字。 输入样例: 1 2 3 0 8 4 7 6 5 1 0 3 8 2 4 7 6 5 输出样例: 2 题目分析 这里先解决任务1,任务2是在任务1的基础上通过标记序列和回溯去解决的。 把一个棋局看成一个状态图,总共有9!= 362880个状态。从初始棋局开始,每次移

weixin_47750287的博客 1905

Aveva Marine C# 二次开发入门001

1# 引用 C:\AVEVA\Marine\OH12.1.SP4\Aveva.ApplicationFramework.dll C:\AVEVA\Marine\OH12.1.SP4\Aveva.ApplicationFramework.Presentation.dll 2# 引用命名空间, using Aveva.ApplicationFramework.Presentation;usi...

weixin_30245867的博客 1911

hdu1043 Eight--分别用A*和IDA*的解法

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

344

从零开始使用matlab实现EKF-SLAM算法(含完整代码)

扩展卡尔曼滤波器同步定位与建图 (EKF-SLAM) 算法建立在 EKF 的基础上,集成了 EKF 的功能,可解决机器人技术中最具挑战性的问题之一:同时定位与建图 (SLAM)。 EKF-SLAM采用EKF方法来同时估计机器人的状态和周围环境的布局,而无需事先了解位置。 这种双重功能对于在未知环境中运行的自主系统至关重要,使它们能够在绘制区域地图时有效导航。 EKF-SLAM 算法一直是自动驾驶车辆和机器人系统开发的基石,为处理现实世界环境中固有的不确定性提供了一个强大的框架。

mjmmm的博客 6634

基于MS5351的三通道可编程时钟发生器设计

时钟发生器是数字系统时序验证与信号完整性测试的核心基础设备,其原理基于锁相环(PLL)对参考晶振进行倍频与分频控制,通过寄存器配置实现多路独立频率输出。技术价值体现在高精度、低抖动、快速切换与工程可部署性之间取得平衡,尤其适用于FPGA/ASIC参考时钟调试、ADC/DAC采样时钟验证及嵌入式硬件时序裕量评估等场景。本方案采用MS5351三通道时钟芯片与ESP32主控协同架构,在保证LVCMOS输出质量与I²C可编程性的前提下,优化电源噪声抑制、晶体负载匹配和跨板信号完整性,兼顾成本、周期与实测稳定性,为实

weixin_31715353的博客 210

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

新手避坑指南:用Silvaco Atlas搞定GaN HEMT电热仿真的完整流程(附代码解读)

本文详细介绍了使用Silvaco Atlas进行GaN HEMT电热特性仿真的完整流程,特别针对新手常见问题提供避坑指南。从软件环境配置、结构定义、网格划分到物理模型设置,逐步解析关键步骤,并附有代码解读和调试技巧,帮助读者快速掌握GaN器件仿真的核心技术。

weixin_29215815的博客 596

八数码问题 bfs+map

八数码问题也称为九宫问题。在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同。棋盘上还有一个空格,与空格相邻的棋子可以移到空格中。要求解决的问题是:给出一个初始状态和一个目标状态,找出一种从初始转变成目标状态的移动棋子步数最少的移动步骤。(用0表示空格) 思路:把八数码问题归结为图上的最短路问题。开始状态对应起点,目标状态对应终点。用广搜去搜索就可以了

leon_liuqinburen的博客 1959

CP AUTOSAR标准之AUTOSAR OS(AUTOSAR_SWS_OS)(二)

AUTOSAR OS是一个Operating System,即操作系统。它最核心的功能是管理任务和事件的调度,不同任务间的同步流,并且提供监控和错误处理功能。或者用最简化理解就是一个函数调度器。

CoolBreeze的博客 653

python代码实现了一个基于图注意力网络(GAT)和长短期记忆网络(LSTM)的股票成交量预测模型

自定义数据集类:定义了类,继承自。该类用于生成股票数据的时间序列样本,每个样本包含一个长度为seq_length的时间序列和对应的目标成交量。数据加载器:使用DataLoader对数据集进行批量加载,设置批量大小为 64,并开启数据打乱功能。GAT - LSTM 模型:定义了GAT_LSTM类,继承自。该模型结合了 GAT 和 LSTM 两种神经网络:GAT 部分:使用GATConv处理全局图特征,捕获股票之间的相互关系。LSTM 部分:使用nn.LSTM处理时间序列数据,捕捉时间序列的动态变化。

huanghm88的专栏 424

HDU-1043 逆向bfs 打表

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

刷题记录 321

开源免费!ReaLTaiizor如何用MIT许可证打造专业级WinForm UI?

摘要: ReaLTaiizor是一款开源UI控件库,为WinForm应用提供现代化界面解决方案。支持100+组件、多主题切换(浅色/深色/自定义)和.NET 4.8-8.0全版本兼容,MIT许可证允许免费商用。通过NuGet快速安装,3步改造传统界面:1)注入主题(如this.Theme = new Dark()),2)替换原生控件为高颜值组件(如ModernGrid替代DataGridView),3)集成高级功能(分页/动画提示)。典型场景包括登录界面美化(进度动画+通知控件)和数据表格Material风

墨夶的博客 1520

[ACM]【康托展开/BFS】HDU1043 Eight

Eight 经典的八数码问题,赛前重温顺便记录一下。 传送门 题意:3*3的方阵,由1-8的数字占据每一个格子,还有一个空格。可以通过空格改变方阵中数字的位置。输入一个方阵,求变为123/456/78的样式的移动步骤。 思路: 移动空格周遭的数字,其实等价于移动空格。 于是,我们把空格的位置作为状态。原本二维的位置转换成一维位置进行记录。 逆向思维,从目标状态开始BFS,每到一个新状态就记录父亲状态到新状态的移动方式,和父亲状态。这样就可以通过状态转移来得到路径。 最重要的问题是如何判重。 因为这里有9个

Artist's Garden 233

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

HDU 1043 Eight(康托展开+BFS)

题目大意 ​ 八数码问题,输入9个字符代表一个3*3的棋盘,对于能还原的输入,输出一段包含 u、d、l、r 操作的操作序列,不能还原的输入输出“unsolvable” 思路 ​ 输入有多组,所以考虑先预处理出所有情况的操作序列,再根据输入进行输出 ​ 因为要遍历所有情况,所以选择 BFS 即可,从结果出发,每操作一次,储存一次该种情况对应的操作序列 ​ 另外对于棋盘的所有情况,与全排列类似...

IuSpet的博客 256

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

HDU 1043 Eight (bfs 康托展开 )

Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 32621    Accepted Submission(s): 8488Special JudgeProblem Description The 15-puzzle ha...

YIU 228

IEM模型,地表粗糙度模拟

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

HDU - 1043(Eight)逆BFS和康托展开

68

Zoran的博客 211

【HDU - 1043】Eight(反向bfs+康托展开)

Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8 在上图中,由于右下角位置是空的,你可以移动数字,比如可以将数字6下移一位: ...

weixin_30518397的博客 199

hdu 1043 Eight BFS 康托展开

EightTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28349    Accepted Submission(s): 7540Special JudgeProblem DescriptionThe 15-puzzle has bee...

popcjz的博客 1688

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

Yi_Qing_Z的博客 385

HDU-1043 Eight(BFS+康托展开)详解

HDU-1043 Eight(BFS+康托展开)详解

qwertf123的博客 822

hdu1043 Eight(A*/双向BFS/单项BFS打表+康托展开)

题目链接:点击打开链接 题意描述:经典八数码问题,给定八数码的初始序列,求经过u、r、l、d四种操作到达1 2 3 4 5 6 7 8 x的状态,打印出操作序列? 解题思路:A*/双向BFS/单项BFS打表+康托展开   202msAC 方法一:BFS逆向打表+康托展开:从1 2 3 4 5 6 7 8 x逆向扩展,记录哪些状态可以到达,以及到达给状态操作 代码: #includ

Crazy_Bear 2240

hdu1043Eight (经典的八数码)(康托展开+BFS)

建议先学会用康托展开: 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 num

青山绿水之辈 专栏 3429

Eight HDU 1043 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 p...

HXX904的博客 264

[BFS+康托展开]Hdu 1043 Eight

题目传送门这道题就是我们耳熟能详的八数码问题,如果写BFS必然涉及到如何判断一个状态是否进队,可以把序列表示成一个9进制的数,用set或者直接hash,但是这样操作效率过低。0~8的排列最多只有9!种排列方式,所以如果我们能很快求出每个序列的大小,那么直接开vis标记就可以了。所以引进了康托展开。康托展开假设我们拿到一个序列是32451,求这个序列的在所有排列中的次序。 其实我们只要确定有几个排列

CHN_JZ的博客 694

HDU 1043 Eight(反向BFS打表+康托展开)

原题链接:Here! 分析:求经过若干次四个操作 ' r ' , ' l ' , ' u ' , ' d ' 到状态1 2 3 4 5 6 7 8 x,输出操作顺序。因为最终状态一定,所以采用反向BFS打表,记录路径即可。 CODE: /* Note: 因为题目要求是任意状态求是否能到一个"完美状态",因为任意状态不确定性和完美状态的确定性,所以不妨 反向BFS+打表 抠脚

OhohOh 430
上一篇: HDU 1026 Ignatius and the Princess I(bfs +记录路径)
下一篇: POJ 2970 The lazy programmer(优先队列+贪心)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值