- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试编写一个使用回溯解决数独问题的程序。我现在正在使用 scilab。我的递归算法不断出错,我一定是做错了什么。欢迎任何帮助。
我把我的错误代码放在底部。
///////////////////////////////////////////////////////////////////////////
////////////////////////// Check Sudoku ///////////////////////////////
///////////////////////////////////////////////////////////////////////////
function r=OneToNine(V) // function checks if the given vector V contains 1 to 9
r = %T // this works
u = %F
index = 1
while r == %T & index < 10
for i=1 : length(V)
if V(i)==index then
u = %T
end
end
index=index+1
if u == %F then r = %F
else u = %F
end
end
if length(V) > 9 then r = %F
end
endfunction
function y=check(M) // Checks if the given matrix M is a solved sudoku
y = %T // this works too
if size(M,1)<>9 | size(M,2)<>9 then // if it has more or less than 9 rows and columns
y = %F // we return false
end
for i=1 : size(M,1) // if not all rows have 1-9 we return false
if OneToNine(M(i,:)) == %F then
y = %F
end
end
endfunction
function P=PossibilitiesPosition(board, x, y)
// this one works
// we fill the vector possibilites with 9 zeros
// 0 means empty, 1 means it already has a value, so we don't need to change it
possibilities = [] // a vector that stores the possible values for position(x,y)
for t=1 : 9 // sudoku has 9 values
possibilities(t)=0
end
// Check row f the value (x,y) for possibilities
// we fill the possibilities further by puttin '1' where the value is not possible
for i=1 : 9 // sudoku has 9 values
if board(x,i) > 0 then
possibilities(board(x,i))=1
end
end
// Check column of the value (x,y) for possibilities
// we fill the possibilities further by puttin '1' where the value is not possible
for j=1 : 9 // sudoku has 9 values
if board(j, y) > 0 then
possibilities(board(j, y))=1
end
end
// Check the 3x3 matrix of the value (x,y) for possibilities
// first we see which 3x3 matrix we need
k=0
m=0
if x >= 1 & x <=3 then
k=1
else if x >= 4 & x <= 6 then
k = 4
else k = 7
end
end
if y >= 1 & y <=3 then
m=1
else if y >= 4 & y <= 6 then
m = 4
else m = 7
end
end
// then we fill the possibilities further by puttin '1' where the value is not possible
for i=k : k+2
for j=m : m+2
if board(i,j) > 0 then
possibilities(board(i,j))=1
end
end
end
P = possibilities
// we want to see the real values of the possibilities. not just 1 and 0
for i=1 : 9 // sudoku has 9 values
if P(i)==0 then
P(i) = i
else P(i) = 0
end
end
endfunction
///////////////////////////////////////////////////////////////////////////
////////////////////////// Solve Sudoku ///////////////////////////////
///////////////////////////////////////////////////////////////////////////
function S=solve(board) // the real solving function, here must be a problem somewhere
x=1
y=1
possibilities = [] // an empty vector to put in the possible value later on
if check(board) == %T then // if it's a fully solved sudoku the function gives the solution
S = board
else
for i=1 : 9 // sudoku has 9 values
for j=1 : 9 // sudoku has 9 values
if board(i,j)==0 then // if the value board(i,j) is empty so 0
x=i // we put the coordinates in x and y
y=j // break means stop the loop
break
end
end
end
possibilities = PossibilitiesPosition(board,x,y) //we check the possibilities
for p=1 : 9 // sudoku has 9 values
if possibilities(p) > 0 then // if this is a possibility
board(x,y) = possibilities(p) // we put in that value
solve(board) // and we repeat until check(board) gives true
end
end
board(x,y)=0 // if we went trough al the possibilities
end // and there is still no solution we have to put that value back to 0
// this is called backtracking
endfunction
//////////////////////////////////////////////////////////////////////////////
错误代码:
// Very Easy sudoku from brainbashers.com
M = [0 2 0 0 0 0 0 4 0
7 0 4 0 0 0 8 0 2
0 5 8 4 0 7 1 3 0
0 0 1 2 8 4 9 0 0
0 0 0 7 0 5 0 0 0
0 0 7 9 3 6 5 0 0
0 8 9 5 0 2 4 6 0
4 0 2 0 0 0 3 0 9
0 1 0 0 0 0 0 8 0]
solve(M)
!--错误4 undefined variable :S在调用函数 solve 的第 31 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
在函数 solve 调用的第 24 行:
求解(M)在调用的 exec 文件的第 184 行:
B-8525585618758424927.sce', 1在执行回调时
最佳答案
主要问题是这一行:
solve(board)
应该是
S = solve(board)
递归函数必须提供两件事:一种将修改后的任务委托(delegate)给自身的另一个实例的方法,和一种将结果返回给调用它的实例的方法。你没有第二件事。
另一个较小的问题是您对 x,y 的搜索:
for i=1 : 9 // sudoku has 9 values
for j=1 : 9 // sudoku has 9 values
if board(i,j)==0 then // if the value board(i,j) is empty so 0
x=i // we put the coordinates in x and y
y=j // break means stop the loop
break
end
end
end
break
命令仅停止调用它的循环。所以,它打破了你在 j 上的循环,但随后在 i 上的循环继续。快速修复:
x=0 // instead of 1
y=0 // instead of 1
// other stuff you have there
for i=1 : 9 // sudoku has 9 values
for j=1 : 9 // sudoku has 9 values
if board(i,j)==0 then // if the value board(i,j) is empty so 0
x=i // we put the coordinates in x and y
y=j // break means stop the loop
break
end
end
if x>0 then
break
end
end
此外,您可能需要 disp(solve(M))
在最后;否则计算的最终结果既不会显示也不会保存在任何变量中。
通过这些更正,您的代码可以正常工作,输出此解决方案:
1. 2. 6. 8. 9. 3. 7. 4. 5.
7. 3. 4. 6. 5. 1. 8. 9. 2.
9. 5. 8. 4. 2. 7. 1. 3. 6.
5. 6. 1. 2. 8. 4. 9. 7. 3.
8. 9. 3. 7. 1. 5. 6. 2. 4.
2. 4. 7. 9. 3. 6. 5. 1. 8.
3. 8. 9. 5. 7. 2. 4. 6. 1.
4. 7. 2. 1. 6. 8. 3. 5. 9.
6. 1. 5. 3. 4. 9. 2. 8. 7.
需要考虑的事情:
return [i,j]
在双循环内部,停止整个双循环:for i=1 : 9 // sudoku has 9 values
for j=1 : 9 // sudoku has 9 values
if board(i,j)==0 then // if the value board(i,j) is empty so 0
return [i,j]
end
end
end
// do something if the matrix is full
board(x,y)=0
没有效果,因为局部变量的值 board
当函数结束时,它就会被遗忘。如果给定一个无法解决的难题,您的代码将进入无限递归。 关于algorithm - 数独求解器 Scilab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30277003/
我正在使用混合效应模型,并且由于我的方法的特殊性我需要解决下面模型的积分,然后制作图表获得的估计值。 换句话说,我需要求解下面的积分: 其中,di^2 是我模型中的 Var3,dh 是混合效应模型对应
我有一个方程组,我想用数值方法求解它。给定起始种子,我想得到一个接近的解决方案。让我解释。 我有一个常量向量,X,值: X <- (c(1,-2,3,4)) 和一个向量 W 的权重: W <- (c(
假设我有以下方程组: a * b = 5 sqrt(a * b^2) = 10 如何求解 R 中 a 和 b 的这些方程? 我想这个问题可以说是一个优化问题,具有以下功能......? fn <- f
我在 R 中有一个简单的通量模型。它归结为两个微分方程,对模型中的两个状态变量进行建模,我们将它们称为 A和 B .它们被计算为四个分量通量的简单差分方程 flux1-flux4 , 5 个参数 p1
R有什么办法吗?求解给定单变量函数的反函数?动机是我以后告诉R使用值向量作为反函数的输入,以便它可以吐出反函数值。 例如,我有函数 y(x) = x^2 ,逆是 y = sqrt(x) .有没有办法R
我在字符串中有以下方程 y = 18774x + 82795 求解x我会这样做:- x = (y-82795) / 18774 我知道y的值 但是方程一直在变化,并且始终采用字符串格式 是否可以简单地
如果我用 diophantine(2*x+3*y-5*z-77) 我收到了这个结果。 {(t_0, -9*t_0 - 5*t_1 + 154, -5*t_0 - 3*t_1 + 77)} 到目前为止还
我正在尝试求解仅限于正解的 ODE,即: dx/dt=f(x) x>=0。 在 MATLAB 中这很容易实现。 R 是否有任何变通方法或包来将解决方案空间限制为仅正值? 这对我来说非常重要,不幸的是没
下面的 ANTLR 文法中的 'expr' 规则显然是相互左递归的。作为一个 ANTLR 新手,我很难解决这个问题。我已经阅读了 ANTLR 引用书中的“解决非 LL(*) 冲突”,但我仍然没有看到解
我有一个关于在 R 中求解函数的可能性的非常基本的问题,但知道答案确实有助于更好地理解 R。 我有以下等式: 0=-100/(1+r)+(100-50)/(1+r)^2+(100-50)/(1+r)^
我正在编写使用递归回溯来解决 8 个皇后问题的代码(将 n 个国际象棋皇后放在 n × n 的棋盘上,这样皇后就不会互相攻击)。 我的任务是创建两个方法:编写一个公共(public)solveQuee
我不知道在以下情况下如何进行,因为最后一个方程没有所有 4 个变量。所以使用了等式下面的代码,但这是错误的......有谁知道如何进行? 方程: 3a + 4b - 5c + d = 10 2a +
假设我们有这个递归关系,它出现在 AVL 树的分析中: F1 = 1 F2 = 2 Fn = Fn - 1 + Fn - 2 + 1(其中 n ≥ 3) 你将如何解决这个递归以获得 F(n) 的封闭形
在Maple中,有谁知道是否存在一个函数来求解变量?例如,我正在尝试求解 r 的 solve4r=(M-x^y)*(r^(-1)) mod (p-1)。所以我知道 M、x、y 和 p 的值,但不知道
我也问过这个here在声音设计论坛上,但问题是沉重的计算机科学/数学,所以它实际上可能属于这个论坛: 因此,通过读取文件中的二进制文件,我能够成功地找到关于 WAV 文件的所有信息,除了 big si
我有以下问题: 设 a 和 b 为 boolean 变量。是否可以设置 a 和 b 的值以使以下表达式的计算结果为 false? b or (((not a) or (not a)) or (a or
我需要用 C 求解这个超越方程: x = 2.0 - 0.5sen(x) 我试过这个: double x, newx, delta; x = 2.0 - 0.5; newx = sin(x); del
我在 Windows 上使用 OpenCV 3.1。 一段代码: RNG rng; // random number generator cv::Mat rVec = (cv::Mat_(3, 1)
我正在尝试求解一个包含 3 个变量和数量可变的方程的方程组。 基本上,系统的长度在 5 到 12 个方程之间,无论有多少个方程,我都试图求解 3 个变量。 看起来像这样: (x-A)**2 + (y-
我正在尝试为有限差分法设计一种算法,但我有点困惑。所讨论的 ODE 是 y''-5y'+10y = 10x,其中 y(0)=0 且 y(1)=100。所以我需要一种方法来以某种方式获得将从关系中乘以“
我是一名优秀的程序员,十分优秀!