"unknown error code 132" 与 IBM Swift 沙盒

"unknown error code 132" with IBM Swift Sandbox

(我对此很陌生,因此非常感谢有关此 post 和我的代码的一般形式的提示!)


我一直在 IBM Sandbox 中使用 Swift,我似乎无法解决以下问题:

func fillPossibilityMatrix() {        //it's a 9x9 Matrix
for i in 0...80 {
    let row = (i - (i % 9)) / 9       //-> row is Valid for 0 - 8
    let column = i % 9                            
    if possibilityMatrix[row, column] == [0] {
        possibilityMatrix[row, column] = possibilities(row, column: column)
    }
}

这给了我 132 未知错误!

- 以下 工作得很好 。 (注意“prints”而不是“=”)

func fillPossibilityMatrix() { for i in 0...80 { let row = (i - (i % 9)) / 9 let column = i % 9 if possibilityMatrix[row, column] == [0] { print(possibilityMatrix[row, column]) print(possibilities(row, column: column)) } } }

这里有什么问题吗?我只是愚蠢吗?这是特定于 IBM 站点的吗?


剩下的

(我正在尝试让它解数独)

-

possibilityMatrix 是这样产生的: (此处:字段 <-> 可能性矩阵)

struct Matrix {
    let rows: Int, columns: Int
    var grid: [[Int]]
    init(rows: Int, columns: Int) {
        self.rows = rows
        self.columns = columns
        grid = Array(count: rows * columns, repeatedValue: [0])
    }
   func indexIsValidForRow(row: Int, column: Int) -> Bool {
        return row >= 0 && row < rows && column >= 0 && column < columns
    }
    subscript(row: Int, column: Int) -> [Int] {
        get {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            return grid[(row * columns) + column]
        }
        set {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            grid[(row * columns) + column] = newValue
        }
    }
}
var inputArray = [Int!] ()
var input = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
var field = Matrix(rows: 9, columns: 9)

for char in input.characters {
    inputArray.append(Int(String(char)))
}
func fromInputToField() {
    for i in 0..<inputArray.count {
        let row = (i - (i % 9))/9
        let column = i % 9
        field[row, column][0] = (inputArray[i])
    }
}
fromInputToField()

var possibilityMatrix = field

-

possibilities() 它的子函数如下所示:

func possibilities(row: Int, column: Int) -> [Int] {
    let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    return numbers.filter {
        !rowContains(row, number: [=14=]) && !columnContains(column, number: [=14=]) && !boxContains(row, c: column, number: [=14=])
    }
}


func rowContains(r: Int, number: Int) -> Bool {
    for i in 0...8 {
        if possibilityMatrix[r, i][0] == number {
            return true
        }
    }
    return false
}
func columnContains(c: Int, number: Int) -> Bool {
   for i in 0...8 {
        if possibilityMatrix[i, c][0] == number {
            return true
        }
    }
    return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
    let boxLocation = locateBox(r, c: c)
    for x in 0...2 {
        for y in 0...2 {
            if possibilityMatrix[boxLocation.0 + y, boxLocation.1 + x][0] == number {
                return true
            }
        }
    }
    return false
}

func locateBox (r: Int, c: Int) -> (upBorder: Int, leftBorder: Int) {
    if r % 3 != 0 {
        return locateBox(r - 1, c: c)
    }
    if c % 3 != 0 {
        return locateBox(r, c: c - 1)
    }
    return (r, c)
}



复制粘贴

struct Matrix {
    let rows: Int, columns: Int
    var grid: [[Int]]
    init(rows: Int, columns: Int) {
        self.rows = rows
        self.columns = columns
        grid = Array(count: rows * columns, repeatedValue: [0])
    }
   func indexIsValidForRow(row: Int, column: Int) -> Bool {
        return row >= 0 && row < rows && column >= 0 && column < columns
    }
    subscript(row: Int, column: Int) -> [Int] {
        get {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            return grid[(row * columns) + column]
        }
        set {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            grid[(row * columns) + column] = newValue
        }
    }
}
var inputArray = [Int!] ()
var input = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
var field = Matrix(rows: 9, columns: 9)

for char in input.characters {
    inputArray.append(Int(String(char)))
}
func fromInputToField() {
    for i in 0..<inputArray.count {
        let row = (i - (i % 9))/9
        let column = i % 9
        field[row, column][0] = (inputArray[i])
    }
}
fromInputToField()

var possibilityMatrix = field
func possibilities(row: Int, column: Int) -> [Int] {
    let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    return numbers.filter {
        !rowContains(row, number: [=15=]) && !columnContains(column, number: [=15=]) && !boxContains(row, c: column, number: [=15=])
    }
}


func rowContains(r: Int, number: Int) -> Bool {
    for i in 0...8 {
        if possibilityMatrix[r, i][0] == number {
            return true
        }
    }
    return false
}
func columnContains(c: Int, number: Int) -> Bool {
   for i in 0...8 {
        if possibilityMatrix[i, c][0] == number {
            return true
        }
    }
    return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
    let boxLocation = locateBox(r, c: c)
    for x in 0...2 {
        for y in 0...2 {
            if possibilityMatrix[boxLocation.0 + y, boxLocation.1 + x][0] == number {
                return true
            }
        }
    }
    return false
}

func locateBox (r: Int, c: Int) -> (upBorder: Int, leftBorder: Int) {
    if r % 3 != 0 {
        return locateBox(r - 1, c: c)
    }
    if c % 3 != 0 {
        return locateBox(r, c: c - 1)
    }
    return (r, c)
}
func fillPossibilityMatrix() {        //it's a 9x9 Matrix
    for i in 0...80 {
        let row = (i - (i % 9)) / 9       //-> row is Valid for 0 - 8
        let column = i % 9                            
        if possibilityMatrix[row, column] == [0] {
            possibilityMatrix[row, column] = possibilities(row, column: column)
        }
    }
}
fillPossibilityMatrix()

原来这个问题很简单。以下是您的可能函数:

func rowContains(r: Int, number: Int) -> Bool {
    for i in 0...8 {
        if possibilityMatrix[r, i][0] == number {
            return true
        }
    }
    return false
}
func columnContains(c: Int, number: Int) -> Bool {
   for i in 0...8 {
        if possibilityMatrix[i, c][0] == number {
            return true
        }
    }
    return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
    let boxLocation = locateBox(r, c: c)
    for x in 0...2 {
        for y in 0...2 {
            if possibilityMatrix[boxLocation.0 + y, boxLocation.1 + x][0] == number {
                return true
            }
        }
    }
    return false
}

问题是您正在检查 probabilityMatrix 以查看该数字是否存在 - 这是您正在修改的变量。因此,如果您将 probabilityMatrix[0, 0] 更改为等于 [4, 5],那么当您检查 probabilityMatrix[0, 1] 时,您的函数将假定 4 在第一行中,因为您刚刚所做的更改.它的部分检查是查看 probabilityMatrix[0, 0] 的第一个元素,即 4,因此您的代码认为第一行自然有一个 4

完成这些之后,您最终会到达一个正方形,其中的可能性列表为 [],因为错误的数字会随着您沿着一行(或列或框)移动并最终累积所有可能性消失。然后在下一次传递中,像 rowContains 这样的函数将查看 [] 并尝试从中获取第一个元素(如 possibilityMatrix[r, i][0]),该元素不存在。这是您 Index out of range 错误的原因。

解决方案是与 field 而不是 possibilityMatrix 进行比较,因为该变量永远不会改变并且始终保持原始矩阵。所以你的函数应该是这样的:

func rowContains(r: Int, number: Int) -> Bool {
    for i in 0...8 {
        if field[r, i][0] == number {
            return true
        }
    }
    return false
}
func columnContains(c: Int, number: Int) -> Bool {
   for i in 0...8 {
        if field[i, c][0] == number {
            return true
        }
    }
    return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
    let boxLocation = locateBox(r, c: c)
    for x in 0...2 {
        for y in 0...2 {
            if field[boxLocation.0 + y, boxLocation.1 + x][0] == number {
                return true
            }
        }
    }
    return false
}

这是在沙盒中实现的工作版本供您查看:

http://swiftlang.ng.bluemix.net/#/repl/6fe779409351531ce07d3bc3f0c197639538729b40d5f0f658e4dd53985223fe

编辑:虽然学习递归是一件好事,但如果可以的话最好避免它,因为它比迭代循环占用更多的资源(因为每次调用函数时,它都放在程序堆栈中并占用资源)。这是实现相同结果的更快方法:

func locateBox (r: Int, c: Int) -> (upBorder: Int, leftBorder: Int) {
    return ((r - (r % 3)), (c - (c % 3)))
}

此外,您的 fillPossibilityMatrix() 函数作为双循环可能会更好,因为 1) 除法和余数函数在计算上有点昂贵,并且 2) 它更容易阅读。

func fillPossibilityMatrix() {        //it's a 9x9 Matrix
    for row in 0...8 {
        for column in 0...8 {                        
            if possibilityMatrix[row, column] == [0] {
                possibilityMatrix[row, column] = possibilities(row, column: column)
            }
        }
    }
}