progs/knight1_sol.scala
changeset 50 9891c9fac37e
parent 45 8399976b77fe
child 85 fd3f8581ce85
equal deleted inserted replaced
49:fdc2c6fb7a24 50:9891c9fac37e
    25        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
    25        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
    26 
    26 
    27 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
    27 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
    28   moves(x).filter(is_legal(dim, path))
    28   moves(x).filter(is_legal(dim, path))
    29 
    29 
    30 legal_moves(8, Nil, (2,2))
    30 assert(legal_moves(8, Nil, (2,2)) == 
    31 legal_moves(8, Nil, (7,7))
    31   List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
    32 
    32 assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
       
    33 assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
       
    34   List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
       
    35 assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
    33 
    36 
    34 def count_tours(dim: Int, path: Path): Int = {
    37 def count_tours(dim: Int, path: Path): Int = {
    35   if (path.length == dim * dim) 1
    38   if (path.length == dim * dim) 1
    36   else 
    39   else 
    37     (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum
    40     (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum