Problem solution · Go

Codeforces 676D — Theseus and labyrinth

Codeforces 676D — Theseus and labyrinth: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
105 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

Direct simulation

For Codeforces 676D — Theseus and labyrinth, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

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

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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 676D — Theseus and labyrinth · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gofunc CF676D(_r io.Reader, _w io.Writer) {	doorTable := [...]string{		'+': "++++",		'-': "-|-|",		'|': "|-|-",		'^': "^>v<",		'>': ">v<^",		'v': "v<^>",		'<': "<^>v",		'U': "URDL",		'R': "RDLU",		'D': "DLUR",		'L': "LURD",	}	dir4 := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} // 上右下左	dirTable := [...][]int{		'+': {0, 1, 2, 3},		'-': {1, 3},		'|': {0, 2},		'^': {0},		'>': {1},		'v': {2},		'<': {3},		'U': {1, 2, 3},		'R': {0, 2, 3},		'D': {0, 1, 3},		'L': {0, 1, 2},	}	canBack := func(from int, backs []int) bool {		for _, back := range backs {			if (back+2)&3 == from {				return true			}		}		return false	} 	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush()	var n, m, ax, ay, bx, by int	Fscan(in, &n, &m)	g := make([][]byte, n)	for i := range g {		Fscan(in, &g[i])	}	Fscan(in, &ax, &ay, &bx, &by)	bx--	by-- 	type stat struct{ x, y, rot int }	qs := [4][]stat{{{ax - 1, ay - 1, 0}}}	allEmpty := func() bool {		for _, q := range qs {			if len(q) > 0 {				return false			}		}		return true	}	vis := [1000][1000][4]bool{}	for time := 0; !allEmpty(); time++ {		q := qs[time&3]		qs[time&3] = []stat{}		for _, s := range q {			if s.x == bx && s.y == by {				Fprint(out, time)				return			}			if vis[s.x][s.y][s.rot] {				continue			}			vis[s.x][s.y][s.rot] = true			for rotTimes := 0; rotTimes < 4; rotTimes++ {				rot := (s.rot + rotTimes) & 3				door := doorTable[g[s.x][s.y]][rot]				for _, i := range dirTable[door] {					d := dir4[i]					x, y := s.x+d[0], s.y+d[1]					if x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*' || vis[x][y][rot] {						continue					}					door1 := doorTable[g[x][y]][rot]					if !canBack(i, dirTable[door1]) {						continue					}					tarTime := time + rotTimes + 1					qs[tarTime&3] = append(qs[tarTime&3], stat{x, y, rot})				}			}		}	}	Fprint(out, -1)} //func main() { CF676D(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 ↗