Skip to content

Commit 7ffa49d

Browse files
committed
Add solution and test-cases for problem 789
1 parent f0a9eb1 commit 7ffa49d

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

leetcode/701-800/0789.Escape-The-Ghosts/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [789.Escape The Ghosts][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array ghosts, where `ghosts[i] = [xi, yi]` represents the starting position of the `ith` ghost. All inputs are **integral coordinates**.
5+
6+
Each turn, you and all the ghosts may independently choose to either **move 1 unit** in any of the four cardinal directions: north, east, south, or west, or **stay still**. All actions happen **simultaneously**.
7+
8+
You escape if and only if you can reach the target **before** any ghost reaches you. If you reach any square (including the target) at the **same time** as a ghost, it **does not** count as an escape.
9+
10+
Return `true` if it is possible to escape regardless of how the ghosts move, otherwise return `false`.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: ghosts = [[1,0],[0,3]], target = [0,1]
16+
Output: true
17+
Explanation: You can reach the destination (0, 1) after 1 turn, while the ghosts located at (1, 0) and (0, 3) cannot catch up with you.
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Escape The Ghosts
23-
```go
2422
```
23+
Input: ghosts = [[1,0]], target = [2,0]
24+
Output: false
25+
Explanation: You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.
26+
```
27+
28+
**Example 3:**
2529

30+
```
31+
Input: ghosts = [[2,0]], target = [1,0]
32+
Output: false
33+
Explanation: The ghost can reach the target at the same time as you.
34+
```
2635

2736
## 结语
2837

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func distance(a, b int) int {
4+
if a < 0 {
5+
a = -a
6+
}
7+
if b < 0 {
8+
b = -b
9+
}
10+
return a + b
11+
}
12+
13+
func Solution(ghosts [][]int, target []int) bool {
14+
a, b := target[0], target[1]
15+
cmp := distance(a, b)
16+
for _, g := range ghosts {
17+
a, b = target[0]-g[0], target[1]-g[1]
18+
tmp := distance(a, b)
19+
if tmp <= cmp {
20+
return false
21+
}
22+
}
23+
return true
524
}

leetcode/701-800/0789.Escape-The-Ghosts/Solution_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
ghosts [][]int
14+
target []int
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{1, 0}, {0, 3}}, []int{0, 1}, true},
18+
{"TestCase2", [][]int{{1, 0}}, []int{2, 0}, false},
19+
{"TestCase3", [][]int{{2, 0}}, []int{1, 0}, false},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.ghosts, c.target)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.ghosts, c.target)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)