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