- 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
- 148 lines of Turing from the credited upstream file ccc04j5.t.
- 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.
1234567891011121314 15var ox : array 1 .. 7000 of int16var oy : array 1 .. 7000 of int17var nx : array 1 .. 7000 of int18var ny : array 1 .. 7000 of int19var ans : array 1 .. 100 of int20var level, xcoor, width, w, size, k, n : int21var done : boolean22 23get level, width, xcoor24ox (1) := 025oy (1) := 126ox (2) := width27oy (2) := 128size := 229for i : 1 .. level30 k := 031 for j : 1 .. size - 1 32 k := k + 133 nx (k) := ox (j)34 ny (k) := oy (j)35 if oy (j) = oy (j + 1) and ox (j + 1) > ox (j) then 36 w := (ox (j + 1) - ox (j)) div 337 k := k + 138 nx (k) := ox (j) + w39 ny (k) := oy (j) + 040 k := k + 141 nx (k) := ox (j) + w42 ny (k) := oy (j) + w43 k := k + 144 nx (k) := ox (j) + 2 * w45 ny (k) := oy (j) + w46 k := k + 147 nx (k) := ox (j) + 2 * w48 ny (k) := oy (j) + 049 k := k + 150 nx (k) := ox (j + 1)51 ny (k) := oy (j + 1)52 elsif oy (j) = oy (j + 1) and ox (j + 1) < ox (j) then 53 w := (ox (j) - ox (j + 1)) div 354 k := k + 155 nx (k) := ox (j) - w56 ny (k) := oy (j) + 057 k := k + 158 nx (k) := ox (j) - w59 ny (k) := oy (j) - w60 k := k + 161 nx (k) := ox (j) - 2 * w62 ny (k) := oy (j) - w63 k := k + 164 nx (k) := ox (j) - 2 * w65 ny (k) := oy (j) + 066 k := k + 167 nx (k) := ox (j + 1)68 ny (k) := oy (j + 1)69 elsif ox (j) = ox (j + 1) and oy (j + 1) < oy (j) then 70 w := (oy (j) - oy (j + 1)) div 371 k := k + 172 nx (k) := ox (j) + 073 ny (k) := oy (j) - w74 k := k + 175 nx (k) := ox (j) + w76 ny (k) := oy (j) - w77 k := k + 178 nx (k) := ox (j) + w79 ny (k) := oy (j) - 2 * w80 k := k + 181 nx (k) := ox (j) + 082 ny (k) := oy (j) - 2 * w83 k := k + 184 nx (k) := ox (j + 1)85 ny (k) := oy (j + 1)86 elsif ox (j) = ox (j + 1) and oy (j + 1) > oy (j) then 87 w := (oy (j + 1) - oy (j)) div 388 k := k + 189 nx (k) := ox (j) + 090 ny (k) := oy (j) + w91 k := k + 192 nx (k) := ox (j) - w93 ny (k) := oy (j) + w94 k := k + 195 nx (k) := ox (j) - w96 ny (k) := oy (j) + 2 * w97 k := k + 198 nx (k) := ox (j) + 099 ny (k) := oy (j) + 2 * w100 k := k + 1101 nx (k) := ox (j + 1)102 ny (k) := oy (j + 1)103 end if104 end for105 size := k106 for m : 1 .. size107 ox (m) := nx (m)108 oy (m) := ny (m)109 end for110end for111 112113for m : 1 .. size - 1114 drawline (ox (m) * 5, oy (m) * 5, ox (m + 1) * 5, oy (m + 1) * 5,115 black)116end for117 118 119120for i : 1 .. 81121 k := 1122 done := false123 loop124 exit when k = size or done125 if ox (k) = ox (k + 1) and oy (k) < oy (k + 1)126 and xcoor = ox (k) and (i >= oy (k) and i <= oy (k + 1)) then127 put i, " " ..128 done := true129 elsif ox (k) = ox (k + 1) and oy (k) > oy (k + 1)130 and xcoor = ox (k) and (i <= oy (k) and i >= oy (k + 1)) then131 put i, " " ..132 done := true133 elsif oy (k) = oy (k + 1) and ox (k) < ox (k + 1)134 and xcoor >= ox (k) and xcoor <= ox (k + 1) and i = oy (k)135 then136 put i, " " ..137 done := true138 elsif oy (k) = oy (k + 1) and ox (k) > ox (k + 1)139 and xcoor <= ox (k) and xcoor >= ox (k + 1) and i = oy (k)140 then141 put i, " " ..142 done := true143 end if144 k := k + 1145 end loop146end for147 148