diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-12-01 16:15:03 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-12-01 16:15:03 +0100 |
| commit | f6a8c542e8b2083df9a5815c1ba2c109cfebf05c (patch) | |
| tree | e914fde6f410043f53b151152f5a130cc68209eb /labb5/src | |
| parent | 93c6b29368d1e0487937b433bc6e678da0058055 (diff) | |
| download | tddd86-f6a8c542e8b2083df9a5815c1ba2c109cfebf05c.tar.gz | |
const &
Diffstat (limited to 'labb5/src')
| -rwxr-xr-x | labb5/src/Boggle.cpp | 20 | ||||
| -rwxr-xr-x | labb5/src/Boggle.h | 4 |
2 files changed, 14 insertions, 10 deletions
diff --git a/labb5/src/Boggle.cpp b/labb5/src/Boggle.cpp index 9ae2d94..829be35 100755 --- a/labb5/src/Boggle.cpp +++ b/labb5/src/Boggle.cpp @@ -137,10 +137,11 @@ bool Boggle::find_single_word(const string& word) const { bool found = false; for (int y = 0; y < BOARD_SIZE; y++) { for (int x = 0; x < BOARD_SIZE; x++) { + set<point> initial_points; found = find_single_word_helper(word, make_pair(x, y), string(1, board[y][x]), - set<point>()); + initial_points); if (found) break; } if (found) break; @@ -163,9 +164,9 @@ bool Boggle::find_single_word(const string& word) const { bool Boggle::find_single_word_helper( const string& word, - point cur_point, - string cur_word, - set<point> visited + const point& cur_point, + const string& cur_word, + set<point>& visited ) const { if (cur_word == word) { return true; @@ -184,6 +185,7 @@ bool Boggle::find_single_word_helper( } } } + visited.erase(cur_point); return false; } @@ -192,10 +194,11 @@ set<string> Boggle::find_all_words() const { auto start = std::chrono::high_resolution_clock::now(); for (int y = 0; y < BOARD_SIZE; y++) { for (int x = 0; x < BOARD_SIZE; x++) { + set<point> initial_points; find_all_words_helper(words, make_pair(x, y), string(1, board[y][x]), - set<point>()); + initial_points); } } auto end = std::chrono::high_resolution_clock::now(); @@ -211,9 +214,9 @@ set<string> Boggle::find_all_words() const { void Boggle::find_all_words_helper( set<string>& words, - point cur_point, - string cur_word, - set<point> visited + const point& cur_point, + const string& cur_word, + set<point>& visited ) const { if (cur_word.length() >= 4 && words.count(cur_word) == 0 @@ -233,6 +236,7 @@ void Boggle::find_all_words_helper( find_all_words_helper(words, neighbour, new_word, visited); } } + visited.erase(cur_point); } string Boggle::board_to_string() const { diff --git a/labb5/src/Boggle.h b/labb5/src/Boggle.h index 8f68e92..95dd0b7 100755 --- a/labb5/src/Boggle.h +++ b/labb5/src/Boggle.h @@ -106,8 +106,8 @@ private: const int MIN_WORD_LENGTH = 4; static const int BOARD_SIZE = 4; - void find_all_words_helper(set<string>& words, point cur_point, string cur_word, set<point> visited) const; - bool find_single_word_helper(const string& word, point cur_point, string cur_word, set<point> visited) const; + void find_all_words_helper(set<string>& words, const point& cur_point, const string& cur_word, set<point>& visited) const; + bool find_single_word_helper(const string& word, const point& cur_point, const string& cur_word, set<point>& visited) const; Lexicon dictionary; Grid<char> board; |
