Skip to content

Commit 05aeae6

Browse files
committed
fix possible hash bug in 8puzzle. explicitly set c++11. fix some warnings
1 parent 8a5fecd commit 05aeae6

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

cpp/8puzzle.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,7 @@ size_t PuzzleState::Hash()
345345
{
346346
stream << tiles[i];
347347
}
348-
size_t state_digits;
349-
stream >> state_digits;
350-
return hash<size_t>{}(state_digits);
348+
return std::hash<std::string>{}(stream.str());
351349
}
352350

353351
void PuzzleState::PrintNodeInfo()

cpp/findpath.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1010

11-
using namespace std;
12-
13-
#include "stlastar.h" // See header for copyright and usage information
14-
1511
#include <iostream>
1612
#include <stdio.h>
1713
#include <math.h>
1814

15+
using namespace std;
16+
17+
#include "stlastar.h" // See header for copyright and usage information
18+
1919
#define DEBUG_LISTS 0
2020
#define DEBUG_LIST_LENGTHS_ONLY 0
2121

cpp/makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
all : 8puzzle findpath minpathbucharest tests
22

3+
# Set the C++ compiler to g++
4+
CXX = g++
5+
6+
# Set some common C++ flags
7+
CXXFLAGS = -std=c++11 -Wall
8+
39
clean :
410
rm 8puzzle findpath minpathbucharest tests
511

612
minpathbucharest : min_path_to_Bucharest.cpp stlastar.h
7-
g++ -Wall min_path_to_Bucharest.cpp -o minpathbucharest
13+
$(CXX) $(CXXFLAGS) min_path_to_Bucharest.cpp -o minpathbucharest
814

915
8puzzle : 8puzzle.cpp stlastar.h
10-
g++ -Wall 8puzzle.cpp -o 8puzzle
16+
$(CXX) $(CXXFLAGS) -Wall 8puzzle.cpp -o 8puzzle
1117

1218
findpath : findpath.cpp stlastar.h
13-
g++ -Wall findpath.cpp -o findpath
19+
$(CXX) $(CXXFLAGS) findpath.cpp -o findpath
1420

1521
tests : tests.cpp 8puzzle findpath minpathbucharest
16-
g++ -Wall tests.cpp -o tests
22+
$(CXX) $(CXXFLAGS) tests.cpp -o tests
1723

1824
test: tests
1925
./tests

cpp/min_path_to_Bucharest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
// mention that the algorithm does some backtracking because the heuristic is not accurate (yet still admissable).
1919
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2020

21-
using namespace std;
22-
23-
#include "stlastar.h"
2421
#include <iostream>
2522
#include <string>
2623
#include <vector>
2724
#include <stdio.h>
2825

26+
using namespace std;
27+
28+
#include "stlastar.h"
29+
2930
#define DEBUG_LISTS 0
3031
#define DEBUG_LIST_LENGTHS_ONLY 0
3132

cpp/stlastar.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ template <class UserState> class AStarSearch
8181
Node *parent; // used during the search to record the parent of successor nodes
8282
Node *child; // used after the search for the application to view the search in reverse
8383

84-
float g; // cost of this node + it's predecessors
84+
float g; // cost of this node + its predecessors
8585
float h; // heuristic estimate of distance to goal
8686
float f; // sum of cumulative cost of predecessors and self and heuristic
8787

@@ -321,8 +321,9 @@ template <class UserState> class AStarSearch
321321
continue;
322322
}
323323
}
324+
typename unordered_set<Node*, NodeHash, NodeEqual>::iterator closedlist_result;
324325

325-
auto closedlist_result = m_ClosedList.find(*successor);
326+
closedlist_result = m_ClosedList.find(*successor);
326327

327328
if( closedlist_result != m_ClosedList.end() )
328329
{

0 commit comments

Comments
 (0)