still too slow

This commit is contained in:
magical 2025-12-10 07:21:28 +00:00
parent c2717ae9ab
commit 0abdabad64

View File

@ -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 {
for i, v := range p { here:
t[i] += v for t, c := s, cost[s]; ; {
if t[i] > target[i] { for i, v := range p {
// blown target, cut path t[i] += v
continue next if t[i] > target[i] {
} // blown target, cut path
} break here
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
}
} }
} }
} else { c += 1
cost[t] = cost[s] + 1
heap.Push(h, t) if cost_t, ok := cost[t]; ok {
if c < cost_t {
cost[t] = c
}
} else {
cost[t] = c
states = append(states, t)
}
} }
} }
} }