我正在尝试编写一个广度优先的搜索程序来解决8-puzzle。当我运行以下代码时,我收到以下错误:
*`/home/a.out' ;: free()出错:无效指针:0x0000000001f81430 *
中止
我很确定问题在于使用 此 指针以及如何存储父节点。有帮助吗?
#include <iostream>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
class Node {
public:
vector<Node> children;
vector<int> puzzle;
vector<int> goal = {1, 2, 3, 4, 5, 6, 7, 8, 0};
Node *parent;
Node(vector<int> _puzzle, Node *_parent){ // constructor for node
puzzle=_puzzle;
parent=_parent;
}
void moveUp(){ //function to move up
int zPos = findZero();
vector<int> temp = puzzle;
if ( zPos != 0 || zPos != 1 || zPos != 2 )
std::swap(temp[zPos], temp[zPos-3]);
Node child = Node(temp, this);
children.push_back(child);
}
void moveDown(){ //function to move down
int zPos = findZero();
vector<int> temp = puzzle;
if ( zPos != 6 || zPos != 7 || zPos != 8 )
std::swap(temp[zPos], temp[zPos+3]);
Node child = Node(temp, this);
children.push_back(child);
}
void moveRight(){ //function to move right
int zPos = findZero();
vector<int> temp = puzzle;
if ( zPos != 2 || zPos != 5 || zPos != 8 )
std::swap(temp[zPos], temp[zPos+1]);
Node child = Node(temp, this);
children.push_back(child);
}
void moveLeft(){ //function to move left
int zPos = findZero();
vector<int> temp = puzzle;
if ( zPos != 0 || zPos != 3 || zPos != 6 )
std::swap(temp[zPos], temp[zPos-1]);
Node child = Node(temp, this);
children.push_back(child);
}
void printPuzzle() { //function to print the puzzle
int count = 0;
for (auto i: puzzle) {
if ( count % 3 == 0)
std::cout << std::endl;
std::cout << i << ' ';
count++;
}
}
int findZero(){ // function to find the location of zero
std::vector<int>::iterator it;
it = find (puzzle.begin(), puzzle.end(), 0);
auto z = std::distance(puzzle.begin(), it);
return (int)z;
}
bool isGoal(){ //function to check if goal state is reached
bool goalFound = false;
if(puzzle == goal)
goalFound = true;
return goalFound;
}
};
bool contains(std::queue<Node> q, Node n){ // checks repeated nodes
std::queue<Node> tempQ = q;
bool exist = false;
while (!tempQ.empty()){
if (tempQ.front().puzzle == n.puzzle)
exist = true;
tempQ.pop();
}
return exist;
}
int main()
{
std::vector<int> initial = {3, 5, 8, 1, 0, 4, 2, 7, 6};
Node init = Node(initial, NULL);
std::queue<Node> openList;
std::queue<Node> closedList;
openList.push(init);
bool goalFound = false;
while(!openList.empty() && !goalFound){
Node currentNode = openList.front();
closedList.push(currentNode);
openList.pop();
currentNode.moveUp();
currentNode.moveDown();
currentNode.moveRight();
currentNode.moveLeft();
for (auto i: currentNode.children){
Node currentChild = i;
if (currentChild.isGoal()){
std::cout << "Goal Found." << endl;
goalFound = true;
}
if (!contains(openList, currentChild) && !contains(closedList, currentChild))
openList.push(currentChild);
}
}
}
现在我只专注于找到目标。我还没有实现目标的路径并打印最终的解决方案。
答案 0 :(得分:0)
if (zPos != 0 || zPos != 1 || zPos != 2)
应该是
if (zPos != 0 && zPos != 1 && zPos != 2)
并且在其他行动中为其兄弟姐妹提供同样的功能。
TL; DR说明:
moveUp
中的
if (zPos != 0 || zPos != 1 || zPos != 2)
无法做你想做的事。将始终执行正文,因为zPos
不能同时为0,1和2。例如,如果zPos
为零,那么
if (0 != 0 || 0 != 1 || 0 != 2)
false || true
一个true
就足够了。
这意味着
std::swap(temp[0], temp[0-3]);
将交换0,-3并超出界限。当你在边界之外弄乱内存时会发生All sorts of weird,我想这次你要覆盖vector
存储缓冲区的部分簿记。一旦vector
被释放,kaBlamo!