@@ -138,7 +138,51 @@ var exist = function(board, word) {
138
138
};
139
139
```
140
140
141
+ ### Go
142
+
143
+ ``` go
144
+ func exist (board [][]byte , word string ) bool {
145
+ if len (board) == 0 {
146
+ return false
147
+ }
148
+ // 标记数组
149
+ isVisited := make ([][]bool ,len (board))
150
+ for i := 0 ; i < len (board); i++ {
151
+ isVisited[i] = make ([]bool , len (board[0 ]))
152
+ }
153
+ for i := 0 ; i < len (board); i++ {
154
+ for j := 0 ; j < len (board[0 ]); j++ {
155
+ if board[i][j] == word[0 ] {
156
+ if bfs (board,i,j,isVisited,word,0 ) {
157
+ return true
158
+ }
159
+ }
160
+ }
161
+ }
162
+ return false
163
+ }
164
+
165
+ func bfs (board [][]byte , i ,j int , isVisited [][]bool , word string , index int ) bool {
166
+ if index == len (word) {
167
+ return true
168
+ }
169
+ if i < 0 || j < 0 || i == len (board) || j == len (board[0 ]) || isVisited[i][j] || board[i][j] != word[index] {
170
+ return false
171
+ }
172
+ isVisited[i][j] = true
173
+ res := bfs (board, i+1 , j, isVisited, word, index+1 ) ||
174
+ bfs (board, i, j+1 , isVisited, word, index+1 ) ||
175
+ bfs (board, i-1 , j, isVisited, word, index+1 ) ||
176
+ bfs (board, i, j-1 , isVisited, word, index+1 )
177
+ isVisited[i][j] = false
178
+ return res
179
+ }
180
+ ```
181
+
182
+
183
+
141
184
### ...
185
+
142
186
```
143
187
144
188
```
0 commit comments