【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=1043

【题意】



会给你很多组数据;

让你输出这组数据到目标状态的具体步骤;

【题解】



从12345678x这个目标状态开始bfs然后获取它能够到达的所有状态;

然后在队列中记录下执行这一步用的是那个操作;

递归输出就好;

map来判定这状态存不存在;

在队列里面存这个状态

就按照所给的存成1维的数组就好;

x用9代替;(用0也可以哦);

模拟八数码的操作就好;

因为是逆序的,所以记录的操作是相反的;

最后递归写输出的时候搞晕了;

又WA了好多次.



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 200e4;
const int chushi[10] = { 0,1,2,3,4,5,6,7,8,9 }; struct abc
{
int s[15];
int pos,ope,pre;
}; map<int, int> dic;
abc dl[N];
int ts[15];
string t; int zt(int s[15])
{
int r = 0;
rep1(i, 1, 9)
r = r * 10 + s[i];
return r;
} void bfs()
{
abc temp,temp1;
rep1(i, 0, 9) temp.s[i] = chushi[i];
temp.pos = 9;
dic[zt(temp.s)] = 1;
int head = 1, tail = 1;
dl[1] = temp;
while (head <= tail)
{
abc now = dl[head++];
int ss[15];
rep1(i, 1, 9) ss[i] = now.s[i];
int t = now.pos;
//up
if (t > 3)
{
int tt = t - 3;
swap(ss[tt], ss[t]);
int pc = zt(ss);
if (!dic[pc])
{
rep1(i, 1, 9) temp1.s[i] = ss[i];
temp1.pos = tt, temp1.pre = head - 1, temp1.ope = 2;
dl[++tail] = temp1;
dic[pc] = tail;
}
swap(ss[tt], ss[t]);
}
//down
if (t<7)
{
int tt = t + 3;
swap(ss[tt], ss[t]);
int pc = zt(ss);
if (!dic[pc])
{
rep1(i, 1, 9) temp1.s[i] = ss[i];
temp1.pos = tt, temp1.pre = head - 1, temp1.ope = 1;
dic[pc] = tail+1;
dl[++tail] = temp1;
}
swap(ss[tt], ss[t]);
}
//left
if ((t % 3) != 1)
{
int tt = t - 1;
swap(ss[tt], ss[t]);
int pc = zt(ss);
if (!dic[pc])
{
rep1(i, 1, 9) temp1.s[i] = ss[i];
temp1.pos = tt, temp1.pre = head - 1, temp1.ope = 4;
dic[pc] = tail+1;
dl[++tail] = temp1;
}
swap(ss[tt], ss[t]);
}
//right
if ((t % 3) != 0)
{
int tt = t + 1;
swap(ss[tt], ss[t]);
int pc = zt(ss);
if (!dic[pc])
{
rep1(i, 1, 9) temp1.s[i] = ss[i];
temp1.pos = tt, temp1.pre = head - 1, temp1.ope = 3;
dic[pc] = tail+1;
dl[++tail] = temp1;
}
swap(ss[tt], ss[t]);
}
}
} void pri(int now){
if (now == 1) return;
int ju = dl[now].ope;
if (ju == 1) printf("u");
if (ju == 2) printf("d");
if (ju == 3) printf("l");
if (ju == 4) printf("r");
pri(dl[now].pre);
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
bfs();
while (getline(cin,t)){
int num = 0;
int len = t.size();
rep1(i,0,len-1)
{
if (t[i]=='x') t[i] = '9';
if (t[i]>='0' && t[i]<='9') ts[++num] = t[i]-'0';
if (num==9) break;
}
int pc=dic[zt(ts)];
if (pc){
pri(pc);
puts("");
}
else
puts("unsolvable");
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【hdu 1043】Eight的更多相关文章

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

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

  2. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  3. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  4. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  5. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  6. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  7. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  8. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

  9. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

随机推荐

  1. 全分布式的Hadoop虚拟机安装

    在集群环境下装机.配置.运行的全过程,梳理总结到本文中. 第一部分:环境规划 •用户 hadoop 密码 hadoop •机器 机器名称 IP地址 Master.Hadoop 192.168.1.10 ...

  2. sql2000数据库置疑造成的原因以及如何解决置疑

    造成数据库置疑一般有以下几点: 1)电脑非法关机或者意外停电: 2)磁盘有坏道或者损坏: 3)数据库感染病毒,日志文件损坏: 4)非正常情况下移动数据库文件 5)系统,硬盘,经常强制性关机(如断电)类 ...

  3. 【第三届强网杯】write up

    一,鲲or鳗orGame 从浏览器里拿到game.gb 用 VisualBoyAdvance ,搜索内存变量,然后改变量就完事了,但是改变量他们说直接利用添加代码功能有bug,所以要在工具里的内存查看 ...

  4. composer查看安装情况

    composer install --no-progress --profile -vvv

  5. [Qt Creator 快速入门] 第8章 界面外观

    一个完善的应用程序不仅应该有实用的功能,还要有一个漂亮的外观,这样才能使应用程序更加友好,更加吸引用户.作为一个跨平台的UI开发框架,Qt提供了强大而灵活的界面外观设计机制.这一章将学习在Qt中设计应 ...

  6. 【转】数据倾斜是多么痛?spark作业/面试/调优必备秘籍

    原博文出自于: http://sanwen.net/a/gqkotbo.html 感谢! 来源:数盟 调优概述 有的时候,我们可能会遇到大数据计算中一个最棘手的问题——数据倾斜,此时Spark作业的性 ...

  7. elastic-job 的简单使用

    说明:这个是使用2.1.5版本 elastic-job是当当开源的的的定时任务,使用也是很简单的,可以解决数据量的大的时候可以分片执行,多应用节点部署时候不会重复执行. 是通过zookeeper作为控 ...

  8. 第一次创建svn的项目的使用方法

    1.第一步.在服务器上创建svn项目,将开发人人员你的账号密码添加上去. 2.第二步.开始在本地创建一个文件夹,点文件夹,右键->tortoisSVN->repo-brower 填写svn ...

  9. Farseer.net轻量级开源框架 入门篇:查询数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 下一篇:Farseer.net轻量级开源框架 中级篇: Where条 ...

  10. GLPI开源资产管理系统

    GLPI一款资产管理系统,功能比较强大,东西比较多,放张图,有机会再深入研究