hdu 1430 魔板 (BFS+预处理)
跟八数码相似的一题搜索题。做法可以是双向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+预处理)的更多相关文章
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- [HDU 1430] 魔板
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- hdu 1430 魔板 康托展开 + 很好的映射
http://acm.hdu.edu.cn/showproblem.php?pid=1430 如果从start ---> end,每一次都bfs进行,那么就肯定会超时. 考虑到先把start映射 ...
- HDU 3567 Eight II BFS预处理
题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
随机推荐
- C#基础之特性
官网地址:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/attributes/accessing- ...
- day37 07-Hibernate二级缓存:查询缓存
查询缓存是比二级缓存功能更强大的缓存.必须把二级缓存配置好之后才能用查询缓存,否则是用不了的.二级缓存主要是对类的缓存/对象缓存.查询缓存针对对象也是可以的(因为功能比二级缓存更强大),而且还可以针对 ...
- 洛谷P1062 数列 [2017年6月计划 数论03]
P1062 数列 题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13,… ...
- RegExp实例方法和字符串的模式匹配方法的总结
RegExp实例方法 ECMAScript通过RegExp类型来支持正则表达式,创建正则表达式有两种方式: //使用字面量形式定义正则表达式 var pattern1 = /[bc]at/i //使用 ...
- SQL —— 存储过程
一.什么是存储过程 预先存储好的SQL程序. 保存在SQL Server中(跟视图的存储方式一样) 通过名称和参数执行. 二.存储过程的优点 执行速度更快 允许模块化程序设计 提高系统安全性 减少网络 ...
- Header解析
不管是作为后端还是前端开发人员,对于web请求的过程和参数都是需要了解的. 下面是对一次简单的http请求的header分析,作为自己的一个总结,也希望对大家有所帮助. 以Chrome为例: 我们对h ...
- 当inline-block和text-indent遇到IE6,IE7
在实际应用中,考虑到seo,很多button,icon都要用到inline-block和text-indent来处理,例如: <a href="#" class=" ...
- Python使用Pandas高效处理测试数据
转自:https://www.cnblogs.com/keyou1/p/10948796.html 一.思考 1.Pandas是什么? 功能极其强大的数据分析库 可以高效地操作各种数据集 csv格式的 ...
- PHP队列类
/** * Created by PhpStorm. * User: LAMP-Q哥 * Date: 2017/8/3 * Time: 12:58 */ class Queue { private $ ...
- Spring_使用(JDBC)
Spring_对JDBC的支持 使用JdbcTemplate更新数据库 导入jar包 创建applicationcontext.xml <?xml version="1.0" ...