-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
110 lines (82 loc) · 2.45 KB
/
Node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Created by Rosalie Razonable on 5/6/2021.
//
#include "Node.h"
// Helper Functions
void Node::printKthChild(Node *root, State &state1, unsigned int k) {
if(root == nullptr)
return;
if(root->state.isEqual(state1)) {
// Traverse children of root starting
// from left child
Node *n = root->child;
int i = 1;
while(n != nullptr && i < k) {
n = n->sibling;
i++;
}
if(n == nullptr)
cout << "Node doesn't exist\n";
else {
n->state.createBoard();
cout << endl;
}
return;
}
printKthChild(root->child, state1, k);
printKthChild(root->sibling, state1, k);
cout << "State does not exist" << endl;
}
unsigned int Node::calculateManhattanCost(State goalState) {
int manhattanCost = 0;
Board b;
vector<vector<int>> curStateCoord, goalStateCoord;
curStateCoord = Board::getBoardCoordinatesByValue(this->state);
goalStateCoord = Board::getBoardCoordinatesByValue(goalState);
unsigned int size = curStateCoord.size();
for(unsigned int i = 0; i < size; i++) {
manhattanCost += (abs(curStateCoord.at(i).at(0) - goalStateCoord.at(i).at(0)) +
abs(curStateCoord.at(i).at(1) - goalStateCoord.at(i).at(1)));
}
return manhattanCost;
}
/*unsigned int Node::calculateHammingCost(State goalState) {
unsigned int hamming = 0;
for(int i = 0; i < goalState.getState().size(); i++) {
if(goalState.getState().at(i) == 0)
continue;
else if(this->state.getState().at(i) != goalState.getState().at(i))
hamming++;
}
return hamming;
}*/
// Mutator Functions
void Node::setPathCostValue(State goalState) {
unsigned int manhattan, hamming;
manhattan = this->calculateManhattanCost(goalState);
/* hamming = this->calculateHammingCost(goalState);
this->pathCost = manhattan + hamming + this->level;*/
this->pathCost = manhattan + this->level;
}
void Node::setSibling(Node *sib) {
this->sibling = sib;
}
void Node::setChild(Node *c) {
this->child = c;
}
void Node::setLevel(unsigned int lvl) {
this->level = lvl;
}
// Accessor Functions
unsigned int Node::getLevel() const {
return this->level;
}
unsigned int Node::getPathCostValue() const {
return this->pathCost;
}
Node* Node::getChild() const {
return this->child;
}
Node* Node::getSibling() const {
return this->sibling;
}