【问题标题】:Swift Recursive Back Tracking Algorithm Not WorkingSwift递归回溯算法不起作用
【发布时间】:2014-12-10 10:41:21
【问题描述】:

您好,我用 swift 编写了一个类,它应该通过递归回溯算法创建一个迷宫。我似乎在为迷宫分配墙壁方面遇到问题。但是我破解不了。

如果我能在这里得到一些帮助,那就太好了,谢谢!

请在下面找到对代码的描述

2D 数组类 - 非常不言自明。给它一些列和行以及一个默认值,从那里生成二维数组。下标方法允许您设置和获取值。

class Array2D<T> {
 let columns: Int
 let rows: Int
 var array : Array<Array<T?>>

 init(columns: Int, rows: Int, repeatedValue: T?) {
  self.columns = columns
  self.rows = rows
  var tmp = Array<T?>(count: rows, repeatedValue: repeatedValue)
  array = Array<Array<T?>>(count: columns, repeatedValue: tmp)
 }

 subscript(column: Int, row: Int) -> T? {
  get {
   return array[column][row]
  }
  set(newValue) {
   array[column][row] = newValue
  }
 }
}

DIR 枚举 一个枚举,它允许我们通过为比特分配名称来抽象自己。

enum DIR : UInt8 {
 case N = 1
 case S = 2
 case E = 4
 case W = 8
 case O = 0
}

Direction 类 该类包含有关Directions 的所有信息。这可能有点矫枉过正。它允许您获取与当前位置相关的方向(N、S、E、W)。你也可以得到相反的方向。

class Direction {
 var bit : DIR
 var dx  : Int
 var dy  : Int

 init(bit: DIR, dx: Int, dy: Int) {
  self.bit  = bit
  self.dx   = dx
  self.dy   = dy
 }

 class func NORTH() -> Direction {
  return Direction(bit: DIR.N, dx: 0, dy: -1)
 }

 class func SOUTH() -> Direction {
  return Direction(bit: DIR.S, dx: 0, dy: 1)
 }

 class func EAST() -> Direction {
  return Direction(bit: DIR.E, dx: 1, dy: 0)
 }

 class func WEST() -> Direction {
  return Direction(bit: DIR.W, dx: -1, dy: 0)
 }

 func opposite() -> Direction {
  switch(bit){
  case DIR.N:
   return Direction.SOUTH()
  case DIR.S:
   return Direction.NORTH()
  case DIR.E:
   return Direction.WEST()
  case DIR.W:
   return Direction.EAST()
  default:
   println("An error occured while returning the opposite of the direction with bit: \(bit)")
   return Direction(bit: DIR.O, dx: 0, dy: 0)
  }
 }  
}

RecursiveBackTracking 类 这就是魔法发生的地方。这个类自动生成一个给定宽度(x)和高度(y)的迷宫。 generateMaze() 函数与支持的其他函数一起完成大部分工作。这里的一切似乎都有效,但我仍然没有得到适当的结果。潜在的问题也可能出在 display() 函数中。

class RecursiveBacktracking {
 var x : Int
 var y : Int
 var maze : Array2D<UInt8>

 init(x: Int, y: Int) {
  self.x = x
  self.y = y
  maze = Array2D<UInt8>(columns: x, rows: y, repeatedValue: 0)
  generateMaze(0, cy: 0)
  display()
 }

 func generateMaze(cx: Int, cy: Int) {
  var directions : [Direction] = [Direction.NORTH(),Direction.SOUTH(),Direction.EAST(),Direction.WEST()]
  directions = shuffle(directions)

  for dir in directions {
   var nx : Int = cx + dir.dx
   var ny : Int = cx + dir.dy

   if between(nx, upper: x) && between(ny, upper: y) && getMazeObject(nx, y: ny) == 0 {
    maze[cx,cy] = bitwiseOr(getMazeObject(cx, y: cy), b: dir.bit.rawValue)
    maze[nx,ny] = bitwiseOr(getMazeObject(nx, y: ny), b: dir.opposite().bit.rawValue)
    generateMaze(nx, cy: ny)
   }
  } 
 }

 func bitwiseOr(a: UInt8, b: UInt8) -> UInt8 {
  return a | b
 }

 func getMazeObject(x: Int, y: Int) -> UInt8 {
  if var object = maze[x,y] {
   return object
  }else{
   println("No object could be found at location: (\(x),\(y)).")
   return 0
  }
 }

 func between(v: Int, upper: Int) -> Bool {
  return (v>=0) && (v<upper)
 }

 func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
  let count : Int = Int(countElements(list))
  for i in 0..<(count - 1) {
   let j = Int(arc4random_uniform(UInt32(count - i))) + i
   swap(&list[i], &list[j])
  }
  return list
 }

 func display() {
  for i in 0..<y {
   // Draw North Edge
   for j in 0..<x {
    var bit : UInt8 = getMazeObject(j, y: i)
    bit = bit & 1
    if bit == 0 {
     print("+---")
    }else{
     print("+   ")
    }
   }
   println("+")

   // Draw West Edge
   for j in 0..<x {
    var bit : UInt8 = getMazeObject(j, y: i)
    bit = bit & 8
    if bit == 0 {
     print("|   ")
    }else{
     print("    ")
    }
   }
   println("|")
  }

  // Draw the bottom line
  for j in 0..<x {
   print("+---")
  }
  println("+")
 }
}

附加信息:此算法基于http://rosettacode.org/wiki/Maze#Java

【问题讨论】:

    标签: algorithm swift recursion backtracking maze


    【解决方案1】:

    错误在这里:

    var nx : Int = cx + dir.dx
    var ny : Int = cx + dir.dy
    

    第二个cx 应该是cy

    备注:您的代码还有一些改进的余地。例如, 按位或| 已为UInt8 定义,因此无需定义 作为一个函数。如果您已修复代码以正常工作,您可能会考虑 将其发布到http://codereview.stackexchange.com 以获得评论。

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2015-01-21
      • 2018-10-08
      • 2011-12-27
      • 2019-10-11
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多