说实话这题确实挺菜的。。。
废话少说,直接上代码^O^
Code:
#include
using namespace std;
inline int read() {
int x = , f = ; char ch = getchar();
for ( ; !isdigit(ch) ; ch = getchar()) if (ch == '-') f = -;
for ( ; isdigit(ch) ; ch = getchar()) x = x * + ch - '';
return x * f;
}
const int fac[] = { , , , , , , , , , };
const int aim = ;
const int maxCon = ;
bool vis[maxCon];
struct data {
int con[];
int hashVal;
int step;
}_begin;
queue Q;
int Cantor(int s[]) {
int sum = ;
for (int i = ; i <= ; i ++) {
int cnt = ;
for (int j = i + ; j <= ; j ++) {
if (s[i] > s[j]) {
cnt ++;
}
}
sum += cnt * fac[ - i];
}
return sum + ;
}
int bfs() {
Q.push(_begin);
while (Q.size()) {
data _top = Q.front(); Q.pop();
if (vis\[\_top.hashVal\]) {
continue;
}
if (\_top.hashVal == aim) {
return \_top.step;
}
vis\[\_top.hashVal\] = true;
\_top.step ++;
data \_next = \_top;
for (int i = ; i <= ; i ++) {
swap(\_next.con\[i\] , \_next.con\[i + \]);
}
\_next.hashVal = Cantor(\_next.con);
Q.push(\_next);
\_next = \_top;
int \_up = \_next.con\[\] , \_dwn = \_next.con\[\];
for (int i = ; i >= ; i --) {
\_next.con\[i\] = \_next.con\[i - \];
\_next.con\[i + \] = \_next.con\[i + \];
}
\_next.con\[\] = \_up;
\_next.con\[\] = \_dwn;
\_next.hashVal = Cantor(\_next.con);
Q.push(\_next);
\_next = \_top;
int two = \_next.con\[\] , three = \_next.con\[\] , seven = \_next.con\[\] , six = \_next.con\[\];
\_next.con\[\] = six , \_next.con\[\] = two , \_next.con\[\] = three , \_next.con\[\] = seven;
\_next.hashVal = Cantor(\_next.con);
Q.push(\_next);
}
}
int main() {
for (int i = ; i <= ; i ++) {
_begin.con[i] = read();
}
_begin.hashVal = Cantor(_begin.con);
printf("%d\n" , bfs());
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章