C++ 双链表父指针更改
实现一个贪婪的解决方案来解决8-puzzle问题 h:C++ 双链表父指针更改,c++,pointers,greedy,sliding-tile-puzzle,C++,Pointers,Greedy,Sliding Tile Puzzle,实现一个贪婪的解决方案来解决8-puzzle问题 h: class Greedy { const string Goal = "12345678_"; State current; string startState; int nodesCreated, nodesExpanded; priority_queue<State> greedyQueue; set<string> visited; stack<st
class Greedy {
const string Goal = "12345678_";
State current;
string startState;
int nodesCreated, nodesExpanded;
priority_queue<State> greedyQueue;
set<string> visited;
stack<string> solutionStack;
public:
Greedy(const string start);
~Greedy();
void doGreedy();
};
类贪婪{
常量字符串目标=“12345678”;
状态电流;
字符串起始状态;
int nodesCreated,nodesExpanded;
优先级队列;
集参观;
堆栈解决方案;
公众:
贪婪(常量字符串开始);
~贪婪();
void doGreedy();
};
Greedy.cpp:
tuple<int, int> getTarget(int n);
Greedy::Greedy(const string start) : startState(start), nodesCreated(0), nodesExpanded(0) {}
Greedy::~Greedy() {}
void Greedy::doGreedy() {
greedyQueue.emplace(startState, "");
while (!greedyQueue.empty()) {
current = greedyQueue.top();
greedyQueue.pop();
if (visited.find(current.stateString) == visited.end()) {
visited.insert(current.stateString);
if (current.stateString == Goal) { //end has been reached, calculate path, print out stats, and end.
cout << "Solution Found!" << endl;
//solutionStack.push(current.moveFromParent);
State* tempParent = current.parent;
while ( solutionStack.size() < 20 && tempParent != NULL) {
solutionStack.push(tempParent->moveFromParent);
tempParent = tempParent->parent;
}
break;
}
vector<State> childrenFound = current.expandNode();
for (int i = 0; i < childrenFound.size(); ++i) { // for each child found, add it to the priority queue, set its parent, and set it as a child of parent
State temp = childrenFound[i];
if (visited.find(temp.stateString) == visited.end()) { // We haven't been here before, put it in the queue
greedyQueue.push(temp);
}
}
}
}
cout << "Last 20 moves:" << endl;
while (!solutionStack.empty()) {
cout << solutionStack.top() << endl;
solutionStack.pop();
}
}
元组getTarget(int n);
贪婪::贪婪(const string start):startState(start)、nodesCreated(0)、nodesExpanded(0){}
贪婪::~Greedy(){}
void Greedy::doGreedy(){
greedyQueue.emplace(startState,“”);
而(!greedyQueue.empty()){
current=greedyQueue.top();
greedyQueue.pop();
if(visted.find(current.stateString)=visted.end()){
insert(current.stateString);
如果已达到(current.stateString==Goal){//end,则计算路径,打印统计数据并结束。
不能做父母;
}
打破
}
vector childrenFound=current.expandNode();
对于(int i=0;i cout问题在于Greedy::doGreedy
和您对当前的使用
分配current=greedyQueue.top()代码>创建队列中顶部对象的副本。稍后,当您调用向量childrenFound=current.expandNode()时
,所有返回的状态都有父指针,它们引用当前的。在下一次循环迭代中,再次将该赋值设置为current
,更改所有返回状态指向的父项
你的代码不容易修复。您需要重新考虑如何存储状态
对象,以便父对象保持不变且不被修改。通常,这类操作是通过堆栈或列表完成的,将每个节点添加到末尾,然后将它们弹出,向上移动到父节点。您能将代码简化吗?这本书太长了,读起来似乎大部分都与你的问题无关……嗯,你是对的,问题与“当前”有关。那么,被创建的子对象指向的是“当前”,而不是它们创建时所持有的任何“当前”?你说的对吗?如果我把“current”设为State*,那会不会消除对“current”中当前内容的依赖?我明白了!我将“State current”更改为“stack visitedStates”,然后每当我想获取当前正在处理的状态时,只要说出visitedStates.top()就可以了。谢谢你的帮助!
class State {
public:
string moveFromParent;
State* parent;
string stateString;
int distance;
State();
State(const string str, State * _parent, string _moveFromParent);
State (const string str, string _moveFromParent);
State(const string str, int dist, State * _parent, string _moveFromParent);
~State();
bool operator<(const State & state) const;
bool operator==(const State & state) const;
int findBlank();
vector<State> expandNode();
};
int manhattan(const string str);
tuple<int, int> getTarget(int n);
State::State() {}
State::State(const string str, State * _parent, string _moveFromParent) : stateString(str), moveFromParent(_moveFromParent) {
parent = _parent;
}
State::State(const string str, string _moveFromParent) : stateString(str), moveFromParent(_moveFromParent) {
parent = NULL;
distance = manhattan(stateString);
}
State::State(const string str, int dist, State* _parent, string _moveFromParent) : stateString(str), distance(dist), moveFromParent(_moveFromParent) {
parent = _parent;
distance = manhattan(stateString);
}
State::~State() {}
bool State::operator<(const State & state) const {
return distance > state.distance;
}
bool State::operator==(const State & state) const {
return ((stateString == state.stateString));
}
int State::findBlank() {
for (int i = 0; i < stateString.length(); ++i) {
if (stateString[i] == '_') {
return i;
}
}
}
vector<State> State::expandNode() {
vector<State> returnStates;
int blank = findBlank();
if (blank % 3 > 0) { // can move left
string newState = stateString;
newState[blank] = newState[blank - 1];
newState[blank - 1] = '_';
int heuristic = manhattan(newState);
State * childsParent = this;
string move = "left";
State temp = State(newState, heuristic, childsParent, move);
returnStates.push_back(temp);
}
if (blank % 3 < 2) { //can move right
string newState = stateString;
newState[blank] = newState[blank + 1];
newState[blank + 1] = '_';
int heuristic = manhattan(newState);
State * childsParent = this;
string move = "right";
State temp = State(newState, heuristic, childsParent, move);
returnStates.push_back(temp);
}
if (blank / 3 > 0) { //can move up
string newState = stateString;
newState[blank] = newState[blank - 3];
newState[blank - 3] = '_';
int heuristic = manhattan(newState);
State * childsParent = this;
string move = "up";
State temp = State(newState, heuristic, childsParent, move);
returnStates.push_back(temp);
}
if (blank / 3 < 2) { // can move down
string newState = stateString;
newState[blank] = newState[blank + 3];
newState[blank + 3] = '_';
int heuristic = manhattan(newState);
State * childsParent = this;
string move = "down";
State temp = State(newState, heuristic, childsParent, move);
returnStates.push_back(temp);
}
return returnStates;
}
int manhattan(const string str) {
int distance = 0;
for (int i = 0, length = str.length(); i != length; ++i) {
tuple<int, int> target;
if (str[i] == '_') {
target = { 2, 2 };
}
else {
int temp = str[i] - '0';
target = getTarget(temp);
}
tuple<int, int> current = getTarget(i + 1);
int localSum = abs(get<0>(current) - get<0>(target)) + abs(get<1>(current) - get<1>(target));
distance += localSum;
}
return distance;
}
tuple<int, int> getTarget(int n) {
return { (n - 1) / 3, (n - 1) % 3 };
}