gpt4 book ai didi

java - 如何修复: Sudoku solver Stack overflow problem

转载 作者:太空宇宙 更新时间:2023-11-04 09:52:10 26 4
gpt4 key购买 nike

我正在尝试用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com