1 // Part 1 about finding and counting Knight's tours  | 
     1 // Part 1 about finding Knight's tours  | 
     2 //==================================================  | 
     2 //=====================================  | 
     3   | 
     3   | 
     4 type Pos = (Int, Int)    // a position on a chessboard   | 
     4 type Pos = (Int, Int)    // a position on a chessboard   | 
     5 type Path = List[Pos]    // a path...a list of positions  | 
     5 type Path = List[Pos]    // a path...a list of positions  | 
     6   | 
     6   | 
         | 
     7 // for measuring time  | 
         | 
     8 def time_needed[T](code: => T) : T = { | 
         | 
     9   val start = System.nanoTime()  | 
         | 
    10   val result = code  | 
         | 
    11   val end = System.nanoTime()  | 
         | 
    12   println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.") | 
         | 
    13   result  | 
         | 
    14 }  | 
         | 
    15   | 
         | 
    16 // for printing a board  | 
     7 def print_board(dim: Int, path: Path): Unit = { | 
    17 def print_board(dim: Int, path: Path): Unit = { | 
     8   println  | 
    18   println  | 
     9   for (i <- 0 until dim) { | 
    19   for (i <- 0 until dim) { | 
    10     for (j <- 0 until dim) { | 
    20     for (j <- 0 until dim) { | 
    11       print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ") | 
    21       print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ") | 
    18 // 1 mark  | 
    28 // 1 mark  | 
    19   | 
    29   | 
    20 def is_legal(dim: Int, path: Path, x: Pos): Boolean =   | 
    30 def is_legal(dim: Int, path: Path, x: Pos): Boolean =   | 
    21   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)  | 
    31   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)  | 
    22   | 
    32   | 
    23 assert(is_legal(8, Nil)((3,4)) == true)  | 
    33 assert(is_legal(8, Nil, (3, 4)) == true)  | 
    24 assert(is_legal(8, List((4,1), (1,0)))((4,1)) == false)  | 
    34 assert(is_legal(8, List((4, 1), (1, 0)), (4, 1)) == false)  | 
    25 assert(is_legal(2, Nil)((0,0)) == true)  | 
    35 assert(is_legal(2, Nil, (0, 0)) == true)  | 
    26   | 
    36   | 
    27   | 
    37   | 
    28 def add_pair(x: Pos)(y: Pos): Pos =   | 
    38 def add_pair(x: Pos, y: Pos): Pos =   | 
    29   (x._1 + y._1, x._2 + y._2)  | 
    39   (x._1 + y._1, x._2 + y._2)  | 
    30   | 
    40   | 
    31 def moves(x: Pos): List[Pos] =   | 
    41 def moves(x: Pos): List[Pos] =   | 
    32   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),  | 
    42   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),  | 
    33        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))  | 
    43        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x, _))  | 
    34   | 
    44   | 
    35 // 1 mark  | 
    45 // 1 mark  | 
    36   | 
    46   | 
    37 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] =   | 
    47 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] =   | 
    38   moves(x).filter(is_legal(dim, path))  | 
    48   moves(x).filter(is_legal(dim, path, _))  | 
    39   | 
    49   | 
    40 assert(legal_moves(8, Nil, (2,2)) ==   | 
    50 assert(legal_moves(8, Nil, (2,2)) ==   | 
    41   List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))  | 
    51   List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))  | 
    42 assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))  | 
    52 assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))  | 
    43 assert(legal_moves(8, List((4,1), (1,0)), (2,2)) ==   | 
    53 assert(legal_moves(8, List((4,1), (1,0)), (2,2)) ==   | 
    94     println(ts.head)  | 
   107     println(ts.head)  | 
    95   }  | 
   108   }  | 
    96 }  | 
   109 }  | 
    97   | 
   110   | 
    98   | 
   111   | 
         | 
   112 // 1 mark  | 
         | 
   113   | 
         | 
   114 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { | 
         | 
   115   case Nil => None  | 
         | 
   116   case x::xs => { | 
         | 
   117     val result = f(x)  | 
         | 
   118     if (result.isDefined) result else first(xs, f)  | 
         | 
   119   }  | 
         | 
   120 }  | 
         | 
   121   | 
         | 
   122 // test cases  | 
         | 
   123 def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None  | 
         | 
   124   | 
         | 
   125 first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)  | 
         | 
   126 first(List((1, 0),(2, 0),(3, 0)), foo)  | 
         | 
   127   | 
         | 
   128   | 
         | 
   129 // 1 mark  | 
         | 
   130   | 
         | 
   131 def first_tour(dim: Int, path: Path): Option[Path] = { | 
         | 
   132   if (path.length == dim * dim) Some(path)  | 
         | 
   133   else  | 
         | 
   134     first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))  | 
         | 
   135 }  | 
         | 
   136   | 
         | 
   137   | 
         | 
   138   | 
         | 
   139 /*  | 
         | 
   140 for (dim <- 1 to 8) { | 
         | 
   141   val t = first_tour(dim, List((0, 0)))  | 
         | 
   142   println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) | 
         | 
   143 }  | 
         | 
   144 */  | 
         | 
   145   | 
         | 
   146 // 15 secs for 8 x 8  | 
         | 
   147 val ts1 = time_needed(0,first_tour(8, List((0, 0))).get)  | 
         | 
   148   | 
         | 
   149 // no result for 4 x 4  | 
         | 
   150 val ts2 = time_needed(0, first_tour(4, List((0, 0))))  | 
         | 
   151   | 
         | 
   152 // 0.3 secs for 6 x 6  | 
         | 
   153 val ts3 = time_needed(0, first_tour(6, List((0, 0))))  | 
         | 
   154   | 
         | 
   155 // 15 secs for 8 x 8  | 
         | 
   156 time_needed(0, print_board(8, first_tour(8, List((0, 0))).get))  | 
         | 
   157   |