#ifndef _A_STAR_
#define _A_STAR_
#include <iostream>
#include <vector>
#include <queue>
#include <assert.h>
using namespace std;
void TestAStar();
/*
八数码:
硬编码,没考虑扩展
*/
/*
1.找到从source到dest的过程
*/
// 节点状态结构
struct NodeState
{
int state[9];
NodeState *parent;
double f;
double g;
NodeState()
{
for (int i = 0; i < 9; ++i)
{
state[i] = 0;
}
parent = 0;
f = 0.0;
g = 0.0;
}
NodeState(const NodeState& node)
{
assert(this != &node);
for (int i = 0; i < 9; ++i)
{
this->state[i] = node.state[i];
}
this->f = node.f;
this->g = node.g;
this->parent = node.parent;
}
// 打印State状态
void PrintState()
{
cout << "------------------" << endl;
for (int i = 0; i < 9; ++i)
{
cout << i << " ";
if (i % 3 == 2)
{
cout << endl;
}
}
cout << "------------------" << endl;
}
};
// 优先级队列的比较函数
class NodeStateComp
{
public:
NodeStateComp(const bool& param = false)
: reverse(param)
{}
// 默认是按照从大到小排序
bool operator () (const NodeState *lhs, const NodeState *rhs)
{
if (reverse)
{
return (lhs->f > rhs->f);
}
return (lhs->f < rhs->f);
}
private:
bool reverse;
};
NodeState* EightNumber(NodeState *source, NodeState *dest, vector<NodeState *> stateVec);
void TestEightNumber();
// 比较两个状态节点是否相等
bool IsTwoNodeStateEqual(NodeState *lhs, NodeState *rhs);
// 扩展子节点
/*
source: 待扩展的节点
stateVec: 已扩展状态集合
open: open表
*/
void ExpandNode(NodeState *source, NodeState *dest, vector<NodeState *> &stateVec,
priority_queue<NodeState *, vector<NodeState *> ,NodeStateComp> &open);
// 找到空格位置
int FindNullPosition(NodeState *source);
// 判断当前空格是否能向某一方向扩展
bool CanExpandDirection(int position, int direction, int tmp);
// 判断在状态集合中是否已经存在了某一状态
bool IsExistStateInStateVec(NodeState *source, vector<NodeState *> &stateVec);
// 计算f、g
void CalculateFG(NodeState *source, NodeState *dest);
// 计算h值:当前状态和目标状态有几个位置是不同的
double CalculateF(NodeState *source, NodeState *dest);
template<typename T>
void MySwap3(T &left, T &right)
{
T tmp = left;
left = right;
right = tmp;
}
// 打印状态集合
// 由于所有的NodeState目前都在内存中,因此没必要传入vector<NodeState *> &stateVec
void PrintStateResult(NodeState *source, NodeState *dest);
// 清空stateVec
void FreeStateVec(vector<NodeState *> &stateVec);
/*
它完全按照open、close的定义
open:待扩展的节点集合
close:已扩展的节点
*/
#endif
#include <algorithm>
#include "1_AStar.h"
void TestAStar()
{
//cout << "AStar" << endl;
TestEightNumber();
}
/*
如何扩展子节点:
1.找到空格位置
2.将空格(0)位置移动到上下左右,同时将相应的数字移动到原来空格的位置
3.如果原来已经有相同的状态,则不能扩展
*/
NodeState* EightNumber(NodeState *source, NodeState *dest, vector<NodeState *> &stateVec)
{
// 构造一个优先级队列,最大堆
priority_queue<NodeState *, vector<NodeState *> ,NodeStateComp> open;
// 将source入队
open.push(source);
stateVec.push_back(source);
while (!open.empty())
{
NodeState *cur = open.top();
open.pop();
// 1.如果找到答案
if (IsTwoNodeStateEqual(cur, dest))
{
return cur;
}
// 2.否则扩展子节点,并把符合要求的子节点放入open表中
ExpandNode(source, dest, stateVec, open);
}
// 如果没有答案,返回NULL
return NULL;
}
bool IsTwoNodeStateEqual(NodeState *lhs, NodeState *rhs)
{
// 比较两个状态是否相同,只比较state数组是否相同即可
for (int i = 0; i < 9; ++i)
{
if (lhs->state[i] != rhs->state[i])
{
return false;
}
}
return true;
}
/*
如何扩展子节点:
1.找到空格位置
2.将空格(0)位置移动到上下左右,同时将相应的数字移动到原来空格的位置
3.如果原来已经有相同的状态,则不能扩展
*/
void ExpandNode(NodeState *source, NodeState *dest, vector<NodeState *> &stateVec,
priority_queue<NodeState *, vector<NodeState *> ,NodeStateComp> &open)
{
// 0->8 找到空格位置
int nullPosition = FindNullPosition(source);
// 定义一个Map, 上、右、下、左。表示与当前位置的距离
int map[4] = {-3, 1, 3, -1};
// 开始用空格位置向4个方向扩展
for (int i = 0; i < 4; ++i)
{
int tmp = nullPosition + map[i];
// 如果能扩展
if (CanExpandDirection(nullPosition, i, tmp))
{
// 构造一个新节点
NodeState *nextNode = new NodeState(*source);
// 将空格位置与tmp的位置交换,产生新的状态
// swap<int>(nextNode->state[nullPosition], nextNode->state[tmp]);
/*int intTmp = nextNode->state[nullPosition];
nextNode->state[nullPosition] = nextNode->state[tmp];
nextNode->state[tmp] = intTmp;*/
MySwap3<int>(nextNode->state[nullPosition], nextNode->state[tmp]);
// 判断在状态集合中是否已经存在了此状态
if (IsExistStateInStateVec(nextNode, stateVec))
{
// 直接删除该状态,并continue
delete nextNode;
continue;
}
nextNode->parent = source;
// 计算f、g
CalculateFG(nextNode, dest);
// 将新节点添加到状态集合和open表中
open.push(nextNode);
stateVec.push_back(nextNode);
}
}
}
// 左边界不能向左,右边界不能向右
// (tmp >= 0 && tmp <= 8)
bool CanExpandDirection(int position, int direction, int tmp)
{
// 左边界且是向左、右边界且向右、超过范围
if (((position % 3 == 0) && (direction == 3)) ||
((position % 3 == 2) && (direction == 1)) ||
(tmp < 0 || tmp > 8))
{
return false;
}
return true;
}
// 计算f、g
void CalculateFG(NodeState *source, NodeState *dest)
{
// g是当前已走过的路径数
assert(source->parent);
double g = source->parent->g + 1;
// 计算h值:表示当前状态和dest有几个不同的!
double h = CalculateF(source, dest);;
source->g = g;
source->f = g + h;
}
double CalculateF(NodeState *source, NodeState *dest)
{
double res = 0;
for (int i = 0; i < 9; ++i)
{
if (source->state[i] == 0)
{
continue;
}
if (source->state[i] != dest->state[i])
{
res++;
}
}
return res;
}
bool IsExistStateInStateVec(NodeState *source, vector<NodeState *> &stateVec)
{
size_t size = stateVec.size();
for (size_t i = 0; i < size; ++i)
{
if (IsTwoNodeStateEqual(source, stateVec[i]))
{
return true;
}
}
return false;
}
int FindNullPosition(NodeState *source)
{
for (int i = 0; i < 9; ++i)
{
if (source->state[i] == 0)
{
return i;
}
}
return -1;
}
void FreeStateVec(vector<NodeState *> &stateVec)
{
size_t size = stateVec.size();
for (int i = 0; i < size; ++i)
{
delete (stateVec[i]);
}
}
void PrintStateResult(NodeState *source, NodeState *dest)
{
// 递归打印
if (IsTwoNodeStateEqual(source, dest))
{
return;
}
PrintStateResult(source, dest->parent);
dest->PrintState();
}
void TestEightNumber()
{
/*priority_queue<double> open;
open.push(3.0);
open.push(2.4);
open.push(5.5);
cout << open.top() << endl;
open.pop();
cout << open.top() << endl;
open.pop();
cout << open.top() << endl;
open.pop();*/
/*priority_queue<NodeState *, vector<NodeState *> ,NodeStateComp> open;
NodeState node1;
node1.f = 3.0;
open.push(&node1);
NodeState node2;
node2.f = 2.0;
open.push(&node2);
NodeState node3;
node3.f = 4.0;
open.push(&node3);
cout << open.top()->f << endl;
open.pop();
cout << open.top()->f << endl;
open.pop();
cout << open.top()->f << endl;
open.pop();*/
// 需要测试
}