- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 59 lines of Go from the credited upstream file 1613E.go.
- The implementation visibly relies on sequence storage.
- 1 loop block 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.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 "bytes"6 . "fmt"7 "io"8)9 1011func CF1613E(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 type pair struct{ x, y int }16 dir4 := []pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}17 18 var T, n, m int19 for Fscan(in, &T); T > 0; T-- {20 Fscan(in, &n, &m)21 x, y := 0, -122 g := make([][]byte, n)23 for i := range g {24 Fscan(in, &g[i])25 if y < 0 {26 x, y = i, bytes.IndexByte(g[i], 'L')27 }28 }29 for _, d := range dir4 {30 x, y := x+d.x, y+d.y31 if 0 <= x && x < n && 0 <= y && y < m && g[x][y] == '.' {32 o:33 for {34 dir := pair{}35 for _, d := range dir4 {36 if x, y := x+d.x, y+d.y; 0 <= x && x < n && 0 <= y && y < m && g[x][y] == '.' {37 if dir != (pair{}) {38 break o39 }40 dir = d41 }42 }43 g[x][y] = '+'44 if dir == (pair{}) {45 break46 }47 x += dir.x48 y += dir.y49 }50 }51 }52 for _, r := range g {53 Fprintf(out, "%s\n", r)54 }55 }56}57 5859