int matrix[N+1][N+1];
/* This matrix is the current status. */
int x,y;
/* These dataes will be the position of the ZERO. */
int value;
/* This proterties will be the actual value of this node. */
struct node *parent,*next;
/* The data point its father node. By it, the program
can output all the required nodes when find a desition node. */
} NODE; /* Define the struct NODE */
int action[5][2] = {{0,0},{-1,0},{0,1},{1,0},{0,-1}};
/* Define the action type */
int source[N+1][N+1] = {
{0, 0, 0, 0, 0},
{0, 1, 2, 3, 4},
{0, 5, 6, 0, 8},
{0, 9,10, 7,11},
{0,13,14,15,12}};
/* This is the initilize status */
int desition[N+1][N+1] = {
{0, 0, 0, 0, 0},
{0, 1, 2, 3, 4},
{0, 5, 6, 8, 0},
{0, 9,10, 7,11},
{0,13,14,15,12}};
/* This is the desition status. */
NODE *head,*tail;
/* This list will store all nodes which is moved out
of the priority queue. */
NODE *head1;
/* This point willl point to the priority queue. */
int steps = 0;
void main(void)
{
/* The MAIN function include the control process.
The control process is as follow:
First,the fuction will create a priority queue. All the child nodes of the
current node will be put into the queue. The process will choose the best
node (another function CalVal() will calculate every child's value) to be
the next current node. Continue by this way till find the desition node. */
int CalVal (NODE *);
void PRINT (NODE *);
void INSERT (NODE *);
int EQUAL (NODE *);
int Exist (NODE *);
void DELETEMIN(void);
NODE *p,*current;
/* the variable will point to the priority queue. */
int i,j,k;
system("cls");
printf("The Program Is Searching...");
printf("\n\t\t\t<<<<WAITTING>>>>\n");
head = (NODE *)malloc(sizeof(NODE));
head->next = NULL;
head1 = (NODE *)malloc(sizeof(NODE));
for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++) head1->matrix[i][j] = source[i][j];
/* Copy the source matrix into the head node. */
p = (NODE *)malloc(sizeof(NODE));
/* allocte a temp memory. */
current = head1;
while(EQUAL(current) != 1)
/* If the current node is not an answer node. */
{
DELETEMIN();
/* the current value is the best child node in the priority queue. */
for(i = 1;i <= 4;i++)
{
if((current->x+action[i][0] <= N&¤t->x+action[i][0] >= 1)
&&(current->y+action[i][1] <= N&¤t->x+action[i][1] >= 1))
/* If the node is fit */
{
for(j = 1;j <= N;j++)
for(k = 1;k <= N;k++)
p->matrix[j][k] = current->matrix[j][k];
p->x = current->x+action[i][0];
p->y = current->y+action[i][1];
j = p->matrix[p->x][p->y];
p->matrix[p->x][p->y] = 0;
p->matrix[current->x][current->y] = j;
p->value = CalVal(p);
/* Copy the current node into a tempelete node, and make changes.
p->parent will be changed by the fuction DELETEMIN(NODE *). */
if(!Exist(p))
INSERT(p);
/* Insert the tempelete node into the priority queue. */
}
}
/* Extract all child nodes of the current node. */
current = head1;
}
if(EQUAL(current) == 1)
{
printf("\nFIND SUCCESS!\n");
current->parent = tail;
PRINT(current);
printf("\nTHE STEPS NEEDED IS :%d ",steps);
}
else
printf("There is no way!");
getch();
}
int CalVal(NODE *p)
{
int i,j,count = 0;
/* The result is the number of the numbers which is
not equal to the desition. */
for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++)
if(p->matrix[i][j] != desition[i][j]) count++;
/*
while(p->parent != NULL)
{
count++;
p = p->parent;
}
*/
return count;
}