From f6a8c542e8b2083df9a5815c1ba2c109cfebf05c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 1 Dec 2020 16:15:03 +0100 Subject: const & --- labb5/src/Boggle.cpp | 20 ++++++++++++-------- labb5/src/Boggle.h | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) (limited to 'labb5/src') 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 initial_points; found = find_single_word_helper(word, make_pair(x, y), string(1, board[y][x]), - set()); + 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 visited + const point& cur_point, + const string& cur_word, + set& 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 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 initial_points; find_all_words_helper(words, make_pair(x, y), string(1, board[y][x]), - set()); + initial_points); } } auto end = std::chrono::high_resolution_clock::now(); @@ -211,9 +214,9 @@ set Boggle::find_all_words() const { void Boggle::find_all_words_helper( set& words, - point cur_point, - string cur_word, - set visited + const point& cur_point, + const string& cur_word, + set& 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& words, point cur_point, string cur_word, set visited) const; - bool find_single_word_helper(const string& word, point cur_point, string cur_word, set visited) const; + void find_all_words_helper(set& words, const point& cur_point, const string& cur_word, set& visited) const; + bool find_single_word_helper(const string& word, const point& cur_point, const string& cur_word, set& visited) const; Lexicon dictionary; Grid board; -- cgit v1.2.1