SHARE
TWEET

BFS-scala-code-la-mode

a guest Mar 13th, 2019 83 in 236 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   def bfs(kitchen: Array[Array[Char]], playerx: Int, playery: Int) = {
  2.  
  3.     var tpoints: Array[(Int, Int)] = Array(playerx -> playery)
  4.  
  5.     val distances = kitchen.map(_.map(_ => 99))
  6.     distances.upd(playerx, playery, 0)
  7.     val prevs = kitchen.map(_.map(_ => (-1, -1)))
  8.  
  9.     val moves = Array((0,1), (1,0), (0, -1), (-1, 0))
  10.  
  11.     while (tpoints.nonEmpty) {
  12.       val newPoints: ArrayBuffer[(Int, Int)] = ArrayBuffer()
  13.       for (point <- tpoints) {
  14.         val newDistance = distances(point._1)(point._2) + 1
  15.         for (move <- moves) {
  16.           val newx = point._1 + move._1
  17.           val newy = point._2 + move._2
  18.           if (newx >= 1 && newx < kitchen.length && newy >= 1 && newy < kitchen(0).length) {
  19.             if (kitchen(newx)(newy) == '.' && distances(newx)(newy) >= 99) {
  20.               distances.upd(newx, newy, newDistance)
  21.               prevs.upd(newx, newy, point._1 -> point._2)
  22.               newPoints += newx -> newy
  23.             }
  24.           }
  25.         }
  26.       }
  27.       tpoints = newPoints.toArray
  28.     }
  29.  
  30.     /*Console.err.println(s"Kitchen dists:")
  31.     for (line <- distances){
  32.       for (i <- line){
  33.         Console.err.print(s" ${"%02d".format(i)}")
  34.       }
  35.       Console.err.println("")
  36.     }
  37.  
  38.     Console.err.println("")
  39.     Console.err.println(s"Kitchen prevs:")
  40.     for (line <- prevs){
  41.       for (i <- line){
  42.         Console.err.print(s"(${"%02d".format(i._1)},${"%02d".format(i._2)})")
  43.       }
  44.       Console.err.println("")
  45.     }*/
  46.  
  47.     distances -> prevs
  48.  
  49.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top