Sudoku

Sort by

recency

|

49 Discussions

|

  • + 0 comments

    **what's the problem with this submission they mentioned that,

    If there are multiple solutions, you can output any one ?**

    using namespace std;
    
    
    bool isOk(vector<vector<int>> &v,int i,int j,int l){
        //checking row and column simultaneously
        for(int y=0;y<9;y++)if(v[i][y]==l || v[y][j]==l)return 0;
    
        i=i-i%3;j=j-j%3;
    
        //check sub matrix of 3 x 3
        for(int y=0;y<3;y++)for(int k=0;k<3;k++)if(v[i+y][j+k]==l)return 0;
        return 1;
    }
    
    void f(vector<vector<int>> &v,string &s){
        if(s=="11")return ;
        int i,j;
        for(i=0;i<9;i++){
    	    int z=0;
            for(j=0;j<9;j++)if(v[i][j]==0){
    		z=1;break;
    	}
    	if(z)break;
        }
    
        if(i>=9 && j>=9){s="11";return ;}
    
        for(int l=1;l<=9;l++){
            if(isOk(v,i,j,l)){
                v[i][j]=l;
                f(v,s);
               // cout<<"** // ";
            }
            if(s=="11")return ;
            else v[i][j]=0;
        }
    }
    
    //@1****34$1*
    
    int main(){
        int t;cin>>t;
        while(t--){
            vector<vector<int>>v(9,vector<int>(9));
            for(int i=0;i<9;i++)for(int j=0;j<9;j++)cin>>v[i][j];
            string s="00";
            f(v,s);
    
            if(s=="00"){
                cout<<"Lol! ,we regret to inform you that , there is no solution for your input ";return 0;
            }
    
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++)cout<<v[i][j]<<" ";
                cout<<"\n";
            }
        }
    }
    
  • + 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.

  • + 0 comments

    I've passed all the test cases except Test Case 0. It doesn't want to accept any valid solution. So the task is broken, and you should probably avoid it. My rejected solution: 4 5 7 8 9 6 2 1 3 2 6 1 4 5 3 7 8 9 3 8 9 2 7 1 5 6 4 6 9 2 1 4 8 3 5 7 7 4 8 5 3 2 6 9 1 5 1 3 7 6 9 4 2 8 1 3 5 6 8 4 9 7 2 8 7 4 9 2 5 1 3 6 9 2 6 3 1 7 8 4 5

  • + 0 comments

    stupid system. wasted all my time

  • + 0 comments

    I just got stuck and got the wrong solution after so many attempts but your post helped me. lemon break up spells