-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwordsearch.go
More file actions
33 lines (31 loc) · 742 Bytes
/
wordsearch.go
File metadata and controls
33 lines (31 loc) · 742 Bytes
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
package wordsearch
func exist(board [][]byte, word string) bool {
if len(board) == 0 || len(board[0]) == 0 {
return false
}
m, n := len(board), len(board[0])
for x := 0; x < m; x++ {
for y := 0; y < n; y++ {
if isExist(board, word, m, n, x, y, 0) {
return true
}
}
}
return false
}
func isExist(board [][]byte, word string, m, n, x, y, z int) bool {
if x < 0 || y < 0 || x >= m || y >= n || board[x][y] != word[z] {
return false
}
t := board[x][y]
board[x][y] = 0
if z++; z >= len(word) ||
isExist(board, word, m, n, x-1, y, z) ||
isExist(board, word, m, n, x+1, y, z) ||
isExist(board, word, m, n, x, y-1, z) ||
isExist(board, word, m, n, x, y+1, z) {
return true
}
board[x][y] = t
return false
}