#include<queue>
#include<iostream>
#include<stack>
using namespace std;
const int N=9;
struct Node {
int nine[N];
struct Node* parent;
int f;
int depth;
friend bool operator < (Node A, Node B)
{
return A.f > B.f;
}
};
priority_queue<Node> openTable;
queue<Node> closeTable;
stack<Node> Path;
int count1 = 0, count2 = 0;
int read(Node& S, Node& G) {
S.parent = NULL; S.depth = 0; S.f = 0;
G.parent = NULL; G.depth = 0; G.f = 0;
cout << "请输入九宫格的初始状态:(空格置0)\n";
for (int i = 0; i < N; i++)
cin >> S.nine[i];
cout << "请输入九宫格的目标状态:(空格置0)\n";
for (int i = 0; i < N; i++)
{
cin >> G.nine[i];
}
for (int i = 0; i <= N - 2; i++)
for (int j = i + 1; j < N; j++)
if (S.nine[i] > S.nine[j] && S.nine[i] * S.nine[j] != 0)
count1++;
for (int i = 0; i <= N - 2; i++)
for (int j = i + 1; j < N; j++)
if (G.nine[i] > G.nine[j] && G.nine[i] * G.nine[j] != 0)
count2++;
if (count1 % 2 != count2 % 2)
{
return 0;
}
return 1;
}
int value(Node A, Node G) {
int count = 0, begin[3][3], end[3][3];
for (int i = 0; i < N; i++) {
begin[i / 3][i % 3] = A.nine[i];
end[i / 3][i % 3] = G.nine[i];
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
if (begin[i][j] == 0)
continue;
else if (begin[i][j] != end[i][j])
{
for (int k = 0; k < 3; k++)
for (int w = 0; w < 3; w++)
if (begin[i][j] == end[k][w])
count = count + fabs(i - k * 1.0) + fabs(j - w * 1.0);
}
}
return count + A.depth;
}
bool judge(Node S, Node G)
{
for (int i = 0; i <= 8; i++)
{
if (S.nine[i] != G.nine[i])
{
return false;
}
}
return true;
}
void creatNode(Node& S, Node G)
{
int blank;
for (blank = 0; blank < 9 && S.nine[blank] != 0; blank++);
int x = blank / 3, y = blank % 3;
for (int d = 0; d < 4; d++)
{
int newX = x, newY = y;
Node tempNode;
if (d == 0) newX = x - 1;
if (d == 1) newY = y - 1;
if (d == 2) newX = x + 1;
if (d == 3) newY = y + 1;
int newBlank = newX * 3 + newY;
if (newX >= 0 && newX < 3 && newY >= 0 && newY < 3)
{
tempNode = S;
tempNode.nine[blank] = S.nine[newBlank];
tempNode.nine[newBlank] = 0;
if (S.parent != NULL && (*S.parent).nine[newBlank] == 0)
{
continue;
}
tempNode.parent = &S;
tempNode.f = value(tempNode, G);
tempNode.depth = S.depth + 1;
openTable.push(tempNode);
}
}
}
int main()
{
Node S0, Sg;
if (!read(S0, Sg))
{
cout << "无解!";
system("pause");
return 0;
}
openTable.push(S0);
while (true)
{
closeTable.push(openTable.top());
openTable.pop();
if (!judge(closeTable.back(), Sg))
{
creatNode(closeTable.back(), Sg);
}
else
{
break;
}
}
Node tempNode;
tempNode = closeTable.back();
while (tempNode.parent != NULL)
{
Path.push(tempNode);
tempNode = *(tempNode.parent);
}
Path.push(tempNode);
cout << "根据A*算法需要移动" << Path.size() - 1 << "步" << endl;
int j = 0;
while (Path.size() != 0)
{
cout << "第" << j << "步:" << endl;
j++;
for (int i = 0; i <= 8; i++)
{
if (Path.top().nine[i] == 0)
cout << " ";
else cout << Path.top().nine[i] << " ";
if ((i + 1) % 3 == 0)
cout << endl;
}
Path.pop();
cout << "\n";
}
system("pause");
return 0;
}