0

i'm a bit stuck with the Sudoku algorithm, i coded it using backtrack, and following the theorical steps this should work, and i tried to debuge it, but is too hard (and yes, it solve some numbers and does things)

i paste the code, i hope that you can help me, i really can't see where the problem is...

public void backtracking(int row,int col){
    if(row > 8){ 
        System.out.println("Solution Found!!"); 
        printSudoku(); 

    }
    if (m[row][col] != 0){
       next(row, col);
    }
    else {
        for(int i =1; i < n;i++) 
            if(row(row, i) && col(col, i)) {
                m[row][col] =i;
                next(row, col);
            }
        m[row][col] = 0;
    }


} 

public void next( int row, int col ) {
   if( col < 8)
       backtracking( row, col + 1 ) ;
   else
       backtracking( row+ 1, 0 ) ;
}

public boolean region(int x, int y, int numReg) {
    x = (x / 3) * 3 ;
    y = (y / 3) * 3 ;
    for( int r = 0; r < 3; r++ )
        for( int c = 0; c < 3; c++ )
        if( m[x+r][y+c] == numReg )
           return false ;

     return true ;
}

public boolean row(int x, int k){
    for(int i =0; i < 9; i++)
        if(m[x][i] == k)
            return false;
    return true;
}

public boolean col(int x, int k){
    for(int i =0; i < 9; i++)
        if(m[i][x] == k)
            return false;
    return true;
}

I ommited the "printSudoku" method, is just a double for and you know.

Kev
  • 118,037
  • 53
  • 300
  • 385
Santanor
  • 566
  • 1
  • 7
  • 20
  • did you already read the questiions about backtracking and sudoku, i.e. : [this question](http://stackoverflow.com/questions/9959172/recursive-solution-to-sudoku-generator?rq=1) [or this one](http://stackoverflow.com/questions/9404673/sudoku-solver-in-java-using-backtracking-and-recursion?rq=1) [even this one](http://stackoverflow.com/questions/6432794/why-is-this-sudoku-backtracking-getting-stuck?rq=1) – icrovett Apr 22 '13 at 17:05
  • i did, and my solution is based in that questions, but it doesn't work.... i'm not asking just for fun :S – Santanor Apr 22 '13 at 18:25

1 Answers1

1

The code seems almost right. As far as i can see you just forgot to call the region method. And I can't see where the variable n is coming from. Try it with this slightly modified backtracking method:

    public static void backtracking(int row, int col) {
    if (row > 8) {
        System.out.println("Solution Found!!");
        printSudoku();
        System.exit(0); //exiting after solution is found
    }
    if (m[row][col] != 0) {
        next(row, col);
    } else {
        for (int i = 1; i <= 9; i++) //replaced i < n with i<=9
            if (row(row, i) && col(col, i) && region(row, col, i)) { //calling region method too
                m[row][col] = i;
                next(row, col);
            }
        m[row][col] = 0;
    }

}
raffael
  • 2,427
  • 4
  • 26
  • 42
  • it worked!! Thank you, but... if i may, i have other question for you. If the Sudoku is bad-builded, and i want to find all the solutions aviable? i can't simply delete the "System.exit" line.... do i have to recall the "backtracking" method? or simply modify the "row" of course i want to know what is the correct way to do it, not just "this works and i don't know why :P" – Santanor Apr 22 '13 at 18:35
  • good question. simply removing System.exit won't work because row is 9 and the next thing you do is access m[row][col]. calling backtracking(0,0) will print the same solution again. I think (not tested) you would have to add another backtracking variable witch starts the for loop at a different number: for (int j = n; j < n+ 9; j++) int i = (j%9)+1 – raffael Apr 23 '13 at 09:45
  • hi, maybe you don't care, but had to change the "next" methot to make the algotithm aviable to find all the solutions, it was easy, and not too much lines to change, but i did it!! :D – Santanor Apr 24 '13 at 16:27