-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathjump_game.go
More file actions
289 lines (228 loc) · 5.95 KB
/
Copy pathjump_game.go
File metadata and controls
289 lines (228 loc) · 5.95 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"fmt"
)
/*
* 跳跃游戏(贪心算法)
*
* 算法思路:
* - 给定一个数组,每个元素表示当前位置最大可跳跃步数
* - 判断能否到达最后一个位置
* - 贪心策略:每一步记录当前能到达的最远位置
*/
// CanJump 判断能否到达最后一个位置
func CanJump(nums []int) bool {
if len(nums) == 0 || len(nums) == 1 {
return true
}
maxReach := 0
for i := 0; i < len(nums); i++ {
// 如果当前位置超过了最远可达位置,则无法到达
if i > maxReach {
return false
}
// 更新最远可达位置
if i+nums[i] > maxReach {
maxReach = i + nums[i]
}
// 如果已经能到达最后一位,提前返回true
if maxReach >= len(nums)-1 {
return true
}
}
return maxReach >= len(nums)-1
}
// MinJumps 返回到达最后一位所需的最少跳跃次数
func MinJumps(nums []int) int {
if len(nums) == 0 || len(nums) == 1 {
return 0
}
// 检查是否可达
maxReach := 0
for i := 0; i < len(nums)-1; i++ {
if i > maxReach {
return -1
}
if i+nums[i] > maxReach {
maxReach = i + nums[i]
}
}
if maxReach < len(nums)-1 {
return -1
}
// 贪心法计算最少跳跃次数
jumps := 0
currentEnd := 0
farthest := 0
for i := 0; i < len(nums)-1; i++ {
if i+nums[i] > farthest {
farthest = i + nums[i]
}
if i == currentEnd {
jumps++
currentEnd = farthest
}
}
return jumps
}
// JumpPath 返回到达终点的跳跃路径(索引序列)
func JumpPath(nums []int) []int {
path := []int{}
if len(nums) == 0 {
return path
}
if len(nums) == 1 {
return []int{0}
}
// 检查是否可达
maxReach := 0
for i := 0; i < len(nums); i++ {
if i > maxReach {
return []int{}
}
if i+nums[i] > maxReach {
maxReach = i + nums[i]
}
if maxReach >= len(nums)-1 {
break
}
}
if maxReach < len(nums)-1 {
return []int{}
}
// 贪心构造跳跃路径
path = append(path, 0)
currentPos := 0
for currentPos < len(nums)-1 {
nextPos := currentPos
maxNextReach := currentPos + nums[currentPos]
// 遍历当前位置能到达的范围内,找到能到达的最远位置
for i := currentPos + 1; i <= currentPos+nums[currentPos] && i < len(nums); i++ {
// 如果当前位置能到达的最远位置小于当前位置能到达的最远位置,则更新当前位置能到达的最远位置
if i+nums[i] > maxNextReach {
maxNextReach = i + nums[i]
nextPos = i
}
}
if nextPos == currentPos {
return []int{}
}
path = append(path, nextPos)
currentPos = nextPos
}
return path
}
// JumpGameAnalysis 保存跳跃游戏分析结果
type JumpGameAnalysis struct {
CanReach bool
MinJumps int
Path []int
}
// AnalyzeJumpGame 综合分析跳跃游戏问题
func AnalyzeJumpGame(nums []int) JumpGameAnalysis {
canReach := CanJump(nums)
minJumps := MinJumps(nums)
path := JumpPath(nums)
return JumpGameAnalysis{
CanReach: canReach,
MinJumps: minJumps,
Path: path,
}
}
func testBasicReachable() {
fmt.Println("\n[测试1] 可到达")
nums := []int{2, 3, 1, 1, 4}
analysis := AnalyzeJumpGame(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", analysis.CanReach)
fmt.Printf("Min jumps: %d\n", analysis.MinJumps)
fmt.Printf("Path: %v\n", analysis.Path)
}
func testNotReachable() {
fmt.Println("\n[测试2] 不可到达")
nums := []int{3, 2, 1, 0, 4}
result := CanJump(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", result)
}
func testSingleElement() {
fmt.Println("\n[测试3] 单元素")
nums := []int{0}
result := CanJump(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", result)
}
func testZeroJump() {
fmt.Println("\n[测试4] 除最后一位外全为零")
nums := []int{0, 1}
result := CanJump(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", result)
}
func testLargeJumps() {
fmt.Println("\n[测试5] 跳跃步数很大")
nums := []int{10, 0, 0, 0, 0}
analysis := AnalyzeJumpGame(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", analysis.CanReach)
fmt.Printf("Min jumps: %d\n", analysis.MinJumps)
fmt.Printf("Path: %v\n", analysis.Path)
}
func testMultipleJumps() {
fmt.Println("\n[测试6] 需要多次跳跃")
nums := []int{2, 3, 1, 1, 1}
analysis := AnalyzeJumpGame(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", analysis.CanReach)
fmt.Printf("Min jumps: %d\n", analysis.MinJumps)
fmt.Printf("Path: %v\n", analysis.Path)
}
func testBlocked() {
fmt.Println("\n[测试7] 倒数第二步被阻断")
nums := []int{1, 0, 1, 0}
result := CanJump(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", result)
}
func testTwoElement() {
fmt.Println("\n[测试8] 两元素数组")
nums := []int{2, 3}
analysis := AnalyzeJumpGame(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", analysis.CanReach)
fmt.Printf("Min jumps: %d\n", analysis.MinJumps)
fmt.Printf("Path: %v\n", analysis.Path)
}
func testDecreasing() {
fmt.Println("\n[测试9] 大数组递减")
nums := []int{5, 4, 3, 2, 1, 0}
analysis := AnalyzeJumpGame(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", analysis.CanReach)
fmt.Printf("Min jumps: %d\n", analysis.MinJumps)
fmt.Printf("Path: %v\n", analysis.Path)
}
func testComplex() {
fmt.Println("\n[测试10] 复杂可达场景")
nums := []int{2, 5, 0, 0}
analysis := AnalyzeJumpGame(nums)
fmt.Printf("Input: %v\n", nums)
fmt.Printf("Can reach end: %v\n", analysis.CanReach)
fmt.Printf("Min jumps: %d\n", analysis.MinJumps)
fmt.Printf("Path: %v\n", analysis.Path)
}
func main() {
fmt.Println("==================================================")
fmt.Println("跳跃游戏 - 贪心算法 (Go)")
fmt.Println("==================================================")
testBasicReachable()
testNotReachable()
testSingleElement()
testZeroJump()
testLargeJumps()
testMultipleJumps()
testBlocked()
testTwoElement()
testDecreasing()
testComplex()
}