Solve a Holy Knight's tour

From Rosetta Code
Task
Solve a Holy Knight's tour
You are encouraged to solve this task according to the task description, using any language you may know.

Night's tours are similar to Hidato. When learning to play chess coaches torture (instruct) their charges by taking a chess board, placing some pennies on some squares and requiring that a Knight's tour is constructed which avoids squares with a penny on. The purpose of this task is to produce a solution to such problems. At least demonstrate you program by solving the following:

Example 1
  0 0 0 
  0   0 0 
  0 0 0 0 0 0 0
0 0 0     0   0
0   0     0 0 0
1 0 0 0 0 0 0
    0 0   0
      0 0 0

Extra credit is available for other interesting examples.

Ada

This solution uses the package Knights_Tour from Knight's Tour#Ada. The board is quadratic, the size of the board is read from the command line and the board itself is read from the standard input. For the board itself, characters ' ' and '-' indicate a no-go (or a coin on the board), all other characters represent places the knight must visit. A '1' represents the start point. Ill-formatted input will crash the program.

<lang Ada>with Knights_Tour, Ada.Text_IO, Ada.Command_Line;

procedure Holy_Knight is

  Size: Positive := Positive'Value(Ada.Command_Line.Argument(1));
  package KT is new Knights_Tour(Size => Size);
  Board: KT.Tour := (others => (others => Natural'Last));
  
  Start_X, Start_Y: KT.Index:= 1; -- default start place (1,1)
  S: String(KT.Index);
  I: Positive := KT.Index'First;

begin

  while not Ada.Text_IO.End_Of_File and I <= Size loop
     S := Ada.Text_IO.Get_Line; 
     for J in KT.Index loop
        if S(J) = ' ' or S(J) = '-' then
           Board(I,J) := Natural'Last;
        elsif S(J) = '1' then 
             Start_X := I; Start_Y := J;  Board(I,J) := 1;
        else Board(I,J) := 0;
        end if;
     end loop;
     I := I + 1;
  end loop;
  Ada.Text_IO.Put_Line("Start Configuration (Length:" 
                         & Natural'Image(KT.Count_Moves(Board)) & "):");
  KT.Tour_IO(Board, Width => 1);
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put_Line("Tour:");
  KT.Tour_IO(KT.Warnsdorff_Get_Tour(Start_X, Start_Y, Board));

end Holy_Knight;</lang>

Output:
>holy_knight 8 < standard_problem.txt

Start Configuration (Length: 36):
--000---
--0-00--
-0000000
000--0-0
0-0--000
1000000-
--00-0--
---000--

Tour:
   -   -  30  15  20   -   -   -
   -   -  21   -  29  16   -   -
   -  33  14  31  22  19   6  17
  13  36  23   -   -  28   -   8
  34   -  32   -   -   7  18   5
   1  12  35  24  27   4   9   -
   -   -   2  11   -  25   -   -
   -   -   -  26   3  10   -   -


Extra Credit

The program has been used to tackle some of the problems from New Knight's Tour Puzzles and Graphs. Here is one solution:

>holy_knight 13 < problem10.txt
Start Configuration (Length: 56):
-----1-0-----
-----0-0-----
----00000----
-----000-----
--0--0-0--0--
00000---00000
--00-----00--
00000---00000
--0--0-0--0--
-----000-----
----00000----
-----0-0-----
-----0-0-----

Tour:
   -   -   -   -   -   1   -  27   -   -   -   -   -
   -   -   -   -   -  56   -   2   -   -   -   -   -
   -   -   -   -  24   3  28  55  26   -   -   -   -
   -   -   -   -   -  54  25   4   -   -   -   -   -
   -   -  50   -   -  23   -  29   -   -   6   -   -
  51  20  47  22  53   -   -   -   5  30   9  32   7
   -   -  52  49   -   -   -   -   -  33  36   -   -
  19  48  21  46  17   -   -   -  37  10  31   8  35
   -   -  18   -   -  45   -  11   -   -  34   -   -
   -   -   -   -   -  16  41  38   -   -   -   -   -
   -   -   -   -  42  39  44  15  12   -   -   -   -
   -   -   -   -   -  14   -  40   -   -   -   -   -
   -   -   -   -   -  43   -  13   -   -   -   -   -

Icon and Unicon

This is a Unicon-specific solution: <lang unicon>global nCells, cMap, best record Pos(r,c)

procedure main(A)

   puzzle := showPuzzle("Input",readPuzzle())
   QMouse(puzzle,findStart(puzzle),&null,0)
   showPuzzle("Output", solvePuzzle(puzzle)) | write("No solution!")

end

procedure readPuzzle()

   # Start with a reduced puzzle space
   p := [[-1],[-1]]
   nCells := maxCols := 0
   every line := !&input do {
       put(p,[: -1 | -1 | gencells(line) | -1 | -1 :])
       maxCols <:= *p[-1]
       }
   every put(p, [-1]|[-1])
   # Now normalize all rows to the same length
   every i := 1 to *p do p[i] := [: !p[i] | (|-1\(maxCols - *p[i])) :]
   return p

end

procedure gencells(s)

   static WS, NWS
   initial {
       NWS := ~(WS := " \t")
       cMap := table()     # Map to/from internal model
       cMap["#"] := -1;  cMap["_"] :=  0
       cMap[-1]  := " "; cMap[0]   := "_"
       }
   s ? while not pos(0) do {
           w := (tab(many(WS))|"", tab(many(NWS))) | break
           w := numeric(\cMap[w]|w)
           if -1 ~= w then nCells +:= 1
           suspend w
           }

end

procedure showPuzzle(label, p)

   write(label," with ",nCells," cells:")
   every r := !p do {
       every c := !r do writes(right((\cMap[c]|c),*nCells+1))
       write()
       }
   return p

end

procedure findStart(p)

   if \p[r := !*p][c := !*p[r]] = 1 then return Pos(r,c)

end

procedure solvePuzzle(puzzle)

   if path := \best then {
       repeat {
           loc := path.getLoc()
           puzzle[loc.r][loc.c] := path.getVal()
           path := \path.getParent() | break
           }
       return puzzle
       }

end

class QMouse(puzzle, loc, parent, val)

   method getVal(); return val; end
   method getLoc(); return loc; end
   method getParent(); return parent; end
   method atEnd(); return nCells = val; end
   method visit(r,c)
       if /best & validPos(r,c) then return Pos(r,c)
   end
   method validPos(r,c)
       v := val+1
       xv := (0 <= puzzle[r][c]) | fail
       if xv = (v|0) then {  # make sure this path hasn't already gone there
           ancestor := self
           while xl := (ancestor := \ancestor.getParent()).getLoc() do
               if (xl.r = r) & (xl.c = c) then fail
           return
           }
   end

initially

   val := val+1
   if atEnd() then return best := self
   QMouse(puzzle, visit(loc.r-2,loc.c-1), self, val)
   QMouse(puzzle, visit(loc.r-2,loc.c+1), self, val)
   QMouse(puzzle, visit(loc.r-1,loc.c+2), self, val)
   QMouse(puzzle, visit(loc.r+1,loc.c+2), self, val)
   QMouse(puzzle, visit(loc.r+2,loc.c+1), self, val)
   QMouse(puzzle, visit(loc.r+2,loc.c-1), self, val)
   QMouse(puzzle, visit(loc.r+1,loc.c-2), self, val)
   QMouse(puzzle, visit(loc.r-1,loc.c-2), self, val)

end</lang>

Sample run:

->hkt <hkt.in
Input with 36 cells:
                                    
                                    
           _  _  _                  
           _     _  _               
           _  _  _  _  _  _  _      
        _  _  _        _     _      
        _     _        _  _  _      
        1  _  _  _  _  _  _         
              _  _     _            
                 _  _  _            
                                    
                                    
Output with 36 cells:
                                    
                                    
          19  4 13                  
          12    18  5               
          25 20  3 14 17  6 31      
       21  2 11       32    16      
       26    24       15 30  7      
        1 22 27 10 35  8 33         
             36 23    29            
                28  9 34            
                                    
                                    
->

Perl 6

Using the Warnsdorff algorithm from Solve_a_Hidato_puzzle. <lang perl6>my @adjacent =

              [ -2, -1],  [ -2, 1],
     [-1,-2],                       [-1,+2],
     [+1,-2],                       [+1,+2],
              [ +2, -1],  [ +2, 1];

solveboard q:to/END/;

   . 0 0 0
   . 0 . 0 0
   . 0 0 0 0 0 0 0
   0 0 0 . . 0 . 0
   0 . 0 . . 0 0 0
   1 0 0 0 0 0 0
   . . 0 0 . 0
   . . . 0 0 0
   END</lang>
Output:
   25 14 27
   36    24 15
   31 26 13 28 23  6 17
35 12 29       16    22
30    32        7 18  5
 1 34 11  8 19  4 21
       2 33     9
         10  3 20
84 tries

REXX

This REXX program is essentially a modified   knight's tour   REXX program with support to place pennies on the chessboard.
Also supported is the specification of the size of the chessboard and the placement of the knight (initial position). <lang rexx>/*REXX pgm solves the holy knight's tour problem for a NxN chessboard.*/ blank=pos('//',space(arg(1),0))\==0 /*see if pennies are to be shown.*/ parse arg ops '/' cent /*obtain the options and pennies.*/ parse var ops N sRank sFile . /*boardsize, starting pos, pennys*/ if N== | N==',' then N=8 /*Boardsize specified? Default. */ if sRank== then sRank=N /*starting rank given? Default. */ if sFile== then sFile=1 /* " file " " */ NN=N**2; NxN='a ' N"x"N ' chessboard' /* [↓] r=Rank, f=File.*/ @.=; do r=1 for N; do f=1 for N; @.r.f=' '; end /*f*/; end /*r*/

                                      /*[↑]  blank the  NxN chessboard.*/

cent=space(translate(cent,,',')) /*allow use of comma (,) for sep.*/ cents=0 /*number of pennies on chessboard*/

      do  while  cent\=             /* [↓]  possibly place pennies.  */
      parse var cent cr cf x '/' cent /*extract where to place pennies.*/
      if x=   then x=1              /*if # not specified, use 1 penny*/
      if cr=  then iterate          /*support the "blanking" option. */
        do cf=cf for x                /*now, place  X  pennies on board*/
        @.cr.cf='¢'                   /*mark board position with penny.*/
        end   /*cf*/                  /* [↑]  places X pennies on board*/
      end     /*while cent¬= */     /* [↑]  allows of placing  X  ¢s.*/
                                      /* [↓]  traipse through the board*/
 do r=1  for N;  do f=1  for N;   cents=cents+(@.r.f=='¢');   end;   end
                                      /* [↑]  count number of pennies. */

if cents\==0 then say cents 'pennies placed on chessboard.' target=NN-cents /*use this as the number of moves*/ 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 knight moves*/
                            Kr.i = word(Kr,i);     Kf.i = word(Kf,i)
                            end    /*i*/           /*for fast indexing.*/

!=left(, 9*(n<18)) /*used for indentation of board. */ if @.sRank.sFile==' ' then @.sRank.sFile=1 /*knight's starting pos*/ if @.sRank.sFile\==1 then do sRank=1 for N /*find a starting rank.*/

                            do sFile=1  for N   /*  "  "    "     file.*/
                            if @.sRank.sFile==' ' then do  /*got a spot*/
                                                       @.sRank.sFile=1
                                                       leave sRank
                                                       end
                            end   /*sRank*/
                          end     /*sFile*/

if \move(2,sRank,sFile) & ,

  \(N==1)  then say "No holy knight's tour solution for" NxN'.'
           else say "A solution for the holy knight's tour on" NxN':'

_=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;     L=L'│'centre(@.r.f,3)   /*preserve squareness.*/
      end      /*f*/
    if blank then L=translate(L,,'¢') /*blank out the pennies ?        */
    say ! L'│'                        /*show a  rank of the chessboard.*/
    end        /*r*/                  /*80 cols can view 19x19 chessbrd*/

say  ! translate('└'_"┘", '┴', "┼") /*show the last rank of the board*/ exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────MOVE subroutine─────────────────────*/ move: procedure expose @. Kr. Kf. N target; parse arg #,rank,file; b=' '

        do t=1  for 8;      nr=rank+Kr.t;      nf=file+Kf.t
        if @.nr.nf==b  then do;                @.nr.nf=#     /*Kn move.*/
                            if #==target       then return 1 /*last mv?*/
                            if move(#+1,nr,nf) then return 1
                            @.nr.nf=b          /*undo the above move.  */
                            end                /*try different move.   */
        end   /*t*/

return 0 /*the tour not possible.*/</lang> output when the following is used for input:
, 3 1 /1,1 3 /1,7 2 /2,1 2 /2,5 /2,8 /3,8 /4,2 /4,4 2 /5,4 2 /5,6 /6,1 /7,1 2 /7,4 /7,7 1 /8,1 2 /8,6 3

26 pennies placed on chessboard.
A solution for the knight's tour on a  8x8  chessboard:

          ┌───┬───┬───┬───┬───┬───┬───┬───┐
          │ ¢ │ ¢ │26 │35 │ 4 │ ¢ │ ¢ │ ¢ │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │ ¢ │ ¢ │ 3 │ ¢ │25 │16 │ ¢ │ 6 │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │ ¢ │27 │36 │17 │34 │ 5 │24 │15 │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │37 │ 2 │33 │ ¢ │ ¢ │ ¢ │ 7 │22 │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │28 │ ¢ │18 │ ¢ │ ¢ │23 │14 │ 9 │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │ 1 │38 │29 │32 │13 │ 8 │21 │ ¢ │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │ ¢ │ ¢ │12 │19 │ ¢ │31 │10 │ ¢ │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │ ¢ │ ¢ │ ¢ │30 │11 │20 │ ¢ │ ¢ │
          └───┴───┴───┴───┴───┴───┴───┴───┘

output when the following is used for input:
, 3 1 /1,1 3 /1,7 2 /2,1 2 /2,5 /2,8 /3,8 /4,2 /4,4 2 /5,4 2 /5,6 /6,1 /7,1 2 /7,4 /7,7 1 /8,1 2 /8,6 3 //

26 pennies placed on chessboard.
A solution for the knight's tour on a  8x8  chessboard:

          ┌───┬───┬───┬───┬───┬───┬───┬───┐
          │   │   │26 │35 │ 4 │   │   │   │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │   │   │ 3 │   │25 │16 │   │ 6 │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │   │27 │36 │17 │34 │ 5 │24 │15 │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │37 │ 2 │33 │   │   │   │ 7 │22 │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │28 │   │18 │   │   │23 │14 │ 9 │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │ 1 │38 │29 │32 │13 │ 8 │21 │   │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │   │   │12 │19 │   │31 │10 │   │
          ├───┼───┼───┼───┼───┼───┼───┼───┤
          │   │   │   │30 │11 │20 │   │   │
          └───┴───┴───┴───┴───┴───┴───┴───┘

Ruby

This solution uses HLPsolver from here <lang ruby> require 'HLPsolver'

ADJACENT = [[-1,-2],[-2,-1],[-2,1],[-1,2],[1,2],[2,1],[2,-1],[1,-2]]

boardy = <<EOS , . . 0 0 0 , . . 0 . 0 0 , . 0 0 0 0 0 0 0 . , 0 0 0 . . 0 . 0 , 0 . 0 . . 0 0 0 , 1 0 0 0 0 0 0 , . . 0 0 . 0 , . . . 0 0 0

EOS t0 = Time.now HLPsolver.new(boardy).solve puts " #{Time.now - t0} sec" </lang>

Which produces:

Problem:
           0  0  0            
           0     0  0         
        0  0  0  0  0  0  0   
     0  0  0        0     0   
     0     0        0  0  0   
     1  0  0  0  0  0  0      
           0  0     0         
              0  0  0         

Solution:
           8 33 14            
          13     7 32         
        9 34 31 22 15  6 29   
    35 12 21       30    16   
    10    36       23 28  5   
     1 20 11 24 27  4 17      
           2 19    25         
             26  3 18         

 0.008917049 sec