#include<queue>
#include<iostream>
#include<stack>
using namespace std;
const int N=9;
struct Node {
int nine[N]; //九宫格状态
struct Node* parent; //父节点
int f; //估价值 int value;
int depth; //深度
friend bool operator < (Node A, Node B) //按照value值小的方案构造优先级队列
{
return A.f > B.f;
}
};
priority_queue<Node> openTable; //open表
queue<Node> closeTable; //close表
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) //产生新节点,加入OPEN表
{ //计算原状态下,0格所在的行列数,从而判断0格可以往哪个方向移动
int blank; //定义0所在位置的下标
for (blank = 0; blank < 9 && S.nine[blank] != 0; blank++);//找到空白格
int x = blank / 3, y = blank % 3; //获取空格所在行列编号
for (int d = 0; d < 4; d++) //找到S扩展的子节点,加入open表中
{
int newX = x, newY = y;//新0格坐标
Node tempNode;
//移动0格
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; //0格新的位置
if (newX >= 0 && newX < 3 && newY >= 0 && newY < 3) //如果移动合法
{
//交换新旧0白格的内容
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; // 把子节点都加入open表中
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()); //将open表中优先级最高的元素压入close表中
openTable.pop(); //剔除open表中优先级最高的元素
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;
}
2021-06-22 隐式图搜索
最新推荐文章于 2024-09-05 10:13:13 发布