still too slow
This commit is contained in:
parent
c2717ae9ab
commit
0abdabad64
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"container/heap"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -114,40 +113,38 @@ func (h *CostHeap) Pop() any {
|
|||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// it doesn't matter what order we push the buttons in --
|
||||||
|
// only how many times we push each one.
|
||||||
|
|
||||||
func best(target Jolts, pool []Jolts) int {
|
func best(target Jolts, pool []Jolts) int {
|
||||||
var cost = make(map[Jolts]int)
|
var cost = make(map[Jolts]int)
|
||||||
h := &CostHeap{nil, cost}
|
var states []Jolts
|
||||||
heap.Push(h, Jolts{})
|
states = append(states, Jolts{})
|
||||||
for h.Len() > 0 {
|
cost[Jolts{}] = 0
|
||||||
s := heap.Pop(h).(Jolts)
|
for pi, p := range pool {
|
||||||
if s == target {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// enumerate all the states that can be reached by toggling button p
|
// enumerate all the states that can be reached by toggling button p
|
||||||
next:
|
queue := states
|
||||||
for _, p := range pool {
|
fmt.Print(pi, len(states), len(queue))
|
||||||
t := s
|
for _, s := range queue {
|
||||||
|
here:
|
||||||
|
for t, c := s, cost[s]; ; {
|
||||||
for i, v := range p {
|
for i, v := range p {
|
||||||
t[i] += v
|
t[i] += v
|
||||||
if t[i] > target[i] {
|
if t[i] > target[i] {
|
||||||
// blown target, cut path
|
// blown target, cut path
|
||||||
continue next
|
break here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c += 1
|
||||||
|
|
||||||
if cost_t, ok := cost[t]; ok {
|
if cost_t, ok := cost[t]; ok {
|
||||||
if cost[s]+1 < cost_t {
|
if c < cost_t {
|
||||||
cost[t] = cost[s] + 1
|
cost[t] = c
|
||||||
for i, x := range h.heap {
|
|
||||||
if x == t {
|
|
||||||
heap.Fix(h, i)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cost[t] = cost[s] + 1
|
cost[t] = c
|
||||||
heap.Push(h, t)
|
states = append(states, t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user