|
| 1 | +package sudoku; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class SudokuSolver { |
| 6 | + |
| 7 | + // N=9 as there will be 9 rows and 9 colums |
| 8 | + static int N = 9; |
| 9 | + |
| 10 | + static boolean solveSudoku(int grid[][], int row, int col) { |
| 11 | + // if we cross the last cell then we return true to avoid further back-tracking |
| 12 | + if (row == N - 1 && col == N) |
| 13 | + return true; |
| 14 | + // if we reach the last column then we move to the next row after setting col as 0 |
| 15 | + if (col == N) |
| 16 | + { |
| 17 | + row++; |
| 18 | + col = 0; |
| 19 | + } |
| 20 | + // check whether the cell is 0 and if it is not 0 then we move to the next column for further processing |
| 21 | + if (grid[row][col] != 0) |
| 22 | + return solveSudoku(grid, row, col + 1); |
| 23 | + |
| 24 | + // check if a no from 1 to 0=9 can be placed in a given row and column |
| 25 | + for(int no=1;no<10;no++) { |
| 26 | + if (isSafe(grid,row,col,no)) { |
| 27 | + // assigning no to the cell assuming it be correct |
| 28 | + grid[row][col] = no; |
| 29 | + |
| 30 | + // then go for the next column assuming the no assigned is correct |
| 31 | + if (solveSudoku(grid,row,col+1)) |
| 32 | + return true; |
| 33 | + } |
| 34 | + // if the assumption is wrong assign 0 to the cell |
| 35 | + grid[row][col] = 0; |
| 36 | + } |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + static boolean isSafe(int grid[][], int row, int col,int num) { |
| 41 | + // return false if the given num is found in the column |
| 42 | + for (int i=0;i<8;i++) { |
| 43 | + if (grid[i][col]==num) |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // return false if the given num is found in the row |
| 48 | + for (int i=0;i<8;i++) { |
| 49 | + if (grid[row][i]==num) |
| 50 | + return false; |
| 51 | + } |
| 52 | + // check if num is present in the 3*3 matrix |
| 53 | + int startRow = row -(row%3); |
| 54 | + int startColumn = col -(col%3); |
| 55 | + for(int i=0;i<3;i++) { |
| 56 | + for(int j=0;j<3;j++) { |
| 57 | + if (grid[i+startRow][j+startColumn]==num) |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + // display the solved sudoku |
| 65 | + static void print(int[][] grid) { |
| 66 | + System.out.println("Solved Sudoku"); |
| 67 | + for(int i=0;i<N;i++) { |
| 68 | + for(int j=0;j<N;j++) |
| 69 | + System.out.print(grid[i][j]+" "); |
| 70 | + System.out.println(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static void main(String[] args) { |
| 75 | + // TODO Auto-generated method stub |
| 76 | + Scanner sc = new Scanner(System.in); |
| 77 | + int[][] grid = new int[N][N]; |
| 78 | + System.out.println("Enter the Sudoku to be solved"); |
| 79 | + // enter the matrix |
| 80 | + for(int i=0; i<N;i++) { |
| 81 | + for(int j=0;j<N;j++) { |
| 82 | + grid[i][j]=sc.nextInt(); |
| 83 | + } |
| 84 | + } |
| 85 | + // check if solution exists |
| 86 | + if(solveSudoku(grid,0,0)) |
| 87 | + print(grid); |
| 88 | + else |
| 89 | + System.out.println("Solution does not exist"); |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments