- 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
- 61 lines of Go from the credited upstream file 1205C.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.
Use this to learn the idea, then write your own version.
1package main2 3import . "fmt"4 56func cf1205C() {7 q := func(a, b, c, d int) (pal byte) {8 Println("?", a+1, b+1, c+1, d+1)9 Scan(&pal)10 return pal ^ 111 }12 13 var n, t int14 Scan(&n)15 a := make([][]byte, n)16 for i := range a {17 a[i] = make([]byte, n)18 }19 a[0][0] = '1'20 a[0][1] = '0'21 for i := range a {22 if i == 1 {23 a[1][1] = a[0][0] ^ q(0, 0, 1, 1)24 a[1][2] = a[0][1] ^ q(0, 1, 1, 2)25 a[1][0] = a[1][2] ^ q(1, 0, 1, 2)26 } else if i > 1 {27 a[i][0] = a[i-2][0] ^ q(i-2, 0, i, 0)28 a[i][1] = a[i-2][1] ^ q(i-2, 1, i, 1)29 }30 for j := 2; j < n; j++ {31 a[i][j] = a[i][j-2] ^ q(i, j-2, i, j)32 }33 if i > 1 && a[i-2][i-2] != a[i][i] {34 t = i - 135 }36 }37 38 pre, v := a[t][t-1], a[t][t]39 tar := v40 var add byte41 if v == a[t-1][t-1] == (pre == a[t][t+1]) {42 add = q(t-1, t-1, t, t+1)43 } else {44 tar = a[t+1][t+1]45 add = q(t, t-1, t+1, t+1)46 }47 rev := pre == tar == (add > 0)48 49 Println("!")50 for i, row := range a {51 if rev {52 for j := i%2 ^ 1; j < n; j += 2 {53 row[j] ^= 154 }55 }56 Printf("%s\n", row)57 }58}59 6061