- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 SciLab 中编写了一个解决数独问题的程序。但它只能解决总是有一个可能值为 1 的正方形的数独。就像 brainbashers.com 上非常简单和简单的数独游戏。
中等数独总是会到达一个点,即它们没有具有 1 个可能值的正方形。我如何修改我的代码来解决这些更难的数独问题?
///////////////////////////////////////////////////////////////////////////
////////////////////////// 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
function [x,y]=firstEmptyValue(board) // Checks the first empty square of the sudoku
R=%T // and returns the position (x,y)
for i=1 : 9
for j=1 : 9
if board(i,j) == 0 & R = %T then
x=i
y=j
R=%F
end
end
end
endfunction
function A=numberOfPossibilities(V) // this checks the number of possible values for a position
A=0 // so basically it returns the number of elements different from 0 in the vector V
for i=1 : 9
if V(i)>0 then
A=A+1
end
end
endfunction
function u=getUniquePossibility(M,x,y) // this returns the first possible value for that square
pos = [] // in function fillInValue we only use it
pos = PossibilitiesPosition(M,x,y) // when we know that this square (x,y) has only one possible value
for n=1 : 9
if pos(n)>0 then
u=pos(n)
end
end
endfunction
///////////////////////////////////////////////////////////////////////////
////////////////////////// Solve Sudoku ///////////////////////////////
///////////////////////////////////////////////////////////////////////////
function G=fillInValue(M) // fills in a square that has only 1 possibile value
x=0
y=0
pos = []
for i=1 : 9
for j=1 : 9
if M(i,j)==0 then
if numberOfPossibilities(PossibilitiesPosition(M,i,j)) == 1 then
x=i
y=j
break
end
end
end
if x>0 then
break
end
end
M(x,y)=getUniquePossibility(M,x,y)
G=M
endfunction
function H=solve(M) // repeats the fillInValue until it is a fully solved sudoku
P=[]
P=M
if check(M)=%F then
P=fillInValue(M)
H=solve(P)
else
H=M
end
endfunction
//////////////////////////////////////////////////////////////////////////////
所以它解决了第一个
// Very easy and easy sudokus from brainbashers.com get solved completely
// 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]
但它并没有解决这个媒介:
M2= [0 0 6 8 7 1 2 0 0
0 0 0 0 0 0 0 0 0
5 0 1 3 0 9 7 0 8
1 0 7 0 0 0 6 0 9
2 0 0 0 0 0 0 0 7
9 0 3 0 0 0 8 0 1
3 0 5 9 0 7 4 0 2
0 0 0 0 0 0 0 0 0
0 0 2 4 3 5 1 0 0]
尝试解决中等数独时的错误代码:
-->solve(M2)
!--error 21
Invalid index.
at line 14 of function PossibilitiesPosition called by :
at line 3 of function getUniquePossibility called by :
at line 20 of function fillInValue called by :
at line 182 of function solve called by :
at line 183 of function solve called by :
at line 183 of function solve called by :
at line 183 of function solve called by :
at line 183 of function solve called by :
solve(M2)
at line 208 of exec file called by :
_SCILAB-6548660277741359031.sce', 1
while executing a callback
最佳答案
好吧,编写数独求解器的最简单方法之一(不是最有效的)可能是使用所有可能的选项递归地求解每个单元格(这可能类似于“回溯”算法),直到得到完整答案找到了。
另一种选择(我会说它更好)是遍历所有方 block 解决所有“简单”方 block 并将可能的答案存储在其他方 block 中,然后重复(现在你已经解决了更多),重复这个过程直到解出数独或不能直接解出更多的方 block 。然后你可以尝试剩下的用暴力或回溯(也许数独已经解出一半或更多,所以它可能相对有效)
无论如何,通过快速搜索我找到了this Wikipedia page其中一些数独求解算法用伪代码示例进行了解释,希望这些对您有用
关于algorithm - 数独求解器 - Scilab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288165/
我的填充数独板的代码如下所示: public class SudokuBoard { static int N = 9; static int[][] grid = new int[N
我正在使用“强力”随机方法创建一个数独生成器。我已经能够使用以下代码检查 x/y 轴是否有重复数字: for(l=0; l<9; l++){//Makes all vertical work.
我有一个数独谜题求解器,需要我们使用递归。问题是我检查可用空间的 boolean 值应该通过引用更新当前位置,但事实并非如此。什么会导致这个? public boolean solve() {
我正在尝试为我的项目制作一个数独游戏,但如果我增加数独Grid中的空白空间数量,代码只会抛出异常arrayoutofbounds但是无法弄清楚它来自哪里。 k 是网格中空白空间的数量。 我没有尝试过任
当我尝试编写一个解决数独问题的程序时,我的代码中有一个错误。 我的程序可以运行,但运行效果不佳。它只解决程序中的第一行。 我认为我在那行代码上犯了错误: int ft_rezolva(int **t)
这是我的。该方法应输出该空间中所有可用数字的数组。出于某种原因,这不会过滤掉相同的框/行/列。应该如何正确编码? public int[] getAllowedValues(int row, in
我遇到了一个问题。我是 Java 的新手,正在尝试尝试比以前更复杂的东西。这是我自己的个人文件输入和主要方法与其他方法的一些蚕食源的组合。我对递归仍然很生疏。出于某种原因,更改二维数组“板”中值的分配
嘿,我无法让代码比较给定行或列和 block 的整数,以确保这些参数中没有重复项。我不知道用 3 种不同的方法分离这三个约束是否是一个好主意,或者只是尝试一次完成所有的操作。 public stati
我正在研究 javascript 数独游戏,就输入验证而言,我很困惑。当每个值都输入到数独表中时,我有一个 onkeyup 触发一个函数,我试图用它来确保没有值进入已经存在这样一个值的行/列/框。这是
我正在研究一个小的个人数独游戏并试图扩展它。 到目前为止,我使用递归回溯方法使“求解”部分正常工作,该方法在设法求解递归时返回 true。 现在我正在尝试构建一个独特的解决方案板生成器,并且我在网上找
数独回溯法 int xx = (pos.getX() / 3) * 3; int yy = (pos.getY() / 3) * 3; for (int y =
我是 c++ 的新手,在做作业(数独)时遇到了问题。 说明说:“你必须创建一个新板作为当前板的拷贝(使用复制构造函数并使用 new) 从堆中分配板。” 我试过了(写在board.cc中): #incl
我目前正在使用 java 制作数独游戏,但我似乎无法弄清楚如何正确地循环我的扫描仪。到目前为止,它打印了这个: . 1 . | 3 . . | 8 . . 5 . 9 | 6 . . | 7 . .
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 12 个月前关闭。 Improve
我正在一个由随机生成的数字(从 1 到 4)填充的多维数组中创建一个 4x4 数独游戏。现在我必须搜索重复项并用其他随机数替换它们,直到单行、单列和四个 2x2 子矩阵上都有唯一的数字。我怎样才能做到
我正在制作一个数独板 GUI,它应该看起来像这样 http://www.sudoku.4thewww.com/Grids/grid.jpg 由于某种原因,它只显示最后一个 3*3 板。如果有人能告诉我
嘿,我写了这个程序来解决数独问题,但它只适用于数独矩阵的几个单元格,而其他单元格则返回 0 。你能明白这有什么问题吗?我是 Java 编码新手,无法编写简单的程序真的很痛苦。 public class
这里是 Stack Overflow 的第一个定时器! 首先,我正在使用 JavaFX 创建一个数独求解器。我一切正常,但是,我遇到的唯一问题是创建粗体 3x3 大块,每个大块内有 3x3 单元格。我
我的逻辑求解算法有问题。它很好地解决了具有大量提示的谜题,它只是解决了少于 45 条线索的谜题。 这是求解的算法。 Immutable 是一个 boolean 值,用于确定该值是否可以更改。 cell
我正在尝试使用 Python 线性优化库 Pulp 来解决 killer 数独问题。 https://en.wikipedia.org/wiki/Killer_sudoku 这是我迄今为止的尝试,添加
我是一名优秀的程序员,十分优秀!