-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
69 lines (56 loc) · 1.09 KB
/
common.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
package main
import (
"bufio"
"os"
"strconv"
"strings"
)
const Size = 70
type P struct{ x, y int }
var (
Start = P{0, 0}
Goal = P{Size, Size}
Delta = []P{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
)
func parseInput() []P {
var m []P
scanner := bufio.NewScanner(os.Stdin)
for i := 0; scanner.Scan(); i++ {
ff := strings.Split(scanner.Text(), ",")
x, _ := strconv.Atoi(ff[0])
y, _ := strconv.Atoi(ff[1])
m = append(m, P{x, y})
}
return m
}
func BFS(corrupted map[P]struct{}) []P {
queue := []P{Start}
visited := map[P]struct{}{Start: {}}
parent := map[P]P{}
for len(queue) > 0 {
var curr P
curr, queue = queue[0], queue[1:]
if curr == Goal {
var path []P
for p := Goal; p != Start; p = parent[p] {
path = append(path, p)
}
return path
}
for _, d := range Delta {
n := P{curr.x + d.x, curr.y + d.y}
if n.x < 0 || n.x > Size || n.y < 0 || n.y > Size {
continue
}
if _, ok := corrupted[n]; ok {
continue
}
if _, ok := visited[n]; !ok {
visited[n] = struct{}{}
parent[n] = curr
queue = append(queue, n)
}
}
}
return nil
}