progs/sudoku.scala
changeset 329 8a34b2ebc8cc
parent 325 ca9c1cf929fa
child 383 c02929f2647c
equal deleted inserted replaced
328:0e591f806290 329:8a34b2ebc8cc
    61       val cs = candidates(game, emptyPosition(game))
    61       val cs = candidates(game, emptyPosition(game))
    62       search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
    62       search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
    63     }
    63     }
    64   }
    64   }
    65 }
    65 }
       
    66 
       
    67 def pretty(game: String): String = 
       
    68   "\n" + (game.sliding(maxValue, maxValue).mkString(",\n"))
       
    69 
    66 
    70 
    67 // a list of hard games according to Andrew Coles and Peter Norvig
    71 // a list of hard games according to Andrew Coles and Peter Norvig
    68 
    72 
    69 val hard_games = 
    73 val hard_games = 
    70   List("52...6.........7.13...........4..8..6......5...........418.........3..2...87.....",
    74   List("52...6.........7.13...........4..8..6......5...........418.........3..2...87.....",
   178        ".....2.......7...17..3...9.8..7......2.89.6...13..6....9..5.824.....891..........",
   182        ".....2.......7...17..3...9.8..7......2.89.6...13..6....9..5.824.....891..........",
   179        "3...8.......7....51..............36...2..4....7...........6.13..452...........8..",
   183        "3...8.......7....51..............36...2..4....7...........6.13..452...........8..",
   180        "....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8...",
   184        "....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8...",
   181        "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......")
   185        "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......")
   182 
   186 
       
   187 println("108:\n" ++ pretty(hard_games(108).replaceAll("\\.", " ")) ++ "\n")
       
   188 println("109:\n" ++ pretty(hard_games(109).replaceAll("\\.", " ")) ++ "\n")
       
   189 
   183 
   190 
   184 // for measuring time
   191 // for measuring time
   185 def time_needed[T](i: Int, code: => T) = {
   192 def time_needed[T](i: Int, code: => T) = {
   186   val start = System.nanoTime()
   193   val start = System.nanoTime()
   187   for (j <- 1 to i) code
   194   for (j <- 1 to i) code