- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- Move the left boundary only as needed while maintaining the invariant and updating the answer.
Code notes
- 206 lines of Turing from the credited upstream file ccc98s3.t.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 456 78910 111213 141516 1718 19var infile : string := "rover.in"20var outfile : string := "rover.out"21var fi, fo : int22var n, i, d : int23var dir, x, y : int24 25 26procedure right27 put : fo, "2"28end right29 30procedure left31 put : fo, "3"32end left33 34procedure ahead (w : int)35 put : fo, "1"36 put : fo, abs (w)37end ahead38 39 40 41open : fi, infile, get42open : fo, outfile, put43 44get : fi, n45for k : 1 .. n46 dir := 047 x := 048 y := 049 loop50 get : fi, i51 exit when i = 052 if i = 1 then53 get : fi, d54 if dir = 0 then55 x := x + d56 elsif dir = 90 then57 y := y + d58 elsif dir = 180 then59 x := x - d60 else61 y := y - d62 end if63 elsif i = 2 then64 dir := dir - 9065 if dir < 0 then66 dir := 27067 end if68 else69 dir := dir + 9070 if dir = 360 then71 dir := 072 end if73 end if74 end loop75 76 put : fo, "Distance is ", abs (x) + abs (y)77 78 79 if y > 0 and x > 0 and dir = 0 then80 right81 ahead (y)82 right83 ahead (x)84 elsif y > 0 and x > 0 and dir = 90 then85 left86 ahead (x)87 left88 ahead (y)89 elsif y > 0 and x > 0 and dir = 180 then90 ahead (x)91 left92 ahead (y)93 elsif y > 0 and x > 0 and dir = 270 then94 ahead (y)95 right96 ahead (x)97 elsif y > 0 and x < 0 and dir = 0 then98 ahead (x)99 right100 ahead (y)101 elsif y > 0 and x < 0 and dir = 90 then102 right103 ahead (x)104 right105 ahead (y)106 elsif y > 0 and x < 0 and dir = 180 then107 left108 ahead (y)109 left110 ahead (x)111 elsif y > 0 and x < 0 and dir = 270 then112 ahead (y)113 left114 ahead (x)115 elsif y < 0 and x < 0 and dir = 0 then116 ahead (x)117 left118 ahead (y)119 elsif y < 0 and x < 0 and dir = 90 then120 ahead (y)121 right122 ahead (x)123 elsif y < 0 and x < 0 and dir = 180 then124 right125 ahead (y)126 right127 ahead (x)128 elsif y < 0 and x < 0 and dir = 270 then129 left130 ahead (x)131 left132 ahead (y)133 elsif y < 0 and x > 0 and dir = 0 then134 left135 ahead (y)136 left137 ahead (x)138 elsif y < 0 and x > 0 and dir = 90 then139 ahead (y)140 left141 ahead (x)142 elsif y < 0 and x > 0 and dir = 180 then143 ahead (x)144 right145 ahead (y)146 elsif y < 0 and x > 0 and dir = 270 then147 right148 ahead (x)149 right150 ahead (y)151 elsif y = 0 and x > 0 and dir = 0 then152 right153 right154 ahead (x)155 elsif y = 0 and x > 0 and dir = 90 then156 left157 ahead (x)158 elsif y = 0 and x > 0 and dir = 180 then159 ahead (x)160 elsif y = 0 and x > 0 and dir = 270 then161 right162 ahead (x)163 elsif y > 0 and x = 0 and dir = 0 then164 right165 ahead (y)166 elsif y > 0 and x = 0 and dir = 90 then167 left168 left169 ahead (y)170 elsif y > 0 and x = 0 and dir = 180 then171 left172 ahead (y)173 elsif y > 0 and x = 0 and dir = 270 then174 ahead (y)175 elsif y = 0 and x < 0 and dir = 0 then176 ahead (x)177 elsif y = 0 and x < 0 and dir = 90 then178 right179 ahead (x)180 elsif y = 0 and x < 0 and dir = 180 then181 left182 left183 ahead (x)184 elsif y = 0 and x < 0 and dir = 270 then185 left186 ahead (x)187 elsif y < 0 and x = 0 and dir = 0 then188 left189 ahead (y)190 elsif y < 0 and x = 0 and dir = 90 then191 ahead (y)192 elsif y < 0 and x = 0 and dir = 180 then193 right194 ahead (y)195 elsif y > 0 and x = 0 and dir = 270 then196 left197 left198 ahead (y)199 end if200 put : fo, ""201end for202 203close : fi204close : fo205 206