Approach
Breadth-first search
For CCC 2010 J5 - Knight Hop, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 117 lines of Turing from the credited upstream file ccc10j5.t.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123456 7class Point8 export var x, var y, setValue9 var x, y : int10 11 procedure setValue (a, b : int)12 x := a13 y := b14 end setValue15end Point16 17 18var board : array 1 .. 8, 1 .. 8 of int19 20var p : array 1 .. 64 of ^Point21var q : array 1 .. 64 of ^Point22var psize, qsize : int23 24var x, y : int25var sx, sy : int26var a, b, step : int27 2829get x, y, sx, sy30 3132new p (1)33p (1) -> setValue (x, y)34psize := 135 3637for i : 1 .. 838 for j : 1 .. 839 board (i, j) := 9999940 end for41end for42board (x, y) := 043 4445step := 146loop47 exit when psize = 048 49 50 qsize := 051 for i : 1 .. psize52 a := p (i) -> x53 b := p (i) -> y54 55 56 if a + 1 <= 8 and b + 2 <= 8 and step < board (a + 1, b + 2) then57 board (a + 1, b + 2) := step58 qsize := qsize + 159 new q (qsize)60 q (qsize) -> setValue (a + 1, b + 2)61 end if62 if a + 2 <= 8 and b + 1 <= 8 and step < board (a + 2, b + 1) then63 board (a + 2, b + 1) := step64 qsize := qsize + 165 new q (qsize)66 q (qsize) -> setValue (a + 2, b + 1)67 end if68 if a + 2 <= 8 and b - 1 >= 1 and step < board (a + 2, b - 1) then69 board (a + 2, b - 1) := step70 qsize := qsize + 171 new q (qsize)72 q (qsize) -> setValue (a + 2, b - 1)73 end if74 if a + 1 <= 8 and b - 2 >= 1 and step < board (a + 1, b - 2) then75 board (a + 1, b - 2) := step76 qsize := qsize + 177 new q (qsize)78 q (qsize) -> setValue (a + 1, b - 2)79 end if80 if a - 1 >= 1 and b - 2 >= 1 and step < board (a - 1, b - 2) then81 board (a - 1, b - 2) := step82 qsize := qsize + 183 new q (qsize)84 q (qsize) -> setValue (a - 1, b - 2)85 end if86 if a - 2 >= 1 and b - 1 >= 1 and step < board (a - 2, b - 1) then87 board (a - 2, b - 1) := step88 qsize := qsize + 189 new q (qsize)90 q (qsize) -> setValue (a - 2, b - 1)91 end if92 if a - 2 >= 1 and b + 1 <= 8 and step < board (a - 2, b + 1) then93 board (a - 2, b + 1) := step94 qsize := qsize + 195 new q (qsize)96 q (qsize) -> setValue (a - 2, b + 1)97 end if98 if a - 1 >= 1 and b + 2 <= 8 and step < board (a - 1, b + 2) then99 board (a - 1, b + 2) := step100 qsize := qsize + 1101 new q (qsize)102 q (qsize) -> setValue (a - 1, b + 2)103 end if104 end for105 106 107 108 step := step + 1109 psize := qsize110 for i : 1 .. psize111 p (i) := q (i)112 end for113end loop114 115116put board (sx, sy)117