Anoo
Anoo

Reputation: 113

Solving Sudoku program

I have a problem here. I am not getting perfect output. The following is my code that I wrote to prepare Sudoku. Even I know the reason for it, as it is unable to create any new unique number, it is printing the default value that is 0.I know I'm missing something that I can't think of. Can anyone suggest a solution for it? Thanks in advance.

public class FinalSudoku 
{
    int a[][]=new int[9][9];
    public void initialize1()
    {
        for(int i=0;i<9;i++)
        {
         for(int j=0;j<9;j++)
        {
             a[i][j]=0;
        }

     }
    }
    protected boolean detectRow( int row, int num )
       {
          for( int col = 0; col < 9; col++ )
             if( a[row][col] == num )
                return false;

          return true ;
       }

    protected boolean detectCol( int col, int num )
       {
          for( int row = 0; row < 9; row++ )
             if( a[row][col] == num )
                return false ;

          return true ;
       }

    protected boolean detectBox( int row, int col, int num )
       {
          row = (row / 3) * 3 ;
          col = (col / 3) * 3 ;

          for( int r = 0; r < 3; r++ )
             for( int c = 0; c < 3; c++ )
             if( a[row+r][col+c] == num )
                return false ;

          return true ;
       }

    public void solve( int row, int col ) throws Exception
    {
        if( row > 8 )
             throw new Exception( "Solution found" ) ;

        if( a[row][col] != 0 )
             next( row, col ) ;
          else
          {
    for( int num = 1; num < 10; num++ )
    {
       if(detectRow(row,num) && detectCol(col,num) && detectBox(row,col,num) )
       {
          a[row][col] = num ;
          next(row, col) ;
       }
    }
          }
    }
    public void display()
    { 
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
    }

    public void next( int row, int col ) throws Exception
       {
          if( col < 8 )
             solve( row, col + 1 ) ;
          else
             solve( row + 1, 0 ) ;
       }  
    public static void main(String[] args) throws Exception
    {
        FinalSudoku fs = new FinalSudoku();
        fs.initialize1();

        fs.solve(0,0);

        fs.display();
    }

}

Output of this code:

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 3 2 1
7 8 9 1 2 3 4 5 6
2 1 4 3 6 5 8 9 7
3 6 7 2 9 8 1 4 5
5 9 8 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

Upvotes: 2

Views: 7978

Answers (2)

Rishabh Rawat
Rishabh Rawat

Reputation: 1204

This Code Check the Sudoku.If it is correct then check_sudoku() method return true if it wrong then shows the row and column number which has a duplicate element.

  public static void main(String[] args) {
    int array[][]={{9,6,3,1,7,4,2,5,8},
                   {1,7,8,3,2,5,6,4,9},
                   {2,5,4,6,8,9,7,3,1},
                   {8,2,1,4,3,7,5,9,6},
                   {4,9,6,8,5,2,3,1,7},
                   {7,3,5,9,6,1,8,2,4},
                   {5,8,9,7,1,3,4,6,2},
                   {3,1,7,2,4,6,9,8,5},
                   {6,4,2,5,9,8,1,7,3}};

    Sudoku sudoku=new Sudoku();
    if(sudoku.check_sudoku(array))
    {
        System.out.println("You won the game :)");
    }


}


public class Sudoku {

private int temp1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, temp2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
private int data1, data2;

public boolean check_sudoku(int array[][]) {
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            data1 = array[i][j];
            data2 = array[j][i];
            if (data1 >= 10 || data2 >=10 || data1 <= 0 || data2 <= 0) {
                System.out.println("Invalid Solution because value must be in between 1 to 9");
                return false;
            } else if (temp1[data1 - 1] == 0 || temp2[data2 - 1] == 0) {
                System.out.println("Invalid Solution please check " + (i + 1) + " row " + (j + 1) + " column or " + (j + 1) + " row " + (i + 1) + " column");
                return false;
            } else {
                temp1[data1 - 1] = 0;
                temp2[data2 - 1] = 0;
            }
        }
        int check1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int check2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        temp1 = check1;
        temp2 = check2;
    }
    return true;
}

}

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68907

You're not writing a solver, but a generator. You're problem is that you fill in values, without really checking if they will block the puzzle. Look at the place where your algo stopped.

1 2 3  4 5 6  7 8 9
4 5 6  7 8 9  3 2 1
7 8 9  1 2 3  4 5 6

2 1 4  3 6 5  8 9 7
3 6 7  2 9 8  1 4 5
5 9 8  0 0 0  0 0 0 < This row contains 5 and 8, 9

0 0 0  0 0 0  0 0 0
0 0 0  0 0 0  0 0 0
0 0 0  0 0 0  0 0 0
       ^
 This column contains 1,2,3,4 and 7

 And the center box, contains 6

So all numbers are taken for that place.


Take a look here: Wiki: Sudoku algorithms: Blank Sudoku grids
This code produces a filled grid, maybe that is interesting to start with?


Thanks to Alex D, for his good point:

In cases where you do really want to use a brute force algorithm like you are trying to do, you need to make it back up and try a different solution when it reaches a point where it can't succeed. If it backs all the way up to the beginning, with no choices left to try, then there is no solution. There are a few standard ways to implement such a "recursive search with backtracking".

This solving algorithm will work, but it will require some knowledge of recursion.

Upvotes: 6

Related Questions