verification des coups passes

This commit is contained in:
César 2023-02-21 14:09:08 +01:00
parent d6d2c5def5
commit fc077eb86b
5 changed files with 41 additions and 13 deletions

View file

View file

@ -4,5 +4,4 @@
#$$$$$$#
#......#
# #
########
########

View file

@ -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<Node> history);
#endif

View file

@ -99,6 +99,22 @@ void sok_board_t::load(char *_file) {
fclose(fp);
}
bool is_in_history(Node current, std::vector<Node> 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<Node> 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<Node> 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;
}
}
}

View file

@ -23,7 +23,9 @@ int main(int _ac, char **_av) {
std::cout << "Solution : " << result.path << "\n";
} */
if (dfs(result, 0) == -1) {
std::vector<Node> history;
if (dfs(result, 0, history) == -1) {
std::cout << "Aucune solution trouvée\n";
} else {
std::cout << "Coups : " << result.path_len << "\n";