- 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
- 67 lines of Go from the credited upstream file 1023E.go.
- The implementation keeps its working state in language-native values and containers.
- 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.
Use this to learn the idea, then write your own version.
1package main2 3import (4 . "fmt"5 "strings"6)7 8type (9 input23 struct{ n int }10 req23 struct{ sx, sy, tx, ty int }11 resp23 struct{ s string }12 guess23 struct{ ans string }13)14 1516func CF1023E(in input23, Q func(req23) resp23) (gs guess23) {17 q := func(sx, sy, tx, ty int) bool { return Q(req23{sx, sy, tx, ty}).s[0] == 'Y' }18 n := in.n19 ans, t := "", ""20 defer func() { gs.ans = ans }()21 22 x0, y0 := 1, 123 for x0+y0 <= n {24 if q(x0+1, y0, n, n) {25 x0++26 ans += "D"27 } else {28 y0++29 ans += "R"30 }31 }32 x, y := n, n33 for y > y0 {34 if q(1, 1, x, y-1) {35 y--36 t += "R"37 } else {38 x--39 t += "D"40 }41 }42 ans += strings.Repeat("D", x-x0)43 for i := len(t) - 1; i >= 0; i-- {44 ans += string(t[i])45 }46 return47}48 49func ioq23() {50 51 Q := func(req req23) (resp resp23) {52 Println("?", req.sx, req.sy, req.tx, req.ty)53 Scan(&resp.s)54 return55 }56 57 58 d := input23{}59 Scan(&d.n)60 61 62 gs := CF1023E(d, Q)63 Println("!", gs.ans)64}65 6667