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

最新文章

  1. 2015.4.24 移动端,chrome不兼容或无法运行的一些具体问题
  2. Go Data Structures: Interfaces
  3. 把java对象转化为json格式的对象数组
  4. asp接收jquery post 中文乱码问题!
  5. google project tango 学习笔记
  6. vm克隆虚拟机网络配置
  7. css改变谷歌浏览器的滚动条样式
  8. gradle command not found
  9. otf字体转ttf字体
  10. 转 脸书pop动画的五个步骤
  11. 【转】android多分辨率适配
  12. latex命令替换之\newcommand
  13. 2016NEFU集训第n+3场 G - Tanya and Toys
  14. rest第一篇
  15. jq与原生js实现收起展开效果
  16. Android 7.0 Power 按键处理流程
  17. Akka(35): Http:Server side streaming
  18. C#创建IIS站点及相应的应用程序池,支持IIS6.0+Windows Server 2003. 使用Builder设计模式
  19. Perl多线程(2):数据共享和线程安全
  20. mysql case when * else end

热门文章

  1. java-File类-字节流
  2. 【python之路14】发送邮件实例
  3. spring boot 使用POI导出数据到Excel表格
  4. 洛谷 P1567 统计天数【最长上升子序列/断则归一】
  5. BootstrapValidator实现注册校验和登录错误提示效果(转)
  6. PHP正则表达式判断身份
  7. [Vue CLI 3] 插件开发之 registerCommand 到底做了什么
  8. Android基础&进阶
  9. phpcms 允许英文目录有空格
  10. SQLServer —— 视图