Problem - 1430

  跟八数码相似的一题搜索题。做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可。

代码如下:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <stack>
#include <string> using namespace std; char q[][], op[], tmp[];
int qh, qt, last[];
map<string, int> pos; void op1(char *s) { reverse(s, s + );} void op2(char *s, bool op) {
if (op) {
rotate(s, s + , s + );
rotate(s + , s + , s + );
} else {
rotate(s, s + , s + );
rotate(s + , s + , s + );
}
} void op3(char *s, bool op) {
char t;
if (op) {
t = s[];
s[] = s[];
s[] = s[];
s[] = s[];
s[] = t;
} else {
t = s[];
s[] = s[];
s[] = s[];
s[] = s[];
s[] = t;
}
} void PRE() {
for (int i = ; i < ; i++) tmp[i] = i + '';
tmp[] = ;
pos.clear();
qh = qt = ; strcpy(q[qt], tmp);
pos[tmp] = qt;
last[qt] = -;
op[qt++] = ;
while (qh < qt) {
strcpy(tmp, q[qh]);
op1(tmp);
if (pos.find(tmp) == pos.end()) {
strcpy(q[qt], tmp);
pos[tmp] = qt;
last[qt] = qh;
op[qt++] = 'A';
} strcpy(tmp, q[qh]);
op2(tmp, true);
if (pos.find(tmp) == pos.end()) {
strcpy(q[qt], tmp);
pos[tmp] = qt;
last[qt] = qh;
op[qt++] = 'B';
} strcpy(tmp, q[qh]);
op3(tmp, true);
if (pos.find(tmp) == pos.end()) {
strcpy(q[qt], tmp);
pos[tmp] = qt;
last[qt] = qh;
op[qt++] = 'C';
} qh++;
}
} int main() {
// freopen("in", "r", stdin);
PRE();
char bg[], ed[], con[];
while (cin >> bg >> ed) {
for (int i = ; i < ; i++) {
int t = ;
while (bg[i] != ed[t]) t++;
con[t] = i + '';
}
con[] = ;
int cur = pos[con];
stack<char> path;
while (!path.empty()) path.pop();
while (cur) {
path.push(op[cur]);
cur = last[cur];
}
while (!path.empty()) {
putchar(path.top());
path.pop();
}
puts("");
}
return ;
}

——written by Lyon

hdu 1430 魔板 (BFS+预处理)的更多相关文章

  1. HDU - 1430 魔板 (bfs预处理 + 康托)

    对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...

  2. hdu.1430.魔板(bfs + 康托展开)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  4. [HDU 1430] 魔板

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  5. HDU - 1430 魔板 【BFS + 康托展开 + 哈希】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...

  6. hdu 1430 魔板 康托展开 + 很好的映射

    http://acm.hdu.edu.cn/showproblem.php?pid=1430 如果从start ---> end,每一次都bfs进行,那么就肯定会超时. 考虑到先把start映射 ...

  7. HDU 3567 Eight II BFS预处理

    题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...

  8. hdu1430魔板(BFS+康托展开)

    做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...

  9. HDU 3533 Escape(BFS+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...

随机推荐

  1. iOS -NSOperation并发编程

    http://www.cocoachina.com/game/20151201/14517.html http://blog.csdn.net/qinlicang/article/details/42 ...

  2. 洛谷P1006 NOIP提高组2008 传纸条

    P1006 传纸条 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n 列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无 ...

  3. itextsharp 1.0

    1 效果图 2.代码 引用组件: using iTextSharp.text;using iTextSharp.text.pdf;using System;using System.Data;usin ...

  4. line-height的用法(一)

    行高”顾名思意指一行文字的高度.具体来说是指两行文字间基线之间的距离. 从上到下四条线分别是顶线.中线.基线.底线,很像才学英语字母时的四线三格,我们知道vertical-align属性中有top.m ...

  5. 层次分析法MATLAB

    输入成对比较矩阵,输出权重值和一致性检验结果. disp('请输入判断矩阵A(n阶)'); A=input('A='); [n,n]=size(A); x=ones(n,100); y=ones(n, ...

  6. Primitive Topology

    原文:Primitive Topology 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737/article/details/78 ...

  7. java 日期合法

    try { String date_str = "5555-22-33"; SimpleDateFormat format=new SimpleDateFormat("y ...

  8. 异步操作async await

    async函数的特点: 语义化强 里面的await只能在async函数中使用 await后面的语句可以是promise对象.数字.字符串等 async函数返回的是一个Promsie对象 await语句 ...

  9. SQL执行计划Cost与性能之间的的关系

    关于执行计划Cost的三个疑问: 1. 执行计划的Cost越低,SQL就一定跑得越快吗?或者说Cost 和 执行时间成比例关系吗? 2. Oracle 默认产生的执行计划是Cost最低的吗? 3. 如 ...

  10. Ubuntu 18.04中的Vim编辑器的高级配置

    VIM (Unix及类Unix系统文本编辑器) Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.VIM是开源软件. Vim普遍被推崇为类Vi编辑器中 ...