Problem solution · Go

Codeforces 79D — Password

Codeforces 79D — Password: a Go solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Breadth-first search
Source
EndlessCheng Codeforces Go
Length
85 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Codeforces 79D — Password, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 85 lines of Go from the credited upstream file 79D.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 79D — Password · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io"	"math/bits"	"slices") // https://github.com/EndlessChengfunc cf79D(in io.Reader, out io.Writer) {	var n, k, l int	Fscan(in, &n, &k, &l)	x := make([]int, k)	for i := range x {		Fscan(in, &x[i])	}	t := []int{}	for i := 0; i < k; {		t = append(t, x[i])		for i++; i < k && x[i] == x[i-1]+1; i++ {		}		t = append(t, x[i-1]+1)	}	x = t	k = len(x) 	a := make([]int, l)	for i := range a {		Fscan(in, &a[i])	}	slices.Sort(a)	dis := make([]int, n+2)	bfs := func(st int) []int {		for i := range dis {			dis[i] = 1e9		}		q := []int{st}		dis[st] = 0		for len(q) > 0 {			v := q[0]			q = q[1:]			for _, w := range a {				if v > w && dis[v]+1 < dis[v-w] {					dis[v-w] = dis[v] + 1					q = append(q, v-w)				}				if v+w < len(dis) && dis[v]+1 < dis[v+w] {					dis[v+w] = dis[v] + 1					q = append(q, v+w)				}			}		}		dx := make([]int, k)		for i, v := range x {			dx[i] = dis[v]		}		return dx	}	dx := make([][]int, k)	for i, v := range x {		dx[i] = bfs(v)	} 	f := make([]int, 1<<k)	for i := 1; i < len(f); i++ {		if bits.OnesCount(uint(i))&1 > 0 {			continue		}		f[i] = 1e9		tz := bits.TrailingZeros(uint(i))		for m := uint(i ^ 1<<tz); m > 0; m &= m - 1 {			j := bits.TrailingZeros(m)			f[i] = min(f[i], f[i^1<<tz^1<<j]+dx[tz][j])		}	}	ans := f[1<<k-1]	if ans == 1e9 {		ans = -1	}	Fprint(out, ans)} //func main() { cf79D(os.Stdin, os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗