Validity of a sudoku

Check if given sudoku is a valid one or not

We were given the 9X9 matrix with the blankspaces filled with zero. We need to check if that configuration can lead to solving the puzzle or not.



 //submitted  
 //https://practice.geeksforgeeks.org/problems/is-sudoku-valid/0  
 #include <stdio.h>  
 #include<stdlib.h>  
 #include <string.h>  
 #include <math.h>  
 #define lim 1000001  
 int arr[9][9];  
 int func()  
 {  
      int i,j,x,y;  
      for(i=0;i<9;i++)  
      {  
           for(j=0;j<9;j++)  
           {  
                scanf("%d",&arr[i][j]);  
           }  
      }  
      int *check;  
      check=(int*)calloc(10,sizeof(int));  
      //row wise  
      int row;  
      for(row=0;row<9;row++)  
      {  
           memset(check,0,10*sizeof(int));  
           for(j=0;j<9;j++)  
           {  
                check[arr[row][j]]++;  
           }  
           for(j=1;j<=9;j++)  
           {  
                if(check[j]>1)  
                {  
                     return 0;  
                }  
           }  
      }  
      //column wise  
      int column;  
      for(column=0;column<9;column++)  
      {  
           memset(check,0,10*sizeof(int));  
           for(i=0;i<9;i++)  
           {  
                check[arr[i][column]]++;  
           }  
           for(i=1;i<=9;i++)  
           {  
                if(check[i]>1)  
                {  
                     return 0;  
                }  
           }  
      }  
      //boxwise  
      for(row=0;row<9;row=row+3)  
      {  
           for(column=0;column<9;column=column+3)  
           {  
                memset(check,0,10*sizeof(int));  
                for(i=0;i<3;i++)  
                {  
                     for(j=0;j<3;j++)  
                     {  
                          x=row+i;  
                          y=column+j;  
                          check[arr[x][y]]++;  
                     }  
                }  
                for(i=1;i<=9;i++)  
                {  
                     if(check[i]>1)  
                     {  
                          return 0;  
                     }  
                }  
           }  
      }  
      return 1;  
 }       
 int main()  
 {  
      int cases;  
      scanf("%d",&cases);  
      while(cases--)  
      {  
           printf("%d\n",func() );  
      }  
      return 0;  
 }  

VALID SUDOKU:

  1. No row can have a number more than once
  2. No column can have a number more than once
  3. No 3X3 matrix (I mean each block) can have a number more than once

The Process:

Using the above mentioned rules for a valid sudoku, we process each rule and check if every rule is valid or not.

Thanks for visiting the site...

If you wish get the updates in facebook, you can like our page.



Comments

Popular

Traversal In A Binary Tree - Tree -3

Tree data structure - 2

Pre Order Traversal In Binary Tree