Sudoku

  • + 0 comments

    I have solved the question in java language and it is giving a valid output but it is different from the output in the given test cases. Can anybody figure out the issue with my code?

    import java.util.*;
    
    public class Solution {
        static boolean st = true;
        public static void displaySudoku(int[][] sudoku, int start, int end) {
            for(int i=start; i<end; i++) {
                for(int j=0; j<9; j++) {
                    System.out.print(sudoku[i][j]+" ");
                }
                System.out.println("");
            }    
        }
        
        public static void sudokuSolver(int[][] sudoku, int[] rows, int[] cols, int[][] grid, int i, int j, int max) {
            if(i == max) {
                if(st) {
                    displaySudoku(sudoku, max-9, max);
                    st = false;
                }
                return;
            }
            
            if(sudoku[i][j] > 0) {
                sudokuSolver(sudoku, rows, cols, grid, j == sudoku[0].length-1? i+1 : i, 
                j == sudoku[0].length-1? 0 : j+1, max);
            }
            else {
                for(int num=1; num<=9; num++) {
                    if(!st) return;
                    
                    if( (rows[i] & (1 << num)) == 0 && 
                        (cols[j] & (1 << num)) == 0 &&
                        (grid[i/3][j/3] & (1 << num)) == 0 ) {
                            rows[i] ^= (1 << num);
                            cols[j] ^= (1 << num);
                            grid[i/3][j/3] ^= (1 << num);
                            sudoku[i][j] = num;
                            sudokuSolver(sudoku, rows, cols, grid, 
                                        j == sudoku[0].length-1? i+1 : i, 
                                        j == sudoku[0].length-1? 0 : j+1, max);
                            rows[i] ^= (1 << num);
                            cols[j] ^= (1 << num);
                            grid[i/3][j/3] ^= (1 << num);
                            sudoku[i][j] = 0;
                        }
                }
            }
        }
        
        public static void main(String[] args) {
            int N;
            
            Scanner sc = new Scanner(System.in);
            N = sc.nextInt();
            
            int[][] sudoku = new int[N*9][9];
            int[] rows = new int[N*9];
            int[] cols = new int[9];
            int[][] grid = new int[N*3][3];
            
            
            int num;
            for(int i=0; i<N*9; i++) {
                for(int j=0; j<9; j++) {
                    num = sc.nextInt();
                    sudoku[i][j] = num;
                    rows[i] |= (1 << num);
                    cols[j] |= (1 << num);
                    grid[i/3][j/3] |= (1 << num);
                }
            }
            
            int x = 1;
            for(int i=0; i<x*9; i+=9) {
                st = true;
                sudokuSolver(sudoku, rows, cols, grid, i, 0, i+9);
                
                if(x < N) x++;
            }
            
            sc.close();
        }
    }
    

    Input:

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

    My Ouput:

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

    Expected Output:

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

    As you can see although my output is valid but it is different from expected output.