代码测试通过.
#include<iostream>
#include<bitset>
using namespace std;
int goalStatus;//目标序号
bitset<362880> Flags;//标记已用序号
bitset<362880>gFlags;
const int MAXS = 370000;
char result[MAXS];//移动的过程
struct Node
{
int status;//排列序号
int father;//父序号
char move;//移动到当前序号的动作
Node(int s, int f, int m) :status(s), father(f), move(m) {}
Node() {}
};
Node myQueue[MAXS];//状态队列
Node goalQueue[MAXS];//队列2
int qHead; int qTail;//队头,队尾的'指针'
int gHead; int gTail;//第二组队列的'指针'
int qEnd=0;//记录从哪个队列返回
char sz4Moves[] = "udrl";//四个移动动作
unsigned int factorial[21];//阶乘的值数组
char reslt[MAXS];//暂存移动的状态
unsigned int GetPermutationNumForInt(int* perInt, int len) {
//从数值排列perInt计算序号
bool numUsed[21];//数字使用标记
unsigned int num = 0;
memset(numUsed, 0, sizeof(numUsed));
for (int i = 0; i < len; i++)//循环每一位数
{
int n = 0;
numUsed[perInt[i]] = 1;//标记第i位数已使用
for (int j = 0; j < perInt[i]; j++) {
if (!numUsed[j])
++n;
}
num += n * factorial[len - 1 - i];
}
return num;//从0开始所以不加上它自身的状态
}
template<class T>
unsigned int GetPermutationNum(T s1, T s2, int len) {
//给定任意排列求序号,s1是第一个排列,s2待求的排列
int* perInt = new int[len];
for (int i = 0; i < len; i++)//把任意排列转为数值排列perInt
for (int j = 0; j < len; j++)
if (*(s2 + i) == *(s1 + j))
{
perInt[i] = j;
break;
}
unsigned int num = GetPermutationNumForInt(perInt, len);
delete[] perInt;//清空
return num;//返回序号
}
template<class T>
void GetPermutationByNum(T s1, T s2, int len, unsigned int No) {
//根据序号求任意排列,s1是第一个排列
int* perInt = new int[len];//存排列的数值
bool* numUsed = new bool[len];//已使用标记
memset(numUsed, 0, sizeof(bool) * len);//numUsed是指针不能放在第三位,注意
for (int i = 0; i < len; i++) { //第i位
int j;
for (j = 0; j < len; j++) {
if (!numUsed[j])
{
if (factorial[len - 1 - i] >= No + 1)
break;
else
No -= factorial[len - 1 - i];
}
}
perInt[i] = j;//找到的数放入排列
numUsed[j] = 1;
}
for (int i = 0; i < len; i++) {
*(s2 + i) = *(s1 + perInt[i]);//转换成任意形式的排列s2
}
delete[] perInt;
delete[] numUsed;
}
unsigned int StrStatusToIntStatus(const char* strStatus) {
//从字串排列求序号
return GetPermutationNum("012345678", strStatus, 9);
}
void IntStatusToStrStatus(int n, char* strStatus) {
//从序号求字串排列
GetPermutationByNum((char*)"012345678", strStatus, 9, n);//为了类型统一,加上(char*)
}
int NewStatus(int nStatus, char cMove) {
//从序号nStatus,通过cMove移动为新排列序号
char szTmp[20];//保存序号对应的字串排列
int nZeroPos;//0的位置
IntStatusToStrStatus(nStatus, szTmp);//序号求字符排列
for (int i = 0; i < 9; i++)//找0位
if (szTmp[i] == '0') {
nZeroPos = i;
break;
}
switch (cMove) {//移动0的位置
case'u':if (nZeroPos < 3)return -1;//最上排,不能上移
else {
szTmp[nZeroPos] = szTmp[nZeroPos - 3];
szTmp[nZeroPos - 3] = '0';
break;
}
case'd':if (nZeroPos > 5)return -1;
else {
szTmp[nZeroPos] = szTmp[nZeroPos + 3];
szTmp[nZeroPos + 3] = '0';
break;
}
case'r':if (nZeroPos % 3 == 2) return -1;
else {
szTmp[nZeroPos] = szTmp[nZeroPos + 1];
szTmp[nZeroPos + 1] = '0';
break;
}
case'l':if (nZeroPos % 3 == 0) return -1;
else {
szTmp[nZeroPos] = szTmp[nZeroPos - 1];
szTmp[nZeroPos - 1] = '0';
}
}
return StrStatusToIntStatus(szTmp);
}
bool Dbfs(int startStatus,int goalStatus) {//双向广搜
int nNewStatus;
int Status; int gStatus;
Flags.reset();
gFlags.reset();
qHead = 0; qTail = 1;
gHead = 0; gTail = 1;
myQueue[qHead] = Node(startStatus, -1, 0);
goalQueue[gHead] = Node(goalStatus,-1,0 );
Flags.set(startStatus, 1);//被使用
gFlags.set(goalStatus, 1);
while (qHead != qTail||gHead!=gTail)
{
Status = myQueue[qHead].status;
gStatus = goalQueue[gHead].status;
if (qTail <= gTail)//队列短的扩展
{
for (int i = 0; i < 4; i++)//四个方向移动
{
nNewStatus = NewStatus(Status, sz4Moves[i]);
if (nNewStatus == -1) continue;//不能移
if (Flags[nNewStatus]) continue;//状态已存在
Flags.set(nNewStatus, 1);
myQueue[qTail++] = Node(nNewStatus, qHead, sz4Moves[i]);
if (gFlags[nNewStatus] == 1)
return true;//序号在另一个队列里找到,结束
}
qHead++;
}
else
{
for (int i = 0; i < 4; i++)//四个方向移动
{
nNewStatus = NewStatus(gStatus, sz4Moves[i]);
if (nNewStatus == -1) continue;//不能移
if (gFlags[nNewStatus]) continue;//状态已存在
gFlags.set(nNewStatus, 1);
goalQueue[gTail++] = Node(nNewStatus, gHead, sz4Moves[i]);
if (Flags[nNewStatus] == 1) {
qEnd = 1;//找到的节点在第二队列
return true; }//序号在另一个队列里找到,结束
}
gHead++;
}
}
return false;//没有找到
}
int main() {
factorial[0] = factorial[1] = 1;
for (int i = 2; i < 21; i++)
factorial[i] = i * factorial[i - 1];
goalStatus = StrStatusToIntStatus("123456780");
char szLine[50];
char szLine2[20];
while (cin.getline(szLine, 48)) {//清洗字符放入szLine2
int j = 0;
for (int i = 0; szLine[i]; i++) {
if (szLine[i] != ' ') {//跳过空字符
if (szLine[i] == 'x')szLine2[j++] = '0';//替换字符x,
else szLine2[j++] = szLine[i];
}
}
szLine2[j] = 0;//字符串最后一个字符为0
int sumGoal = 0;
for (int i = 0; i < 8; i++)//"123456780"的奇偶性
sumGoal += i;
int sumOri = 0;
for (int i = 0; i < 9; i++)//判断奇偶性
for (int j = 0; j < i; j++)
if (szLine2[j] < szLine2[i] && szLine2[i] != '0' && szLine2[j] != '0')//奇偶性必须忽略0
{
++sumOri;
continue;
}
if (sumGoal % 2 != sumOri % 2)
{
cout << "unsolvable" << endl;
continue;
}
char nMoves = 0;
int nPos = qTail-1;
if (Dbfs(StrStatusToIntStatus(szLine2),goalStatus)) {
if (qEnd == 0)//标志为0,共有序号在myQueue的末尾
{
memset(reslt, 0, sizeof(reslt));
nPos = qTail - 1;
do {
reslt[nMoves++] = myQueue[nPos].move;
nPos = myQueue[nPos].father;
} while (nPos);
for (int i = nMoves - 1; i >= 0; i--)
cout << reslt[i];//反向输出myQueue中的移动
for(int i=gTail-1;i>=0;i--)
if (goalQueue[i].status == myQueue[qTail - 1].status) {
nPos = i;
break; }
do{
switch (goalQueue[nPos].move)//反向goalQueue里的移动
{
case 'u': {cout << 'd'; break; }
case 'd': {cout << 'u'; break; }
case 'l': {cout << 'r'; break; }
case 'r': {cout << 'l'; break; }
}
nPos = goalQueue[nPos].father;
} while (nPos);
}
else
{
memset(reslt, 0, sizeof(reslt));
for (int i = qTail - 1; i >= 0; i--)//共同序号在goalQueue末尾
if ( myQueue[i].status==goalQueue[gTail-1].status ) {
nPos = i;
break;
}
nMoves = 0;
do {
reslt[nMoves++]= myQueue[nPos].move;
nPos = myQueue[nPos].father;
} while (nPos);
for (int i = nMoves - 1; i >= 0; i--)
cout << reslt[i];
nPos = gTail - 1;
do {
switch (goalQueue[nPos].move)//反转goalQueue里的移动方向
{
case 'u': {cout << 'd'; break;}
case 'd': {cout << 'u'; break;}
case'l': {cout << 'r';break;}
case'r': {cout << 'l';break;}
}
nPos = goalQueue[nPos].father;
} while (nPos);
}
}
else {
cout << "unsolvable" << endl;
continue;
}
}
return 0;
}