- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试用 Java 创建一个数独求解器,总的来说,我对编程和 Java 都是新手。我真的不知道如何处理这种错误。我不断收到堆栈溢出错误。
我尝试了不同的代码,但没有一个有效,但无论如何,这是我最新的代码:
public class Sudoku {
private int[][] values;
private boolean [][] writable;
private static final int ZERO = 0;
private static final int SIZE = 9;
//just a normal constructor that sets which values are changeable and which aren't. only values equal to zero are changeable.
public Sudoku(int[][] values) {
this.values = new int[SIZE][SIZE];
for(int row = 0; row< SIZE ; row++)
{
for(int col = 0; col< SIZE; col++)
{
this.values[row][col] = values[row][col];
}
}
writable = new boolean[values.length][values[1].length];
for(int i = 0;i < writable.length;i++)
{
for(int j = 0; j<writable[1].length;j++)
{
if(values[i][j] == ZERO)
{
writable[i][j] = true;
}
}
}
}
public void setValues(int row,int col ,int value) //changes the value if the value was changeable.
{
if(writable[row][col])
{
values[row][col]= value;
}
}
public int getValue(int row,int col) {
return values[row][col];
}
public boolean isWritable(int row,int col)
{
return writable[row][col];
}
private boolean ConflictAtRow(int row , int num)
{
for(int i = 0;i < SIZE;i++)
if(getValue(row,i) == num)
return true;
return false;
}
private boolean ConflictAtCol(int col, int num)
{
for(int i = 0;i<SIZE;i++)
if(getValue(i,col) == num)
return true;
return false;
}
private boolean ConflictAtBox(int row, int col, int num)
{
int r = row - row %3;
int c = col - col %3;
for(int i = r;i<r+3;i++)
{
for(int j = c;j<c+3;j++)
{
if(getValue(i, j) == num && row != i && col != j)
return true;
}
}
return false;
}
private boolean ConflictAt(int row, int col, int num)
{
return ConflictAtBox(row, col, num) && ConflictAtCol(col,num) && ConflictAtRow(row, num); //line 108
}
public boolean solve(int row,int col)
{
int nextRow = (col < 8) ? row:row+1;
int nextCol = (col +1)%9;
for (row = nextRow; row < SIZE; row++) {
for (col = NextCol; col < SIZE; col++) {
if(isWritable(row,col))
{
for (int num = 1; num <= 9; num++) {
if(!ConflictAt(row,col,num)) //line 118
{
setValues(row,col,num);
if(solve(nextRow,nextCol)) //line 122
return true;
}
setValues(row,col,ZERO);
}
}return !ConflictAt(row,col,getValue(row,col)) &&
solve(nextRow,nextCol);;
}
}return true;
}
当我运行solve()方法时,我收到堆栈溢出错误
Exception in thread "main" java.lang.StackOverflowError
at Sudoku.Sudoku.ConflictAt(Sudoku.java:108)
at Sudoku.Sudoku.solve(Sudoku.java:118)
at Sudoku.Sudoku.solve(Sudoku.java:122)
at Sudoku.Sudoku.solve(Sudoku.java:122)
at Sudoku.Sudoku.solve(Sudoku.java:122)
at Sudoku.Sudoku.solve(Sudoku.java:122)
等等……
最佳答案
一旦控件第一次进入 solve()
方法,并且如果直到第 122 行所有 if
条件都计算为 true
,则您将再次调用 solve()
方法。
问题是,每次控件调用此方法时,就好像它是第一次执行它一样。因为条件没有变化(for
循环始终从 0
开始)。
这意味着,solve()
方法会被重复调用,直到堆栈内存耗尽。
关于java - 如何修复: Sudoku solver Stack overflow problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54591657/
打开几个编写宏/代码的示例,我突然被一个似乎安全的代码卡住了。求解器 (SOLVER.XLAM) 这是值得注意的,我想从我的系统中删除它。我想这不是一个相关的代码。谁能告诉我我应该怎么做?我已经安装了
是否可以使用 MSF api 将变量指定为半整数(V = 0 或 a = 5; Q1 = 5; Q2 10 * VPositive<= V <= 20 * VPositive ] ] 如果您使用
我正在研究线性和非线性编程(优化)框架的选项。 要求是: 支持大约线性和非线性规划问题。 100-1000 个变量和最多约 1000 个约束(我认为这很简单)。非线性问题具有涉及多个变量的乘法或除法的
SMT-Solver 可用于约束求解。众所周知,CSP 求解器也用于约束求解多年。那么 SMT 求解器与 CSP 求解器相比有什么优势呢? 最佳答案 这完全取决于你想做什么。您可以将两者都转换为 SA
我正在查看 Caffe LeNet 教程 here我想到了一个问题: 这两个代码有什么区别: self.solver.step(1) 和 self.solver.net.forward() # tr
我正在使用 CBC 求解器在 PyCharm 上解决优化问题,但一直遇到此错误: 错误:求解器 (cbc) 返回非零返回代码 (3221225781) 和 pyutilib.common._excep
我是 microsoft excels 求解器的用户,我很确定不可能求解最大化两个值。我想知道是否有人可能有另一种聪明的方法来做到这一点。 基本上,我有一列介于 1 到 30 之间的数字,我需要查看它
我想从数值的角度理解混合建模(特别是状态事件)背后的一般思想(尽管我不是数学家 :))。鉴于以下 Modelica 模型: model BouncingBall constant Real g
最近,我开始研究形式验证技术。在文献中,模型检查器和求解器可以以某种方式互换使用。 但是,模型检查器和求解器如何相互连接? p.s.如果建议提供一些论文或链接,我将不胜感激。 最佳答案 为了执行模型检
我希望这对某人来说是显而易见的。我只使用过 GLPK/MathProg。 我无法弄清楚 GNU MathProg(在 GLPK 内)中的语法来执行以下操作,例如: set PartsOfWeek; s
我有一个(对我来说)非常复杂的问题。我有两个向量: vectora <- c(111, 245, 379, 516, 671) vectorb <- c(38, 54, 62, 67, 108) 此外
我在 VBA 循环中使用 Excel 2007 中的内置求解器来解决许多不同的问题。有时,求解器会达到最大时间,这会导致出现弹出对话框,询问用户是否要继续、停止或结束。在所有情况下,我都希望它结束
我在 PC 上的 Excel 2010 中创建了一个程序,该程序依赖于 Excel 的内置求解器。我确保它适用于 PC 版 Excel 2010 和 Excel 2013。然后我尝试在 Excel 2
在汽车行业,当您购买汽车时,您有数千种不同的组件可供选择。并非每个组件都是可组合的,因此对于每辆汽车,都存在许多用命题逻辑表达的规则。就我而言,每辆车都有 2000 到 4000 条规则。 它们看起来
有没有办法让我们知道解算器中添加了多少约束?例如,我们初始化一个 z3 求解器 s = Solver(),然后使用 s.add() 添加约束。我们如何获得最终添加到求解器中的约束数量? 最佳答案 您可
我正在尝试修改 ceres 的默认行为,即计算残差的平方和作为成本函数。我希望它只计算总和(残差已经以只能为正的方式计算) 根据文档,我应该使用 ConditionedCostFunction 这是我
我有这个方法来求解根据增量返回 2 种类型数组的二次方程(如果小于零则有复数解) public static final String[] quadEquationSolver(double a, d
p = Int('p') q = Int('q') s = Solver() s.add(1<=p<=9, 1<=q<=19, 5<(3*p-4*q)<10) s.check() print s.mo
“问题”在标题中不应该是问题的情况。 我想为一组问题(类 Problem 的所有子项)实现一个求解器(类 Solver),这些问题或多或少共享同一组方法。我目前的设计是这样的: 在solver.h :
我为所有方向创建了一个单词求解器。它可以水平、垂直和反向查找单词。然而,我在让它走向各个方向时遇到了问题。所以把“你好”放在: H E i l x L p q c L O m 任何人
我是一名优秀的程序员,十分优秀!