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
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
std::vector pathRow { 0, 0, 1, 1, -1, 1, -1, -1 };
std::vector pathCol { 1, -1, -1, 1, 1, 0, 0, -1 };
bool IFValid(int rowNew, int colNew, std::vector< std::vector<bool> > visited) {
if (rowNew >= 0 && colNew >= 0 && rowNew < 3 && colNew < 3 && !visited[rowNew][colNew])
return true;
return false;
}
void FindWord(std::vector< std::vector<char> > board, std::vector< std::vector<bool> > visited
, int row, int col, std::string word, std::vector<std::string> englishDic)
{
if (std::find(begin(englishDic), end(englishDic), word) != end(englishDic))
std::cout << word << '\n';
if (board.size() == word.size())
return;
for (int i = 0; i < pathRow.size(); i++)
{
int rowNew = row + pathRow[i];
int colNew = col + pathCol[i];
if (IFValid(rowNew, colNew, visited))
{
visited[rowNew][colNew] = true;
FindWord(board, visited, rowNew, colNew, word + board[rowNew][colNew], englishDic);
visited[rowNew][colNew] = false;
}
}
}
int main() {
std::vector< std::vector<char> > board{{'G', 'I', 'Z'},
{'U', 'E', 'K'},
{'Q', 'S', 'E'}};
std::vector< std::vector<bool> > visited {{false, false, false },
{false, false, false },
{false, false, false }};
std::vector<std::string> englishDictionary{"GEEKS", "QUIZ", "FOR", "GO", "EUGE"};
std::string word;
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
{
visited[row][col] = true;
FindWord(board, visited, 0, 0, word + board[row][col], englishDictionary);
visited[row][col] = false;
}
return 0;
}
| |