Knight's tour: Difference between revisions

(→‎{{header|Python}}: (Excerpts from) 200x200 example shown.)
Line 219:
<lang Icon>procedure main(A)
B := KnightsTour(8) # Try a tour
(\DumpBoard)(B) # for debug, ignored if omitted
end
 
procedure KnightsTour(N) #: Warnsdorff’s algorithm
moves := []
visited := set()
 
B := Board(N) # create board
 
sq := ?B.cols || ?B.rows # initial random position
repeat {
put(moves,sq) # record move
insert(visited,sq) # mark visited
choices := B.movesto[sq] -- visited
sq := ?(choices ** \!B.accessibility) | break # least accessible
Line 281:
}
every write(hdr2|hdr1)
end</lang>
<lang Icon>procedure DumpBoard(B) #: dump the board structure for analysis
write("Board size=",B.N)
every put(K := [],key(B.movesto))
Line 314:
+-------------------------+
a b c d e f g h</pre>
 
 
=={{header|J}}==
Anonymous user