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