源代码(注释在代码里面)
#include <iostream>
using namespace std;
#define MAXLISTSIZE 10000
#define MAXSTEPSIZE 100
/
struct ENode
{
int status[9];
int G;
int H;
int F;
int Zero;
int step;
ENode *Parent;
};
int FinalStatus[9] = { 1, 2, 3, 8, 0, 4, 7, 6, 5 };
ENode OPEN[MAXLISTSIZE];
ENode CLOSE[MAXLISTSIZE];
int open = 0;
int close = 0;
ENode *Node;
int CountH(int *status)
{
int H = 0;
int i;
for (i = 0; i <= 8; i++)
{
if (FinalStatus[i] != status[i])
{
H++;
}
}
return H;
}
int Exist(ENode *N)
{
int i, j;
int H = 0;
int status[9];
Node = new ENode;
Node = N;
for (i = 0; i <= 8; i++)
{
status[i] = Node->status[i];
}
for (i = 0; i <= open - 1; i++)
{
for (j = 0; j <= 8; j++)
{
if (status[j] != OPEN[i].status[j])
{
H++;
}
}
if (H == 0)
{
return i + 1;
}
H = 0;
}
for (i = 0; i <= close - 1; i++)
{
for (j = 0; j <= 8; j++)
{
if (status[j] != CLOSE[i].status[j])
{
H++;
}
}
if (H == 0)
{
return (-i) - 1;
}
H = 0;
}
return 0;
}
ENode *ENodeInit(int *status, int zero, int g, ENode *parent, int step)
{
int i;
Node = new ENode;
for (i = 0; i <= 8; i++)
{
Node->status[i] = status[i];
}
Node->Zero = zero;
Node->G = g;
Node->H = CountH(Node->status);
Node->F = Node->G + Node->H;
Node->Parent = parent;
Node->step = step;
return Node;
}
int *Left(int *s, int z)
{
int temp, i;
static int status[9];
for (i = 0; i <= 8; i++)
{
status[i] = s[i];
}
temp = status[z - 1];
status[z - 1] = 0;
status[z] = temp;
return status;
}
int *Right(int *s, int z)
{
int temp, i;
static int status[9];
for (i = 0; i <= 8; i++)
{
status[i] = s[i];
}
temp = status[z + 1];
status[z + 1] = 0;
status[z] = temp;
return status;
}
int *Up(int *s, int z)
{
int temp, i;
static int status[9];
for (i = 0; i <= 8; i++)
{
status[i] = s[i];
}
temp = status[z - 3];
status[z - 3] = 0;
status[z] = temp;
return status;
}
int *Down(int *s, int z)
{
int temp, i;
static int status[9];
for (i = 0; i <= 8; i++)
{
status[i] = s[i];
}
temp = status[z + 3];
status[z + 3] = 0;
status[z] = temp;
return status;
}
void ExistAndOperate(ENode *N)
{
int i;
int inList;
Node = new ENode;
Node = N;
if (Node->G == 1)
{
OPEN[open] = *Node;
open++;
return;
}
inList = Exist(Node);
if (inList == 0)
{
OPEN[open] = *Node;
open++;
}
else if (inList > 0)
{
if (OPEN[inList - 1].F > Node->F)
{
OPEN[inList - 1] = *Node;
}
}
else if (inList < 0)
{
inList = -inList;
if (CLOSE[inList - 1].F > Node->F)
{
OPEN[open] = *Node;
open++;
}
for (i = inList - 1; i <= close - 1; i++)
{
CLOSE[i] = CLOSE[i + 1];
}
close--;
}
}
ENode *Search()
{
int *status;
int i, j;
ENode *Temp;
while (1)
{
Temp = new ENode;
for (i = open - 1; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (OPEN[j].F > OPEN[j + 1].F)
{
*Temp = OPEN[j + 1];
OPEN[j + 1] = OPEN[j];
OPEN[j] = *Temp;
}
}
}
Node = new ENode;
*Node = OPEN[0];
if (!CountH(Node->status))
{
break;
}
Temp = Node;
CLOSE[close] = *Node;
close++;
for (i = 0; i <= open - 1; i++)
{
OPEN[i] = OPEN[i + 1];
}
open--;
if ((Temp->Zero) % 3 >= 1)
{
Node = new ENode;
status = Left(Temp->status, Temp->Zero);
Node = ENodeInit(status, Temp->Zero - 1, (Temp->G) + 1, Temp, 1);
ExistAndOperate(Node);
}
if ((Temp->Zero) % 3 <= 1)
{
Node = new ENode;
status = Right(Temp->status, Temp->Zero);
Node = ENodeInit(status, Temp->Zero + 1, (Temp->G) + 1, Temp, 2);
ExistAndOperate(Node);
}
if (Temp->Zero >= 3)
{
Node = new ENode;
status = Up(Temp->status, Temp->Zero);
Node = ENodeInit(status, Temp->Zero - 3, (Temp->G) + 1, Temp, 3);
ExistAndOperate(Node);
}
if (Temp->Zero <= 5)
{
Node = new ENode;
status = Down(Temp->status, Temp->Zero);
Node = ENodeInit(status, Temp->Zero + 3, (Temp->G) + 1, Temp, 4);
ExistAndOperate(Node);
}
if (open == 0)
return NULL;
}
return Node;
}
void ShowStep(ENode *Node)
{
int STEP[MAXSTEPSIZE];
int STATUS[MAXSTEPSIZE][9];
int step = 0;
int i, j;
int totalStep = Node->G;
while (Node)
{
STEP[step] = Node->step;
for (i = 0; i <= 8; i++)
{
STATUS[step][i] = Node->status[i];
}
step++;
Node = Node->Parent;
}
cout << "----------------------" << endl;
cout << totalStep << endl;
cout << "----------------------" << endl;
for (i = step - 1; i >= 0; i--)
{
if (STEP[i] == 1)
cout << "left";
else if (STEP[i] == 2)
cout << "right";
else if (STEP[i] == 3)
cout << "up";
else if (STEP[i] == 4)
cout << "down";
else if (STEP[i] == 0)
cout << "START";
cout << " ";
}
cout << endl << "----------------------" << endl;
for (i = step - 1; i >= 0; i--)
{
for (j = 0; j <= 8; j++)
{
cout << STATUS[i][j];
if (j == 2 || j == 5 || j == 8)
cout << endl;
else
cout << " ";
}
cout << "----------------------" << endl;
}
}
int main()
{
int fstatus[9];
int i;
ENode *FNode;
ENode *EndNode;
for (i = 0; i <= 8; i++)
{
cin >> fstatus[i];
}
for (i = 0; i <= 8; i++)
{
if (fstatus[i] == 0)
break;
}
FNode = ENodeInit(fstatus, i, 0, NULL, 0);
OPEN[open] = *FNode;
open++;
EndNode = Search();
if (!EndNode)
cout << "无解" << endl;
else
ShowStep(EndNode);
return 0;
}
***