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' /*legal "rank" move for a knight.*/
Kf = '1 2 2 1 -1 -2 -2 -1' /* " "file" " " " " */
 
do i=1 for words(Kr) /*legal moves*/
Kr.i = word(Kr,i); Kf.i = word(Kf,i)
end /*i*/
@.1.1 =1 1 /*the knight's starting position.*/
if N==1 | move(2,1,1) then call show
else say "ThereNo knight's notour solution for" NxN'.'
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; nf=f+Kf.t
if @.nr.nf==0 then do; @.nr.nf=?
if ?==NN then return 1
if move(?+1,nr,nf) then return 1
@.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('┌'_"┐", '┬', "┼")
do r=N for N by -1; if r\==N then say ! '├'_"┤"; L=@.
do f=1 for n N; L=L'│'centre(@.r.f,3) /*handlepreserve the ranks and filessquareness. */
end L=L'│'center(@.r.f,3) /*trying to preserve squareness. f*/
say ! L'│' end /*fshow a rank of the chessboard. */
end say !/*r*/ L'│' /*show a80 rankcols ofcan theview chessboard.19x19 chessbrd*/
end /*r*/ /*80 cols can view 19x19 chessbrd*/
say ! translate('└'_"┘", '┴', "┼") /*show the last rank of the board*/
return</lang>
'''output'''
<pre style="overflow:scroll">