forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
199 lines (188 loc) · 5.06 KB
/
search.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package copypasta
import "sort"
// NOTE: 对于搜索格子的题,可以不用创建 vis 而是通过修改格子的值为范围外的值(如零、负数、'#' 等)来做到这一点
func searchCollection() {
// DFS 格点找有多少个连通分量
// 下列代码来自 LC162C https://leetcode-cn.com/problems/number-of-closed-islands/
dfsGrids := func(grid [][]int) (comps int) {
dir4 := [...][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右
n, m := len(grid), len(grid[0])
vis := make([][]bool, n)
for i := range vis {
vis[i] = make([]bool, m)
}
var f func(i, j int) bool
f = func(i, j int) bool {
// 出边界的不算
if i < 0 || i >= n || j < 0 || j >= m {
return false
}
if vis[i][j] || grid[i][j] == 1 {
return true
}
vis[i][j] = true
validComp := true
for _, dir := range dir4 {
if !f(i+dir[0], j+dir[1]) {
// 遍历完该连通分量再 return,保证不重不漏
validComp = false
}
}
return validComp
}
for i, gi := range grid {
for j, gij := range gi {
if gij == 0 && !vis[i][j] && f(i, j) {
comps++
}
}
}
return
}
// 生成字符串 s 的所有长度至多为 r 的非空子串
// https://codeforces.ml/problemset/problem/120/H
genSubStrings := func(s string, r int) []string {
a := []string{}
var f func(s, sub string)
f = func(s, sub string) {
a = append(a, sub)
if len(sub) < r {
for i, b := range s {
f(s[i+1:], sub+string(b))
}
}
}
f(s, "")
a = a[1:] // 去掉空字符串
sort.Strings(a)
j := 0
for i := 1; i < len(a); i++ {
if a[j] != a[i] {
j++
a[j] = a[i]
}
}
return a[:j+1]
}
// 从左往右枚举排列(可以剪枝)
// 即有 n 个位置,从左往右地枚举每个位置上可能出现的值(值必须在 sets 中),且每个位置上的元素不能重复
// 例题见 LC169D https://leetcode-cn.com/problems/verbal-arithmetic-puzzle/
dfsPermutations := func(n int, sets []int) bool {
used := make([]bool, len(sets))
//used := [10]bool{}
var f func(cur, x, y int) bool
f = func(pos, x, y int) bool {
if pos == n {
return true // custom
}
// 对每个位置,枚举可能出现的值,跳过已经枚举的值
for i, v := range sets {
_ = v
// custom pruning
//if {
// continue
//}
if used[i] {
continue
}
used[i] = true
// custom calc x y
if f(pos+1, x, y) {
return true
}
used[i] = false
}
return false
}
return f(0, 0, 0)
}
// 从一个长度为 n 的数组中选择 r 个元素,按字典序生成所有组合,每个组合用下标表示 r <= n
// 由于实现上直接传入了 indexes,所以在 do 中不能修改 indexes。若要修改则代码在传入前需要 copy 一份
// 参考 https://docs.python.org/3/library/itertools.html#itertools.combinations
// https://stackoverflow.com/questions/41694722/algorithm-for-itertools-combinations-in-python
combinations := func(n, r int, do func(indexes []int)) {
indexes := make([]int, r)
for i := range indexes {
indexes[i] = i
}
do(indexes)
for {
i := r - 1
for ; i >= 0; i-- {
if indexes[i] != i+n-r {
break
}
}
if i == -1 {
return
}
indexes[i]++
for j := i + 1; j < r; j++ {
indexes[j] = indexes[j-1] + 1
}
do(indexes)
}
}
// 从一个长度为 n 的数组中选择 r 个元素,按字典序生成所有排列,每个排列用下标表示 r <= n
// 由于实现上直接传入了 indexes,所以在 do 中不能修改 indexes。若要修改则代码在传入前需要 copy 一份
// 参考 https://docs.python.org/3/library/itertools.html#itertools.permutations
permutations := func(n, r int, do func(indexes []int)) {
indexes := make([]int, n)
for i := range indexes {
indexes[i] = i
}
do(indexes[:r])
cycles := make([]int, r)
for i := range cycles {
cycles[i] = n - i
}
for {
i := r - 1
for ; i >= 0; i-- {
cycles[i]--
if cycles[i] == 0 {
tmp := indexes[i]
copy(indexes[i:], indexes[i+1:])
indexes[n-1] = tmp
cycles[i] = n - i
} else {
j := cycles[i]
indexes[i], indexes[n-j] = indexes[n-j], indexes[i]
do(indexes[:r])
break
}
}
if i == -1 {
return
}
}
}
// 生成全排列(非字典序)
// 会修改 arr
// Permute the values at index i to len(arr)-1.
// https://codeforces.ml/problemset/problem/910/C
var _permute func([]int, int, func())
_permute = func(arr []int, i int, do func()) {
if i == len(arr) {
do()
return
}
_permute(arr, i+1, do)
for j := i + 1; j < len(arr); j++ {
arr[i], arr[j] = arr[j], arr[i]
_permute(arr, i+1, do)
arr[i], arr[j] = arr[j], arr[i]
}
}
permuteAll := func(arr []int, do func()) { _permute(arr, 0, do) }
// 剪枝:
// todo https://blog.csdn.net/weixin_43914593/article/details/104613920
// A*:
// todo https://blog.csdn.net/weixin_43914593/article/details/104935011
// 舞蹈链
// TODO: https://oi-wiki.org/search/dlx/
_ = []interface{}{
dfsPermutations, dfsGrids,
genSubStrings, combinations, permutations, permuteAll,
}
}