File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77// //////////////////////////////////////////////////////////////////////////////////////////////////////////////
88
99#include < iostream>
10+ #include < sstream>
1011#include < stdio.h>
1112#include < assert.h>
1213#include < new>
@@ -115,6 +116,7 @@ class PuzzleState
115116 bool GetSuccessors ( AStarSearch<PuzzleState> *astarsearch, PuzzleState *parent_node );
116117 float GetCost ( PuzzleState &successor );
117118 bool IsSameState ( PuzzleState &rhs );
119+ size_t Hash ();
118120
119121 void PrintNodeInfo ();
120122
@@ -335,6 +337,19 @@ bool PuzzleState::IsSameState( PuzzleState &rhs )
335337
336338}
337339
340+ // The 9 tiles positions can be encoded as digits
341+ size_t PuzzleState::Hash ()
342+ {
343+ stringstream stream;
344+ for ( size_t i = 0 ; i < (BOARD_HEIGHT * BOARD_WIDTH ); i++ )
345+ {
346+ stream << tiles[i];
347+ }
348+ size_t state_digits;
349+ stream >> state_digits;
350+ return hash<size_t >{}(state_digits);
351+ }
352+
338353void PuzzleState::PrintNodeInfo ()
339354{
340355 char str[100 ];
Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ class MapSearchNode
8787 bool GetSuccessors ( AStarSearch<MapSearchNode> *astarsearch, MapSearchNode *parent_node );
8888 float GetCost ( MapSearchNode &successor );
8989 bool IsSameState ( MapSearchNode &rhs );
90+ size_t Hash ();
9091
9192 void PrintNodeInfo ();
9293
@@ -109,6 +110,13 @@ bool MapSearchNode::IsSameState( MapSearchNode &rhs )
109110
110111}
111112
113+ size_t MapSearchNode::Hash ()
114+ {
115+ size_t h1 = hash<float >{}(x);
116+ size_t h2 = hash<float >{}(y);
117+ return h1 ^ (h2 << 1 );
118+ }
119+
112120void MapSearchNode::PrintNodeInfo ()
113121{
114122 char str[100 ];
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ class PathSearchNode
4949 bool GetSuccessors ( AStarSearch<PathSearchNode> *astarsearch, PathSearchNode *parent_node );
5050 float GetCost ( PathSearchNode &successor );
5151 bool IsSameState ( PathSearchNode &rhs );
52+ size_t Hash ();
5253
5354 void PrintNodeInfo ();
5455};
@@ -60,6 +61,11 @@ bool PathSearchNode::IsSameState( PathSearchNode &rhs )
6061 return (false );
6162}
6263
64+ size_t PathSearchNode::Hash ()
65+ {
66+ return hash<int >{}(city);
67+ }
68+
6369// Euclidean distance between "this" node city and Bucharest
6470// Note: Numbers are taken from the book
6571float PathSearchNode::GoalDistanceEstimate ( PathSearchNode &nodeGoal )
Original file line number Diff line number Diff line change @@ -73,6 +73,7 @@ class MapSearchNode
7373 bool GetSuccessors ( AStarSearch<MapSearchNode> *astarsearch, MapSearchNode *parent_node );
7474 float GetCost ( MapSearchNode &successor );
7575 bool IsSameState ( MapSearchNode &rhs );
76+ size_t Hash ();
7677
7778 void PrintNodeInfo ();
7879
@@ -190,6 +191,13 @@ float MapSearchNode::GetCost( MapSearchNode &successor )
190191
191192}
192193
194+ size_t MapSearchNode::Hash ()
195+ {
196+ size_t h1 = hash<float >{}(x);
197+ size_t h2 = hash<float >{}(y);
198+ return h1 ^ (h2 << 1 );
199+ }
200+
193201int main (int argc, char *argv[]) {
194202
195203 AStarSearch<MapSearchNode> astarsearch;
You can’t perform that action at this time.
0 commit comments