Anonymous user
Knight's tour: Difference between revisions
m
→{{header|REXX}}: changed the orientation of the presentation of the chessboard. -- ~~~~
(→{{header|REXX}}: added the REXX language. -- ~~~~) |
m (→{{header|REXX}}: changed the orientation of the presentation of the chessboard. -- ~~~~) |
||
Line 2,690:
=={{header|REXX}}==
This REXX version is modeled after the XPL0 example.
<lang rexx>/*REXX pgm to solve the knight's tour problem for a NxN chessboard. */
@.= /*define the outside of the board*/
Line 2,697:
do r=1 for N; do f=1 for N; @.r.f=0; end /*f*/; end /*r*/
/*[↑] zero out the NxN chessboard*/
Kr = '2 1 -1 -2 -2 -1 1 2'
Kf = '1 2 2 1 -1 -2 -2 -1'
do i=1 for words(Kr) /*legal moves*/
Kr.i = word(Kr,i); Kf.i = word(Kf,i)
end /*i*/
@.1.1 =
if N==1 | move(2,1,1) then call show
else say "
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────MOVE subroutine─────────────────────*/
move: procedure expose @. Kr. Kf. N NN; parse arg ?,r,f
do t=1 for N; nr=r+Kr.t;
if @.nr.nf==0 then do;
if ?==NN
if move(?+1,nr,nf)
@.nr.nf=0
end
end /*t*/
return 0
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: say "A solution for the knight's tour on" NxN':';!=left('',9*(n<18))
_=substr(copies("┼───",N),2); say; say ! translate('┌'_"┐", '┬', "┼")
end
say ! L'│'
end
say ! translate('└'_"┘", '┴', "┼") /*show the last rank of the board*/
'''output'''
<pre style="overflow:scroll">
|