|  | 
|  | 1 | +/* | 
|  | 2 | + * Search a given word in a 2D board containing letters.  | 
|  | 3 | + * The word can be constructed by sequentially traversing adjacent  | 
|  | 4 | + * horizontal or vertical cells. In a sequence to form word, | 
|  | 5 | + * letter on same position can not be used more than once. | 
|  | 6 | + *  | 
|  | 7 | + * Example: | 
|  | 8 | + * Input: | 
|  | 9 | + *  | 
|  | 10 | + * ['H' 'E' 'Y' 'A'] | 
|  | 11 | + * ['O' 'L' 'A' 'Y'] | 
|  | 12 | + * ['I' 'L' 'O' 'V'] | 
|  | 13 | + *  | 
|  | 14 | + * Word: HELLO | 
|  | 15 | + * Output: Yes/true | 
|  | 16 | + *  | 
|  | 17 | + * Word: YELL | 
|  | 18 | + * Output: Yes/true | 
|  | 19 | + *  | 
|  | 20 | + * Word: LOVE | 
|  | 21 | + * Output: No/false | 
|  | 22 | + *  | 
|  | 23 | + * Assumption: grid or word does not contain '$' char | 
|  | 24 | + *  | 
|  | 25 | + * Approach: We can use Depth First Search with backtracking to solve this. | 
|  | 26 | + * We can search the grid to match the grid the first letter of search word | 
|  | 27 | + * in the grid, and then apply depth first search on the grid. | 
|  | 28 | + * Finding appropriate next characters in the search word at each depth while | 
|  | 29 | + * searching sequentially in the four directions. | 
|  | 30 | + *  | 
|  | 31 | + * We need some way to mark a position in grid as already visited. I will use | 
|  | 32 | + * '$' to mark the position as visited, and we will reset the letter at the | 
|  | 33 | + * position back to original letter at the end of backtracking. | 
|  | 34 | + *  | 
|  | 35 | + */ | 
|  | 36 | + | 
|  | 37 | +#include <iostream> | 
|  | 38 | +#include <vector> | 
|  | 39 | +#include <iomanip> | 
|  | 40 | + | 
|  | 41 | +bool depth_first_search(std::vector<std::vector<char>>& grid, const std::string& word, | 
|  | 42 | +    int r, int c, int index) | 
|  | 43 | +{ | 
|  | 44 | +    if (index == word.size()) { | 
|  | 45 | +        return true; | 
|  | 46 | +    } | 
|  | 47 | +    int rows = grid.size(); | 
|  | 48 | +    int cols = grid[0].size(); | 
|  | 49 | +    if (r < 0 || r >= rows || c < 0 || c >= cols) { | 
|  | 50 | +        return false; | 
|  | 51 | +    } | 
|  | 52 | + | 
|  | 53 | +    if (word[index] != grid[r][c]) { | 
|  | 54 | +        return false; | 
|  | 55 | +    } | 
|  | 56 | + | 
|  | 57 | +    char cur = grid[r][c]; | 
|  | 58 | +    grid[r][c] = '$'; | 
|  | 59 | +    bool result = false; | 
|  | 60 | + | 
|  | 61 | +    result = depth_first_search(grid, word, r - 1, c, index + 1) || | 
|  | 62 | +        depth_first_search(grid, word, r + 1, c, index + 1) || | 
|  | 63 | +        depth_first_search(grid, word, r, c - 1, index + 1) || | 
|  | 64 | +        depth_first_search(grid, word, r, c + 1, index + 1); | 
|  | 65 | + | 
|  | 66 | +    grid[r][c] = cur; | 
|  | 67 | +    return result; | 
|  | 68 | +} | 
|  | 69 | + | 
|  | 70 | + | 
|  | 71 | + | 
|  | 72 | +bool grid_search(std::vector<std::vector<char>>& grid, const std::string& word) | 
|  | 73 | +{ | 
|  | 74 | +    int rows = grid.size(); | 
|  | 75 | +    int cols = grid[0].size(); | 
|  | 76 | +    for (int i = 0; i < rows; ++i) { | 
|  | 77 | +        for (int j = 0; j < cols; ++j) { | 
|  | 78 | +           if (word[0] == grid[i][j]) { | 
|  | 79 | +               if (depth_first_search(grid, word, i, j, 0)) { | 
|  | 80 | +                   return true; | 
|  | 81 | +               } | 
|  | 82 | +           } | 
|  | 83 | +        } | 
|  | 84 | +    } | 
|  | 85 | +    return false; | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +void print_grid(const std::vector<std::vector<char>>& grid) | 
|  | 89 | +{ | 
|  | 90 | +    for (auto vec : grid) { | 
|  | 91 | +        std::cout << "{ "; | 
|  | 92 | +        for (auto c : vec) { | 
|  | 93 | +            std::cout << c << " "; | 
|  | 94 | +        } | 
|  | 95 | +        std::cout << "}" << std::endl; | 
|  | 96 | +    } | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | +int main() | 
|  | 100 | +{ | 
|  | 101 | +    std::vector<std::vector<char>> grid{ | 
|  | 102 | +        {'H', 'E', 'Y', 'A'}, | 
|  | 103 | +        {'O', 'L', 'A', 'Y'}, | 
|  | 104 | +        {'I', 'L', 'O', 'V'} | 
|  | 105 | +    }; | 
|  | 106 | +    print_grid(grid); | 
|  | 107 | +    std::cout << std::boolalpha; | 
|  | 108 | +    std::cout << "Does word 'HELLO' exist in grid? "; | 
|  | 109 | +    bool result = grid_search(grid, "HELLO"); | 
|  | 110 | +    std::cout << result << std::endl; | 
|  | 111 | + | 
|  | 112 | +    std::cout << "Does word 'HELL' exist in grid? "; | 
|  | 113 | +    result = grid_search(grid, "HELL"); | 
|  | 114 | +    std::cout << result << std::endl; | 
|  | 115 | + | 
|  | 116 | +    std::cout << "Does word 'LOVE' exist in grid? "; | 
|  | 117 | +    result = grid_search(grid, "LOVE"); | 
|  | 118 | +    std::cout << result << std::endl; | 
|  | 119 | + | 
|  | 120 | +    std::vector<std::vector<char>> grid2 { | 
|  | 121 | +        {'A','B','C','E'}, | 
|  | 122 | +        {'S','F','C','S'}, | 
|  | 123 | +        {'A','D','E','E'} | 
|  | 124 | +    }; | 
|  | 125 | + | 
|  | 126 | +    print_grid(grid2); | 
|  | 127 | +    std::cout << "Does word 'SEE' exist in grid? "; | 
|  | 128 | +    result = grid_search(grid2, "SEE"); | 
|  | 129 | +    std::cout << result << std::endl; | 
|  | 130 | + | 
|  | 131 | +    std::cout << "Does word 'ABCCSE' exist in grid? "; | 
|  | 132 | +    result = grid_search(grid2, "ABCCSE"); | 
|  | 133 | +    std::cout << result << std::endl; | 
|  | 134 | + | 
|  | 135 | +    std::cout << "Does word 'ABDE' exist in grid? "; | 
|  | 136 | +    result = grid_search(grid2, "ABDE"); | 
|  | 137 | +    std::cout << result << std::endl; | 
|  | 138 | + | 
|  | 139 | +    return 0; | 
|  | 140 | +} | 
0 commit comments