-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
94 lines (83 loc) · 3.22 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class Solution
{
public:
// Définition d'un caractère pour marquer les cases visitées
const char MARKED_CHAR = '.';
// Vecteur d'indices pour déplacement dans les 4 directions
vector<pair<int, int>> indexHelper = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
// Fonction de backtracking pour rechercher le mot dans la grille
bool backtracking(vector<vector<char>> &board, string &word, int indexW, int y, int x)
{
// Si on a parcouru tout le mot, c'est une solution
if (indexW == word.size())
{
return true;
}
// Parcours des déplacements possibles à partir de la position actuelle
for (const auto &iH : indexHelper)
{
int _y = y + iH.first;
int _x = x + iH.second;
// Vérification des limites de la grille et correspondance avec le prochain caractère du mot
if (_y >= 0 && _y < board.size() && _x >= 0 && _x < board[0].size() && board[_y][_x] == word[indexW])
{
// Marquage de la case visitée
board[_y][_x] = MARKED_CHAR;
// Appel récursif pour explorer le prochain caractère
if (backtracking(board, word, indexW + 1, _y, _x))
{
return true;
}
// Restauration du caractère original après exploration
board[_y][_x] = word[indexW];
}
}
return false;
}
bool exist(vector<vector<char>> &board, string word)
{
int countFirstChar = 0; // Compteur pour le premier caractère du mot
int countLastChar = 0; // Compteur pour le dernier caractère du mot
// Parcours de la grille pour compter les occurrences du premier et dernier caractère du mot
for (int y = 0; y < board.size(); y++)
{
for (int x = 0; x < board[0].size(); x++)
{
if (board[y][x] == word[0])
{
countFirstChar++;
}
else if (board[y][x] == word.back())
{
countLastChar++;
}
}
}
// Si le premier caractère apparaît plus souvent que le dernier, on inverse le mot pour optimiser la recherche
if (countFirstChar > countLastChar)
{
reverse(word.begin(), word.end());
}
// Parcours de la grille pour rechercher le premier caractère du mot
for (int y = 0; y < board.size(); y++)
{
for (int x = 0; x < board[0].size(); x++)
{
if (board[y][x] == word[0])
{
// Marquage de la case visitée
board[y][x] = MARKED_CHAR;
// Appel à la fonction de backtracking pour rechercher le reste du mot
if (backtracking(board, word, 1, y, x))
{
return true;
}
// Restauration du caractère original après exploration
board[y][x] = word[0];
}
}
}
// Le mot n'a pas été trouvé dans la grille
return false;
}
};