Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 7e289f2

Browse files
aQuaaQua
aQua
authored and
aQua
committed
789 accepted
1 parent 5227f11 commit 7e289f2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package problem0789
22

33
func escapeGhosts(ghosts [][]int, target []int) bool {
4-
steps := target[0] + target[1]
4+
steps := countSteps([]int{0, 0}, target)
55
for _, g := range ghosts {
6-
if steps > g[0]+g[1] {
6+
if steps >= countSteps(g, target) {
77
return false
88
}
99
}
1010
return true
1111
}
12+
13+
func countSteps(from, to []int) int {
14+
return abs(to[0]-from[0]) + abs(to[1]-from[1])
15+
}
16+
17+
func abs(n int) int {
18+
if n > 0 {
19+
return n
20+
}
21+
return -n
22+
}

Algorithms/0789.escape-the-ghosts/escape-the-ghosts_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ var tcs = []struct {
1414
ans bool
1515
}{
1616

17+
{
18+
[][]int { { -1,0 },{ 0,1 },{ -1,0 },{ 0,1 },{ -1,0 } },
19+
[]int{ 0,0 },
20+
true,
21+
},
22+
1723
{
1824
[][]int{{1, 0}, {0, 3}},
1925
[]int{0, 1},

0 commit comments

Comments
 (0)