-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhas_path.go
52 lines (45 loc) · 1.01 KB
/
has_path.go
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
package main
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param matrix string字符串
* @param rows int整型
* @param cols int整型
* @param path string字符串
* @return bool布尔型
*/
func hasPath(matrix string, rows int, cols int, path string) bool {
// https://www.nowcoder.com/share/jump/14730551684078698139
var walk func(a, b, j int) bool
var mark []bool
walk = func(a, b, j int) bool {
if j == len(path) {
return true
}
if a < 0 || a >= rows || b < 0 || b >= cols {
return false
}
idx := a*cols + b
if mark[idx] {
return false
}
if matrix[idx] != path[j] {
return false
}
mark[idx] = true
if walk(a-1, b, j+1) || walk(a+1, b, j+1) || walk(a, b-1, j+1) || walk(a, b+1, j+1) {
return true
}
mark[idx] = false
return false
}
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
mark = make([]bool, rows*cols)
if walk(r, c, 0) {
return true
}
}
}
return false
}