# HG changeset patch # User Christian Urban # Date 1667475009 0 # Node ID 440836d805e35a47107ba5aeaed819b54f43383c # Parent 4c3d14a99441fa7913a7732ee8eca8c3d99dfee8 updated diff -r 4c3d14a99441 -r 440836d805e3 main_templates4/knight4.scala --- a/main_templates4/knight4.scala Thu Nov 03 09:55:11 2022 +0000 +++ b/main_templates4/knight4.scala Thu Nov 03 11:30:09 2022 +0000 @@ -1,7 +1,14 @@ // Part 4 about finding a single tour on "mutilated" chessboards //============================================================== -object M4d { // for preparing the jar +object M4d { + +// !!! Copy any function you need from file knight1.scala !!! +// !!! or knight2.scala or knight3.scala !!! +// +// If you need any auxiliary function, feel free to +// implement it, but do not make any changes to the +// templates below. type Pos = (Int, Int) type Path = List[Pos] @@ -16,38 +23,11 @@ } } -def add_pair(x: Pos, y: Pos): Pos = - (x._1 + y._1, x._2 + y._2) - -def is_legal(dim: Int, path: Path, x: Pos): Boolean = - 0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) - -def moves(x: Pos): List[Pos] = - List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), - (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x, _)) - -def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = - moves(x).filter(is_legal(dim, path, _)) - -import scala.annotation.tailrec +// ADD YOUR CODE BELOW +//====================== -@tailrec -def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { - case Nil => None - case x::xs => { - val result = f(x) - if (result.isDefined) result else first(xs, f) - } -} - - -def one_tour_pred(dim: Int, path: Path, n: Int, pred: Pos => Boolean): Option[Path] = { - if (path.length == n) Some(path) - else - first(legal_moves(dim, path, path.head).filter(pred), (x: Pos) => one_tour_pred(dim, x::path, n, pred)) -} - -//print_board(8, one_tour_pred(8, List((0, 0)), 40, x => x._1 < 5).get) +// (10) +def one_tour_pred(dim: Int, path: Path, n: Int, pred: Pos => Boolean): Option[Path] = ??? } diff -r 4c3d14a99441 -r 440836d805e3 main_templates5/bfc.scala --- a/main_templates5/bfc.scala Thu Nov 03 09:55:11 2022 +0000 +++ b/main_templates5/bfc.scala Thu Nov 03 11:30:09 2022 +0000 @@ -66,7 +66,7 @@ // testcases // // optimise(load_bff("benchmark.bf")) // should have inserted 0's -// optimise(load_bff("mandelbrot.bf")).length // => 11205 +// optimise(load_bff("mandelbrot.bf")).length // => 11203 // // time_needed(1, run3(load_bff("benchmark.bf")))