(我对此非常陌生,因此非常感谢有关此帖子和我的代码的一般形式的提示!)
我在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个未知的错误!
即使我可以使用此处使用的每个值组合调用 possibleMatrix 和的可能性(),但只要我放入" =&# 34;在他们之间,事情变得奇怪。
我之前看到132错误,当我尝试将值分配给数组/矩阵的无效索引时,我在这里看不到......
- 以下完美无缺。 (注意" 打印 s"而不是" =")
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站点?
(我试图让它解决数独)
-
possibleMatrix 是这样的: (此处:字段< - > possibleMatrix)
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: $0) && !columnContains(column, number: $0) && !boxContains(row, c: column, number: $0)
}
}
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: $0) && !columnContains(column, number: $0) && !boxContains(row, c: column, number: $0)
}
}
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()
答案 0 :(得分:1)
事实证明问题非常简单。以下是您的可能性函数:
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
}
这里是Sandbox中实现的工作版本供您查看:
编辑:虽然学习递归是一件好事,但如果可以的话,它也可以避免它,因为它比迭代循环占用更多的资源(因为每次你调用一个函数时,它都是&#39;放在程序堆栈上并占用资源)。这是实现相同结果的更快方法: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)
}
}
}
}