练习代码,已经测试.
#include<iostream>
#include<bitset>
using namespace std;
int goalStatus;//目标序号
bitset<362880> Flags;//标记已用序号
const int MAXS = 370000;
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];//状态队列
int qHead; int qTail;//队头,队尾的'指针'
char sz4Moves[] = "udrl";//四个移动动作
unsigned int factorial[21];//阶乘的值数组
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 Bfs(int startStatus) {//广搜
int nNewStatus;
int Status;
Flags.reset();
qHead = 0; qTail = 1;
myQueue[qHead] = Node(startStatus, -1, 0);
Flags.set(startStatus, 1);//被使用
while (qHead != qTail)
{
Status = myQueue[qHead].status;
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]);
}
qHead++;
}
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");
Bfs(goalStatus);//预置,反推结果的所有可能状态
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;
}
int nStatus = StrStatusToIntStatus(szLine2);
int nPos;
if (Flags[nStatus] == 1) {//输入的序号是不是已经在队列中,如果有找到位置
{for (int i = 0; i < qTail; ++i)
if (myQueue[i].status == nStatus)
nPos = i;
}
do {
switch (myQueue[nPos].move)//反转移动方向
{
case 'u': {cout << 'd'; break; }
case 'd': {cout << 'u'; break; }
case 'l': {cout << 'r'; break; }
case 'r': {cout << 'l'; break; }
}
nPos = myQueue[nPos].father;
} while (nPos);
}
else {
cout << "unsolvable" << endl;
continue;
}
}
return 0;
}