- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 67 lines of Go from the credited upstream file 1198D.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
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 . "fmt"6 "io"7)8 910func CF1198D(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 min := func(a, b int) int {13 if a < b {14 return a15 }16 return b17 }18 max := func(a, b int) int {19 if a > b {20 return a21 }22 return b23 }24 25 var n int26 Fscan(in, &n)27 a := make([][]byte, n)28 for i := range a {29 Fscan(in, &a[i])30 }31 dp := make([][50][50][50]int, n)32 for i := range dp {33 for j := range dp[i] {34 for k := range dp[i][j] {35 for l := range dp[i][j][k] {36 dp[i][j][k][l] = -137 }38 }39 }40 }41 var f func(x1, y1, x2, y2 int) int42 f = func(x1, y1, x2, y2 int) (res int) {43 if x1 == x2 && y1 == y2 {44 if a[x1][y1] == '#' {45 return 146 }47 return48 }49 dv := &dp[x1][y1][x2][y2]50 if *dv != -1 {51 return *dv52 }53 res = max(x2-x1+1, y2-y1+1)54 for i := x1; i < x2; i++ {55 res = min(res, f(x1, y1, i, y2)+f(i+1, y1, x2, y2))56 }57 for i := y1; i < y2; i++ {58 res = min(res, f(x1, y1, x2, i)+f(x1, i+1, x2, y2))59 }60 *dv = res61 return62 }63 Fprint(out, f(0, 0, n-1, n-1))64}65 6667