Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[
["ABCE"],
["SFCS"],
["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
1 public static bool WordSearch(List<List<char>> board, string word) 2 { 3 int r = board.Count; 4 int c = board[0].Count; 5 6 List<List<bool>> map = new List<List<bool>>(); 7 List<bool> temp = new List<bool>(); 8 9 // initial map 10 for (int i = 0; i < c; i++) 11 { 12 temp.Add(false); 13 } 14 for (int i = 0; i < r; i++) 15 { 16 map.Add(new List<bool>(temp)); 17 } 18 19 for (int i = 0; i < r; i++) 20 { 21 for (int j = 0; j < c; j++) 22 { 23 if (WordSearchHelper(board, i, j, word, 0, map)) 24 return true; 25 } 26 } 27 28 return false; 29 } 30 31 public static bool WordSearchHelper(List<List<char>> board, int i, int j, string word, int index, List<List<bool>> map) 32 { 33 int r = board.Count; 34 int c = board[0].Count; 35 36 if (index == word.Length) 37 return true; 38 39 if (i < 0 || i >= r || j < 0 || j >= c) 40 return false; 41 42 if (board[i][j] == word[index] && map[i][j] == false) 43 { 44 map[i][j] = true; 45 46 // first row 47 if (i == 0) 48 { 49 // first column 50 if (j == 0) 51 if (WordSearchHelper(board, i, j + 1, word, index + 1, map) || 52 WordSearchHelper(board, i + 1, j, word, index + 1, map)) 53 return true; 54 else 55 { 56 map[i][j] = false; 57 return false; 58 } 59 // last column 60 else if (j == c - 1) 61 if (WordSearchHelper(board, i, j - 1, word, index + 1, map) || 62 WordSearchHelper(board, i + 1, j, word, index + 1, map)) 63 return true; 64 else 65 { 66 map[i][j] = false; 67 return false; 68 } 69 // other columns 70 else 71 if (WordSearchHelper(board, i, j - 1, word, index + 1, map) || 72 WordSearchHelper(board, i, j + 1, word, index + 1, map) || 73 WordSearchHelper(board, i + 1, j, word, index + 1, map)) 74 return true; 75 else 76 { 77 map[i][j] = false; 78 return false; 79 } 80 } 81 // last row 82 else if (i == r - 1) 83 { 84 if (j == 0) 85 if (WordSearchHelper(board, i, j + 1, word, index + 1, map) || 86 WordSearchHelper(board, i - 1, j, word, index + 1, map)) 87 return true; 88 else 89 { 90 map[i][j] = false; 91 return false; 92 } 93 else if (j == c - 1) 94 if (WordSearchHelper(board, i, j - 1, word, index + 1, map) || 95 WordSearchHelper(board, i - 1, j, word, index + 1, map)) 96 return true; 97 else 98 { 99 map[i][j] = false; 100 return false; 101 } 102 else 103 if (WordSearchHelper(board, i, j - 1, word, index + 1, map) || 104 WordSearchHelper(board, i, j + 1, word, index + 1, map) || 105 WordSearchHelper(board, i - 1, j, word, index + 1, map)) 106 return true; 107 else 108 { 109 map[i][j] = false; 110 return false; 111 } 112 } 113 // other rows 114 else 115 { 116 if (j == 0) 117 if (WordSearchHelper(board, i - 1, j, word, index + 1, map) || 118 WordSearchHelper(board, i + 1, j, word, index + 1, map) || 119 WordSearchHelper(board, i, j + 1, word, index + 1, map)) 120 return true; 121 else 122 { 123 map[i][j] = false; 124 return false; 125 } 126 else if (j == c - 1) 127 if (WordSearchHelper(board, i - 1, j, word, index + 1, map) || 128 WordSearchHelper(board, i + 1, j, word, index + 1, map) || 129 WordSearchHelper(board, i, j - 1, word, index + 1, map)) 130 return true; 131 else 132 { 133 map[i][j] = false; 134 return false; 135 } 136 else 137 if (WordSearchHelper(board, i, j - 1, word, index + 1, map) || 138 WordSearchHelper(board, i - 1, j, word, index + 1, map) || 139 WordSearchHelper(board, i, j + 1, word, index + 1, map) || 140 WordSearchHelper(board, i + 1, j, word, index + 1, map)) 141 return true; 142 else 143 { 144 map[i][j] = false; 145 return false; 146 } 147 } 148 } 149 else 150 return false; 151 }
代码分析:
递归。