| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Mon, 25 Dec 2023 01:10:55 +0100 | |
| changeset 479 | 78cb5cdda3c3 | 
| parent 473 | be818c5a67d4 | 
| permissions | -rw-r--r-- | 
| 397 | 1 | // Main Part 4 about finding Knight's tours | 
| 2 | //========================================== | |
| 296 | 3 | |
| 4 | ||
| 397 | 5 | object M4a {
 | 
| 214 | 6 | |
| 400 | 7 | // If you need any auxiliary functions, feel free to | 
| 8 | // implement them, but do not make any changes to the | |
| 214 | 9 | // templates below. Also have a look whether the functions | 
| 400 | 10 | // at the end of the file are of any help. | 
| 214 | 11 | |
| 12 | type Pos = (Int, Int) // a position on a chessboard | |
| 13 | type Path = List[Pos] // a path...a list of positions | |
| 14 | ||
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 15 | // ADD YOUR CODE BELOW | 
| 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 16 | //====================== | 
| 214 | 17 | |
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 18 | |
| 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 19 | |
| 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 20 | //(1) | 
| 347 | 21 | def is_legal(dim: Int, path: Path, x: Pos) : Boolean = ??? | 
| 214 | 22 | |
| 23 | ||
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 24 | //(2) | 
| 347 | 25 | def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ??? | 
| 214 | 26 | |
| 27 | ||
| 296 | 28 | //some testcases | 
| 214 | 29 | // | 
| 30 | //assert(legal_moves(8, Nil, (2,2)) == | |
| 31 | // List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4))) | |
| 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))) | |
| 36 | ||
| 37 | ||
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 38 | // (3) | 
| 347 | 39 | def count_tours(dim: Int, path: Path) : Int = ??? | 
| 214 | 40 | |
| 347 | 41 | def enum_tours(dim: Int, path: Path) : List[Path] = ??? | 
| 214 | 42 | |
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 43 | // (4) | 
| 347 | 44 | def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ??? | 
| 214 | 45 | |
| 46 | ||
| 296 | 47 | // testcases | 
| 48 | // | |
| 214 | 49 | //def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None | 
| 50 | // | |
| 51 | //first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo) // Some(List((4,0))) | |
| 52 | //first(List((1, 0),(2, 0),(3, 0)), foo) // None | |
| 53 | ||
| 54 | ||
| 425 
6e990ae2c6a3
updated solutions and templates
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
400diff
changeset | 55 | //(5) | 
| 347 | 56 | def first_tour(dim: Int, path: Path) : Option[Path] = ??? | 
| 214 | 57 | |
| 58 | ||
| 59 | ||
| 60 | /* Helper functions | |
| 61 | ||
| 62 | ||
| 63 | // for measuring time | |
| 64 | def time_needed[T](code: => T) : T = {
 | |
| 65 | val start = System.nanoTime() | |
| 66 | val result = code | |
| 67 | val end = System.nanoTime() | |
| 68 |   println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
 | |
| 69 | result | |
| 70 | } | |
| 71 | ||
| 72 | // can be called for example with | |
| 400 | 73 | // | 
| 214 | 74 | // time_needed(count_tours(dim, List((0, 0)))) | 
| 400 | 75 | // | 
| 214 | 76 | // in order to print out the time that is needed for | 
| 77 | // running count_tours | |
| 78 | ||
| 296 | 79 | |
| 214 | 80 | // for printing a board | 
| 81 | def print_board(dim: Int, path: Path): Unit = {
 | |
| 347 | 82 | println() | 
| 214 | 83 |   for (i <- 0 until dim) {
 | 
| 84 |     for (j <- 0 until dim) {
 | |
| 85 |       print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
 | |
| 86 | } | |
| 347 | 87 | println() | 
| 214 | 88 | } | 
| 89 | } | |
| 90 | ||
| 91 | ||
| 92 | */ | |
| 296 | 93 | |
| 94 | } |