小编use*_*542的帖子

如何修复此堆栈溢出错误?

所以我认为我认为java中的数独求解器的代码非常好,但我需要一些帮助.当我将它嵌入main方法时,它给了我一个堆栈溢出.问题是我的方法不知道如何扭转并修复它的错误.我需要一个布尔标志(一个与下面的代码中使用的标志不同,实际上最好工作)或其他东西让它知道什么时候应该转回来,什么时候它可以再次前进并继续解决游戏.谢谢你提供的所有帮助

public void play(int r, int c){//this method throws the StackOverflowError
    if(needAtLoc(r,c).size()==9){
        int num=1+generator.nextInt(9);
        setCell(r,c,num,this);

    if(c<8){
    System.out.println(this);///////////////
    play(r, c+1);
    }
    else{
    play(r+1, 0);
    }
}
else{
    if(needAtLoc(r,c).size()==0){//no possible moves THIS IS THE PROBLEM LINE!!!
    if(c>0){
        play(r, c-1);//play last cell, in column to left
    }
    else{
        if(r==0){
        play(r,c);//first square, so must play again (can't go back)
        }
        else{
        play(r-1, 8);/*first cell of row so must go to previous row and 
                   the end column*/
        }
    }
    }

    else{//if there are possible moves …
Run Code Online (Sandbox Code Playgroud)

java stack-overflow recursion sudoku

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

recursion ×1

stack-overflow ×1

sudoku ×1