您的位置:首页 > 其它

hdu 1430 魔板 (BFS+预处理)

2013-06-28 21:55 429 查看
Problem - 1430

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

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <stack>
#include <string>

using namespace std;

char q[44444][10], op[44444], tmp[10];
int qh, qt, last[44444];
map<string, int> pos;

void op1(char *s) { reverse(s, s + 8);}

void op2(char *s, bool op) {
if (op) {
rotate(s, s + 3, s + 4);
rotate(s + 4, s + 5, s + 8);
} else {
rotate(s, s + 1, s + 4);
rotate(s + 4, s + 7, s + 8);
}
}

void op3(char *s, bool op) {
char t;
if (op) {
t = s[1];
s[1] = s[6];
s[6] = s[5];
s[5] = s[2];
s[2] = t;
} else {
t = s[1];
s[1] = s[2];
s[2] = s[5];
s[5] = s[6];
s[6] = t;
}
}

void PRE() {
for (int i = 0; i < 8; i++) tmp[i] = i + '0';
tmp[8] = 0;
pos.clear();
qh = qt = 0;

strcpy(q[qt], tmp);
pos[tmp] = qt;
last[qt] = -1;
op[qt++] = 0;
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[10], ed[10], con[10];
while (cin >> bg >> ed) {
for (int i = 0; i < 8; i++) {
int t = 0;
while (bg[i] != ed[t]) t++;
con[t] = i + '0';
}
con[8] = 0;
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 0;
}


View Code

——written by Lyon
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: