Valid Sudoku in C++

C++Server Side ProgrammingProgramming

Let’s suppose we’ve given a 9×9 matrix called a Sudoku. The task is to check whether the given Sudoku Pattern is valid or not.

In general, a Sudoku board look like this,

Rules of Sudoku

  • Every row contains a number in the range 1-9

  • Every column contains numbers in the range 1-9.

  • Each block of 3×3 contains unique numbers in it.

  • A particular row cannot have the same number.

  • A particular column cannot have the same number.

For Example

Input-1

sudoku[]=
   [["3","5",".",".","2",".",".",".","."]
   ,["7",".",".","1","6","5",".",".","."]
   ,[".","9","8",".",".",".",".","6","."]
   ,["8",".",".",".","6",".",".",".","3"]
   ,["4",".",".","5",".","4",".",".","1"]
   ,["7",".",".",".","2",".",".",".","6"]
   ,[".","6",".",".",".",".","2","8","."]
   ,[".",".",".","4","1","9",".",".","5"]
   ,[".",".",".",".","8",".",".","7","9"]]

Output − True.

Explanation − Since all the numbers inside the Sudoku matrix follow the pattern of a valid Sudoku, the output is True.

Approach to solve this problem

Initially we will check if the given Sudoku board has the column with unique numbers or not. Then we will check for the row. Each 3*3 block contains all the numbers unique in it. We will check each of the block row and block column if it contains any number duplicate we will return false otherwise return true.

  • Take input of a 2-D array for the Sudoku board.

  • A Boolean function to check the row whether the elements present in it are unique is not.

  • A Boolean function to check the column whether the elements present in it are unique is not.

  • A Boolean function to check the block whether the elements present in it are unique is not.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
bool validSudoku(vector<vector<char>>& sudoku) {
   int row = 0, col = 0, i = 0, block = 0;
   int count[9];
   for (row = 0; row < 9; ++row){
      memset(count, 0, 9 * sizeof(int));
      for (col = 0; col < 9; ++col){
         if (sudoku[row][col] != '.')
            ++count[sudoku[row][col]-'1'];
      }
      for (i = 0; i < 9; ++i)
         if (count[i] > 1)
            return false;
   }
   for (col = 0; col < 9; ++col){
      memset(count, 0, 9 * sizeof(int));
      for (row = 0; row < 9; ++row){
         if (sudoku[row][col] != '.')
            ++count[sudoku[row][col]-'1'];
      }
      for (i = 0; i < 9; ++i)
         if (count[i] > 1)
            return false;
   }
   int block_row = 0, block_col = 0;
   for (block = 0; block < 9; ++block){
      block_row = (block / 3) * 3, block_col = (block % 3) * 3;
      memset(count, 0, 9 * sizeof(int));
      for (row = block_row; row < (block_row + 3); ++row)
      for (col = block_col; col < (block_col + 3); ++col)
         if (sudoku[row][col] != '.')
            ++count[sudoku[row][col] - '1'];
      for (i = 0; i < 9; ++i)
            if (count[i] > 1)
         return false;
   }
   return true;
}
int main(){
   vector<vector<char> > sudoku= {
      {'5','3','.','.','7','.','.','.','.'},
      {'6','.','.','1','9','5','.','.','.'},
      {'.','9','8','.','.','.','.','6','.'},
      {'8','.','.','.','6','.','.','.','3'},
      {'4','.','.','8','.','3','.','.','1'},
      {'7','.','.','.','2','.','.','.','6'},
      {'.','6','.','.','.','.','2','8','.'},
      {'.','.','.','4','1','9','.','.','5'},
      {'.','.','.','.','8','.','.','7','9'}
   };
   bool ans= validSudoku(sudoku);
   if(ans){
      cout<<"True"<<endl;
   } else {
      cout<<"false"<<endl;
   }
   return 0;
}

Output

True
raja
Published on 05-Feb-2021 10:49:20
Advertisements