progs/lecture2.scala
changeset 366 1c829680503e
parent 365 fc118ee0fce4
child 367 e6ae724255a8
equal deleted inserted replaced
365:fc118ee0fce4 366:1c829680503e
   590 
   590 
   591 
   591 
   592 def moves(xs: List[Int], n: Int) : List[List[Int]] = 
   592 def moves(xs: List[Int], n: Int) : List[List[Int]] = 
   593  (xs, n) match {
   593  (xs, n) match {
   594    case (Nil, _) => Nil
   594    case (Nil, _) => Nil
   595    case (xs, 0) => Nil
   595    case (_, 0) => Nil
   596    case (x::xs, n) => (x::xs) :: moves(xs, n - 1)
   596    case (x::xs, n) => (x::xs) :: moves(xs, n - 1)
   597  }
   597  }
   598 
   598 
   599 
   599 // List(5,5,1,0) -> moves(List(5,1,0), 5)
   600 moves(List(5,1,0), 1)
   600 moves(List(5,1,0), 1)
   601 moves(List(5,1,0), 2)
   601 moves(List(5,1,0), 2)
   602 moves(List(5,1,0), 5)
   602 moves(List(5,1,0), 5)
   603 
   603 
   604 // checks whether a jump tour exists at all
   604 // checks whether a jump tour exists at all
   605 
   605 
   606 def search(xs: List[Int]) : Boolean = xs match {
   606 def search(xs: List[Int]) : Boolean = xs match {
   607   case Nil => true
   607   case Nil => true
   608   case (x::xs) =>
   608   case x::xs =>
   609     if (xs.length < x) true else moves(xs, x).exists(search(_))
   609     if (xs.length < x) true 
       
   610     else moves(xs, x).exists(search(_))
   610 }
   611 }
   611 
   612 
   612 
   613 
   613 search(List(5,3,2,5,1,1))
   614 search(List(5,3,2,5,1,1))
   614 search(List(3,5,1,0,0,0,1))
   615 search(List(3,5,1,0,0,0,1))
   619 search(Nil)
   620 search(Nil)
   620 search(List(1))
   621 search(List(1))
   621 search(List(5,1,1))
   622 search(List(5,1,1))
   622 search(List(3,5,1,0,0,0,0,0,0,0,0,1))
   623 search(List(3,5,1,0,0,0,0,0,0,0,0,1))
   623 
   624 
       
   625 
       
   626 import scala.util._
       
   627 List.fill(100)(Random.nextInt(2))
       
   628 search(List.fill(100)(Random.nextInt(10)))
       
   629 
   624 // generate *all* jump tours
   630 // generate *all* jump tours
   625 //    if we are only interested in the shortes one, we could
   631 //    if we are only interested in the shortes one, we could
   626 //    shortcircut the calculation and only return List(x) in
   632 //    shortcircut the calculation and only return List(x) in
   627 //    case where xs.length < x, because no tour can be shorter
   633 //    case where xs.length < x, because no tour can be shorter
   628 //    than 1
   634 //    than 1
   629 // 
   635 // 
   630 
   636 
   631 def jumps(xs: List[Int]) : List[List[Int]] = xs match {
   637 def jumps(xs: List[Int]) : List[List[Int]] = xs match {
   632   case Nil => Nil
   638   case Nil => Nil
   633   case (x::xs) => {
   639   case x::xs => {
   634     val children = moves(xs, x)
   640     val children = moves(xs, x)
   635     val results = children.map(cs => jumps(cs).map(x :: _)).flatten
   641     val results = 
       
   642       children.map(cs => jumps(cs).map(x :: _)).flatten
   636     if (xs.length < x) List(x) :: results else results
   643     if (xs.length < x) List(x) :: results else results
   637   }
   644   }
   638 }
   645 }
   639 
   646 
   640 jumps(List(3,5,1,2,1,2,1))
   647 jumps(List(3,5,1,2,1,2,1))