-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path808.go
72 lines (63 loc) · 1.15 KB
/
808.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
// UVa 808 - Bee Breeding
package main
import (
"fmt"
"os"
)
const max = 10000
type cell struct{ x, y int }
var grid = func() []cell {
directions := [][2]int{{-1, 0}, {-1, 1}, {0, 1}, {1, 0}, {1, -1}, {0, -1}}
grid := make([]cell, max+1)
x, y, cnt := 0, 0, 1
grid[cnt] = cell{x, y}
for ring := 1; ; ring++ {
y--
if cnt++; cnt > max {
break
}
grid[cnt] = cell{x, y}
for i, direction := range directions {
side := ring
if i == 0 {
side--
}
for ; side > 0; side-- {
x += direction[0]
y += direction[1]
if cnt++; cnt > max {
break
}
grid[cnt] = cell{x, y}
}
}
}
return grid
}()
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
func distance(a, b int) int {
xDiff := abs(grid[a].x - grid[b].x)
yDiff := abs(grid[a].y - grid[b].y)
if yDiff > xDiff {
yDiff -= xDiff
}
return xDiff + yDiff
}
func main() {
in, _ := os.Open("808.in")
defer in.Close()
out, _ := os.Create("808.out")
defer out.Close()
var a, b int
for {
if fmt.Fscanf(in, "%d%d", &a, &b); a == 0 && b == 0 {
break
}
fmt.Fprintf(out, "The distance between cells %d and %d is %d.\n", a, b, distance(a, b))
}
}