-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_paths.go
64 lines (62 loc) · 1.48 KB
/
find_paths.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
package lem
func CountTurns(paths []*Path, nest *Nest, ants int) int {
return len(paths[0].Rooms) + paths[0].Ants - 2
}
func FindPaths(nest *Nest, ants int) (int, []*Path) {
numberOfTurns := len(nest.Rooms) + ants - 2
var paths []*Path
for len(paths) < ants {
q := []*Room{nest.Start}
for _, r := range nest.Rooms {
r.Predecessor = nil
}
for len(q) != 0 {
u := q[0]
if len(q) > 1 {
q = q[1:]
} else {
q = q[:0]
}
uIsFlowing := false
for _, v := range u.Neighbors {
if u.Flow[v] {
uIsFlowing = true
}
}
for _, v := range u.Neighbors {
neighboringRoomIsUnvisited := v.Predecessor == nil
tunnelCapacityPermits := v.Flow[u] || !u.Flow[v]
roomCapacityPermits := !uIsFlowing || v.Flow[u] || u.Start || u.Flow[u.Predecessor]
if !v.Start && neighboringRoomIsUnvisited && tunnelCapacityPermits && roomCapacityPermits {
q = append(q, v)
v.Predecessor = u
}
}
}
if nest.End.Predecessor == nil {
break
}
nest.End.Predecessor.Flow[nest.End] = true
for v := nest.End.Predecessor; !v.Start; {
u := v.Predecessor
u.Flow[v] = !v.Flow[u]
v.Flow[u] = false
v = u
}
newPaths := GatherPaths(nest)
SendAnts(newPaths, nest, ants)
newNumberOfTurns := CountTurns(newPaths, nest, ants)
if newNumberOfTurns > numberOfTurns {
break
}
numberOfTurns = newNumberOfTurns
paths = newPaths
}
flow := 0
for _, n := range nest.End.Neighbors {
if n.Flow[nest.End] {
flow++
}
}
return flow, paths
}