From fc077eb86b4e5c1b69ed766d82a0b17edad5b67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar?= Date: Tue, 21 Feb 2023 14:09:08 +0100 Subject: [PATCH] verification des coups passes --- TP1/C-Cpp/Screens-1/scree-test.txt | 0 TP1/C-Cpp/Screens-1/screen-0.txt | 3 +- TP1/C-Cpp/includes/mysok.h | 2 +- TP1/C-Cpp/src/mysok.cpp | 45 ++++++++++++++++++++++++------ TP1/C-Cpp/src/r0.cpp | 4 ++- 5 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 TP1/C-Cpp/Screens-1/scree-test.txt diff --git a/TP1/C-Cpp/Screens-1/scree-test.txt b/TP1/C-Cpp/Screens-1/scree-test.txt new file mode 100644 index 0000000..e69de29 diff --git a/TP1/C-Cpp/Screens-1/screen-0.txt b/TP1/C-Cpp/Screens-1/screen-0.txt index ea941ec..db249cb 100644 --- a/TP1/C-Cpp/Screens-1/screen-0.txt +++ b/TP1/C-Cpp/Screens-1/screen-0.txt @@ -4,5 +4,4 @@ #$$$$$$# #......# # # -######## - +######## \ No newline at end of file diff --git a/TP1/C-Cpp/includes/mysok.h b/TP1/C-Cpp/includes/mysok.h index 34cff32..a0af94c 100644 --- a/TP1/C-Cpp/includes/mysok.h +++ b/TP1/C-Cpp/includes/mysok.h @@ -53,6 +53,6 @@ typedef struct { } Node; int bfs(Node &result); -int dfs(Node &result, int depth); +int dfs(Node &result, int depth, std::vector history); #endif diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index 62a58ba..4da83aa 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -99,6 +99,22 @@ void sok_board_t::load(char *_file) { fclose(fp); } +bool is_in_history(Node current, std::vector history) { + for (int i = 0; i < history.size(); ++i) { + Node noeud = history[i]; + + int c = 0; + for (int l = 0; l < NBL; ++l) { + for (int m = 0; m < NBC; ++m) { + if (current.state.board[l][m] != current.state.board[l][m]) c++; + } + } + + if (c == 0) return true; + } + return false; +} + int bfs(Node &result) { // Création de la queue de BFS std::queue q; @@ -219,7 +235,8 @@ int bfs(Node &result) { return -1; } -int dfs(Node &result, int depth) { +int dfs(Node &result, int depth, std::vector history) { + // Si on a atteint la profondeur maximale, on arrête la recherche if (depth == MAX_DEPTH) { /* std::cout << "max\n"; */ @@ -268,10 +285,15 @@ int dfs(Node &result, int depth) { next_dfs.path_len = result.path_len + 1; next_dfs.path = result.path + move_str[i] + " "; - int res = dfs(next_dfs, depth + 1); - if (res == 0) { - result = next_dfs; - return 0; + if (!is_in_history(next_dfs, history)) { + history.push_back(next_dfs); + int res = dfs(next_dfs, depth + 1, history); + if (res == 0) { + result = next_dfs; + return 0; + } + } else { + return -1; } } @@ -322,10 +344,15 @@ int dfs(Node &result, int depth) { next_dfs.path_len = result.path_len + 1; next_dfs.path = result.path + move_str[i] + " "; - int res = dfs(next_dfs, depth + 1); - if (res == 0) { - result = next_dfs; - return 0; + if (!is_in_history(next_dfs, history)) { + history.push_back(next_dfs); + int res = dfs(next_dfs, depth + 1, history); + if (res == 0) { + result = next_dfs; + return 0; + } + } else { + return -1; } } } diff --git a/TP1/C-Cpp/src/r0.cpp b/TP1/C-Cpp/src/r0.cpp index 65eaa35..8e6dbe5 100644 --- a/TP1/C-Cpp/src/r0.cpp +++ b/TP1/C-Cpp/src/r0.cpp @@ -23,7 +23,9 @@ int main(int _ac, char **_av) { std::cout << "Solution : " << result.path << "\n"; } */ - if (dfs(result, 0) == -1) { + std::vector history; + + if (dfs(result, 0, history) == -1) { std::cout << "Aucune solution trouvée\n"; } else { std::cout << "Coups : " << result.path_len << "\n";