Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. object Maze extends App{
  2.  
  3.  
  4. def solveMaze(maze: List[String]): Option[String] = {
  5. //list of tuple of index that is walkable.
  6. //(row, col)
  7.  
  8. def walkableHelper(maze: List[Char],returnList: List[(Int, Int)], row: Int, col: Int): List[(Int, Int)] = maze match {
  9. case h::t => {
  10. if (h.equals('x')) walkableHelper(t, (-1,-1)::returnList, row, col+1)
  11. else walkableHelper(t, (row, col)::returnList, row, col+1)
  12. }
  13. case _ => returnList
  14. }
  15. def getWalkable(maze: List[String], returnList: List[(Int, Int)], row: Int): List[(Int,Int)] = maze match {
  16. case h::t => getWalkable(t, walkableHelper(h.toList, List(), row, 0)++returnList, row+1)
  17. case _ => returnList
  18. }
  19.  
  20. def getSpecialCharacter(maze: List[String], row: Int, specialChara: String): (Int, Int) = maze match {
  21. case h::t => {
  22. val pos = h.indexOf(specialChara)
  23. if (pos != -1) (row, pos)
  24. else getSpecialCharacter(t, row+1, specialChara)
  25. }
  26. }
  27.  
  28. def helperCreateMap(submaze: List[Char], returnList: Map[(Int, Int), Set[(Int,Int)]], row: Int, col: Int, valid: List[(Int, Int)]): Map[(Int,Int), Set[(Int,Int)]] = submaze match {
  29. case h::t => {
  30. if (valid.contains((row,col))) {
  31. helperCreateMap(t, Map((row,col) -> Set((row-1, col), (row+1,col), (row,col+1), (row,col-1)).filter(valid.contains(_))) ++ returnList, row, col+1, valid)
  32. }
  33. else {
  34. helperCreateMap(t, returnList, row, col+1, valid)
  35. }
  36. }
  37. case _ => returnList
  38. }
  39.  
  40. def createMap(maze: List[String], returnList: Map[(Int, Int), Set[(Int, Int)]], valid: List[(Int, Int)], row: Int): Map[(Int, Int), Set[(Int,Int)]] = maze match {
  41. case h::t => {
  42. createMap(t, helperCreateMap(h.toList, Map(), row, 0, valid) ++ returnList, valid, row+1)
  43. }
  44. case _ => returnList
  45. }
  46.  
  47. def translate(point1: (Int, Int), point2: (Int, Int)): String = {
  48. val direction = (point1._1-point2._1, point1._2-point2._2)
  49. direction match {
  50. case (0, -1) => "r"
  51. case (0, 1) => "l"
  52. case (-1, 0) => "d"
  53. case (1, 0) => "u"
  54. case _ => ""
  55. }
  56. }
  57.  
  58. def finder(returnList: List[String], current: (Int, Int), target: (Int, Int), parents: Map[(Int, Int), (Int, Int)], count: Int): Option[String] = {
  59. if (current.equals(target)) {
  60. Some(returnList.reverse.mkString)
  61. }
  62. else {
  63. val next = parents.get(current)
  64. next match {
  65. case Some(w) => {
  66. finder( translate(current,w)::returnList, w, target, parents, count+1)
  67. }
  68. case None => None
  69. }
  70. }
  71. }
  72.  
  73. val walkablePath = getWalkable(maze, List(), 0)
  74. val start = getSpecialCharacter(maze, 0, "s")
  75. val end = getSpecialCharacter(maze, 0 , "e")
  76. val validPath = createMap(maze, Map(), walkablePath, 0)
  77.  
  78. val (parent, distance) = GraphBFS.bfs[(Int, Int)](validPath, end)
  79.  
  80. finder(List(), start, end, parent, 0)
  81. }
  82.  
  83.  
  84. val maze = List(
  85. "xxxxxxxxxxxxxxxxxx",
  86. "x x x ex",
  87. "x x x x xxxx",
  88. "x x x x x",
  89. "xs x x x",
  90. "xxxxxxxxxxxxxxxxxx"
  91. )
  92.  
  93. def time[R](block: => R): R = {
  94. val t0 = System.currentTimeMillis()
  95. val result = block // call-by-name
  96. val t1 = System.currentTimeMillis()
  97. println("Elapsed time: " + (t1 - t0) + "ms")
  98. result
  99. }
  100.  
  101. // println(solveMaze(maze))
  102.  
  103. val maze250 = List("x" * 250):::List("xs" + " " * 247 + "x"):::List.fill(246)("x"+" " * 248+"x"):::List("x" + " " * 247 + "ex"):::List("x" * 250)
  104.  
  105.  
  106. time(println(solveMaze(maze250)))
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement