This commit is contained in:
VicRen
2020-11-27 09:04:10 +08:00
parent 21a2b9cd69
commit 42fc6f545f
2 changed files with 232 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
package main
func main() {
}
func hasCross(orders string) bool {
pm := map[[2]int]struct{}{}
current := [2]int{0, 0}
pm[current] = struct{}{}
for _, o := range orders {
np := nextPos(current, string(o))
if _, found := pm[np]; found {
return true
}
pm[np] = struct{}{}
}
return false
}
func nextPos(current [2]int, order string) [2]int {
switch order {
case "E":
current[0] += 1
case "S":
current[1] -= 1
case "W":
current[0] -= 1
case "N":
current[1] += 1
}
return current
}
func hasCross2(orders string) bool {
pm := map[pos]struct{}{}
p := &pos{0, 0}
pm[*p] = struct{}{}
for _, o := range orders {
p.Move(string(o))
if _, found := pm[*p]; found {
return true
}
pm[*p] = struct{}{}
}
return false
}
type pos struct {
x int
y int
}
func (p *pos) Equals(p2 *pos) bool {
return p.x == p2.x && p.y == p2.y
}
func (p *pos) Move(order string) {
switch order {
case "E":
p.x += 1
case "S":
p.y -= 1
case "W":
p.x -= 1
case "N":
p.y += 1
}
}