| 
     1 // Part 3 about finding a single tour using the Warnsdorf Rule  | 
         | 
     2 //=============================================================  | 
         | 
     3   | 
         | 
     4 object CW7c { | 
         | 
     5   | 
         | 
     6 type Pos = (Int, Int)  | 
         | 
     7 type Path = List[Pos]  | 
         | 
     8   | 
         | 
     9 def print_board(dim: Int, path: Path): Unit = { | 
         | 
    10   println  | 
         | 
    11   for (i <- 0 until dim) { | 
         | 
    12     for (j <- 0 until dim) { | 
         | 
    13       print(f"${path.reverse.indexOf((i, j))}%3.0f ") | 
         | 
    14     }  | 
         | 
    15     println  | 
         | 
    16   }   | 
         | 
    17 }  | 
         | 
    18   | 
         | 
    19 def add_pair(x: Pos)(y: Pos): Pos =   | 
         | 
    20   (x._1 + y._1, x._2 + y._2)  | 
         | 
    21   | 
         | 
    22 def is_legal(dim: Int, path: Path)(x: Pos): Boolean =   | 
         | 
    23   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)  | 
         | 
    24   | 
         | 
    25 def moves(x: Pos): List[Pos] =   | 
         | 
    26   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),  | 
         | 
    27        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))  | 
         | 
    28   | 
         | 
    29 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] =   | 
         | 
    30   moves(x).filter(is_legal(dim, path))  | 
         | 
    31   | 
         | 
    32 def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] =   | 
         | 
    33   legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)  | 
         | 
    34   | 
         | 
    35   | 
         | 
    36 import scala.annotation.tailrec  | 
         | 
    37   | 
         | 
    38 @tailrec  | 
         | 
    39 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { | 
         | 
    40   case Nil => None  | 
         | 
    41   case x::xs => { | 
         | 
    42     val result = f(x)  | 
         | 
    43     if (result.isDefined) result else first(xs, f)  | 
         | 
    44   }  | 
         | 
    45 }  | 
         | 
    46   | 
         | 
    47   | 
         | 
    48 def first_closed_tour_heuristic(dim: Int, path: Path): Option[Path] = { | 
         | 
    49   if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)  | 
         | 
    50   else  | 
         | 
    51     first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristic(dim, x::path))  | 
         | 
    52 }  | 
         | 
    53   | 
         | 
    54 /*  | 
         | 
    55 for (dim <- 1 to 6) { | 
         | 
    56   val t = first_closed_tour_heuristic(dim, List((dim / 2, dim / 2)))  | 
         | 
    57   println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) | 
         | 
    58 }*/  | 
         | 
    59   | 
         | 
    60   | 
         | 
    61 def first_tour_heuristic(dim: Int, path: Path): Option[Path] = { | 
         | 
    62   | 
         | 
    63   @tailrec  | 
         | 
    64   def aux(dim: Int, path: Path, moves: List[Pos]): Option[Path] =    | 
         | 
    65   if (path.length == dim * dim) Some(path)  | 
         | 
    66   else  | 
         | 
    67     moves match { | 
         | 
    68       case Nil => None  | 
         | 
    69       case x::xs => { | 
         | 
    70         val r = first_tour_heuristic(dim, x::path)  | 
         | 
    71         if (r.isDefined) r else aux(dim, path, xs)  | 
         | 
    72     }  | 
         | 
    73   }      | 
         | 
    74    | 
         | 
    75   aux(dim, path, ordered_moves(dim, path, path.head))   | 
         | 
    76 }  | 
         | 
    77   | 
         | 
    78 /*  | 
         | 
    79 def first_tour_heuristic(dim: Int, path: Path): Option[Path] = { | 
         | 
    80   if (path.length == dim * dim) Some(path)  | 
         | 
    81   else  | 
         | 
    82     first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristic(dim, x::path))  | 
         | 
    83 }  | 
         | 
    84 */  | 
         | 
    85   | 
         | 
    86 /*  | 
         | 
    87 for (dim <- 1 to 50) { | 
         | 
    88   val t = first_tour_heuristic(dim, List((dim / 2, dim / 2)))  | 
         | 
    89   println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) | 
         | 
    90 }  | 
         | 
    91 */  | 
         | 
    92   | 
         | 
    93 }  | 
         | 
    94   | 
         | 
    95   | 
         | 
    96 //CW7c.first_tour_heuristic(50, List((0,0))).get  |