Jump to content

Solve a Holy Knight's tour: Difference between revisions

m
→‎{{header|REXX}}: changed/added some comments and whitespace.
(Added a Perl section.)
m (→‎{{header|REXX}}: changed/added some comments and whitespace.)
Line 2,305:
blank=pos('//', space(arg(1), 0))\==0 /*see if the pennies are to be shown. */
parse arg ops '/' cent /*obtain the options and the pennies. */
parse var ops N sRank sFile . /*boardsize, starting position, pennys.*/
if N=='' | N=="," then N=8 /*no boardsize specified? Use default.*/
if sRank=='' | sRank=="," then sRank=N /*starting rank given? " " */
if sFile=='' | sFile=="," then sFile=1 /* " file " " " */
NN=N**2; NxN='a ' N"x"N ' chessboard' /*file [↓] [↓] r=rank */
@.=; do r=1 for N; do f=1 for N; @.r.f=.; end /*f*/; end /*r*/
/*[↑] create an empty NxN chessboard.*/
cent=space( translate( cent, , ',')) ) /*allow use of comma (,) for separater.*/
cents=0 /*number of pennies on the chessboard. */
do while cent\='' /* [↓] possibly place the pennies. */
Line 2,318:
if x='' then x=1 /*if number not specified, use 1 penny.*/
if cr='' then iterate /*support the "blanking" option. */
do cf=cf for x do cf=cf for x /*now, place X pennies on chessboard.*/
@.cr.cf='¢' @.cr.cf= '¢' /*mark chessboard position with a penny*/
end /*cf*/ end /*cf*/ /* [↑] places X pennies on chessboard.*/
end /*while*/ /* [↑] allows of the placing of X ¢s*/
/* [↓] traipse through the chessboard.*/
do r=1 for N; do f=1 for N; cents=cents + (@.r.f=='¢'); end /*f*/; end /*r*/
/* [↑] count the number of pennies. */
if cents\==0 then say cents 'pennies placed on chessboard.'
target=NN -cents cents /*use this as the number of moves left.*/
beg='-1-' Kr = '2 1 -1 -2 -2 -1 1 2' /*[↑]the legal create"rank" the moves NxNfor a chessboardknight. */
Kr Kf = '1 2 2 1 -1 -2 -2 -1' 1/* " 2' " "file" " /*the legal "rank " moves for a" knight.*/
Kf kr.M=words(Kr) '1 2 2 1 -1 -2 -2 -1' /* " " "file" " /*number of " " possible "moves for a Knight*/
parse var Kr Kr.1 Kr.2 Kr.3 Kr.4 Kr.5 Kr.6 Kr.7 Kr.8 /*parse the legal moves by hand.*/
parse var Kf Kf.1 Kf.2 Kf.3 Kf.4 Kf.5 Kf.6 Kf.7 Kf.8 /* " " " " " " */
beg= '-1-' /* [↑] create the NxN chessboard. */
if @.sRank.sFile ==. then @.sRank.sFile=beg /*the knight's starting position. */
 
if @.sRank.sFile\==beg then do sRank=1 for N /*find starting rank for the knight.*/
do sFile=1 for N /* " " file " " " */
Line 2,339:
@.sRank.sFile=beg /*the knight's starting position. */
leave sRank /*we have a spot, so leave all this.*/
end /*sRanksFile*/
end /*sFilesRank*/
@hkt= "holy knight's tour" /*a handy-dandyhandy─dandy literal for the SAYs. */
if \move(2,sRank,sFile) & \(N==1) then say 'No' @hkt "solution for" NxN'.'
else say 'A solution for the' @hkt "on" NxN':'
 
/*show chessboard with moves &and penniescoins.*/
!=left('', 9 * (n<18) ); say /*used for indentation of chessboard. */
_=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; ?=@.r.f; if ?==target then ?='end'; L=L'│'center(?,3) /*"end"?*/
end /*f*/
if blank then L=translate(L,,'¢') /*blank out the pennies on chessboard ?*/
Line 2,358:
/*──────────────────────────────────────────────────────────────────────────────────────*/
move: procedure expose @. Kr. Kf. target; parse arg #,rank,file /*obtain move,rank,file.*/
do t=1 for 8Kr.M; nr=rank+Kr.t; nf=file+Kf.t /*position of the knight*/
if @.nr.nf==. then do; @.nr.nf=# /*Empty? Knight can move*/
if #==target then return 1 /*is this the last move?*/
if move(#+1,nr,nf) then return 1 /* " " " " " */
@.nr.nf=. /*undo the above move. */
end /*try a different move. */
end /*t*/ /* [↑] all moves tried.*/
return 0 return 0 /*tour is notisn't possible. */</lang>
'''output''' &nbsp; when the following is used for input:
<br><tt> , 3 1 /1,1 3 /1,7 2 /2,1 2 /2,5 /2,7 2 /3,8 /4,2 /4,4 2 /5,4 2 /5,7 /6,1 /7,1 /7,3 /7,6 3 /8,1 /8,5 4 </tt>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.