testing2/knight3.scala
changeset 161 6ea450e999e2
parent 160 863feeb5c760
equal deleted inserted replaced
160:863feeb5c760 161:6ea450e999e2
     1 import scala.annotation.tailrec
     1 // Part 3 about finding a single tour using the Warnsdorf Rule
       
     2 //=============================================================
       
     3 
     2 object CW7c {
     4 object CW7c {
     3 type Pos = (Int, Int)    // a position on a chessboard 
       
     4 type Path = List[Pos]    // a path...a list of positions
       
     5 
     5 
     6 def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = {
     6 type Pos = (Int, Int)
     7     if((x._1 >= 0) && (x._2 >= 0) && (x._1 < dim) && (x._2 < dim)){
     7 type Path = List[Pos]
     8        !(path.contains(x))
       
     9     } else false
       
    10  }
       
    11 
     8 
    12 def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
     9 def print_board(dim: Int, path: Path): Unit = {
    13   val lst = List( (1,2),(2,1),(2,-1),(1,-2), (-1,-2),(-2,-1),(-2,1),(-1,2) )
    10   println
    14   val mapping = lst.map(s => ( s._1 + x._1, s._2 + x._2) )   
    11   for (i <- 0 until dim) {
    15        for( i <- mapping if ( is_legal(dim,path)(i) )) yield i     
    12     for (j <- 0 until dim) {
    16   }
    13       print(f"${path.reverse.indexOf((i, j))}%3.0f ")
    17 
    14     }
    18 def ordered_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
    15     println
    19 legal_moves(dim,path,x).sortBy(legal_moves(dim,path,_).length  )
    16   } 
    20 }
    17 }
    21 
    18 
    22 def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] ={
    19 def add_pair(x: Pos)(y: Pos): Pos = 
    23   if(xs.isEmpty)
    20   (x._1 + y._1, x._2 + y._2)
    24   None 
    21 
    25   else {
    22 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
    26             val b = f(xs.head)
    23   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
    27             if (b!=None)
    24 
    28             b
    25 def moves(x: Pos): List[Pos] = 
    29           else
    26   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
    30              first(xs.tail,f)
    27        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
    31        }
    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)
    32   }
    44   }
    33 
       
    34 def first_closed_tour_heuristic(dim: Int, path: Path) : Option[Path] = {
       
    35   if (dim < 5) None 
       
    36     else 
       
    37         if(path.length==dim*dim) Some(path)
       
    38        else 
       
    39       first(ordered_moves(dim,path,path.head),y => first_closed_tour_heuristic(dim, y::path))
       
    40   }
       
    41   
       
    42 }
    45 }
    43 
    46 
    44 
    47 
    45 first_closed_tour_heuristic(6, List((3, 3)))
    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