Problem

(Checking Sudoku solution) Listing checks whether a solution is valid by checking whether...

(Checking Sudoku solution) Listing checks whether a solution is valid by checking whether every number is valid in the board. Rewrite the program by checking whether every row, every column and every small box has numbers 1 to 9

CheckSudokuSolution.cpp

#include using namespace std;void readASolution(int grid[][9 ]);bool isValid(const int grid[][9]);bool isValid(int i, int j, const int grid[][9]);int main(){   // Read a Sudoku puzzle   int grid[9 ][9 ];   readASolution(grid);   cout << (isValid(grid) ? "Valid solution" : "Invalid solution");
   return 0;}// Read a Sudoku puzzle from the keyboardvoid readASolution(int grid[][9 ]){   cout << “Enter a Sudoku puzzle:” << endl;   for (int i = 0; i < 9; i++)    for (int j = 0; j < 9; j++)      cin >> grid[i][j];}// Check whether the fixed cells are valid in the gridbool isValid(const int grid[][9 ]){   for (int i =0 ; i < 9 ; i++)     for (int j = 0 ; j < 9 ; j++)   if (grid[i][j] < 1 || grid[i][j] > 9 ||      !isValid(i, j, grid))         return false ;return true ; // The fixed cells are valid}// Check whether grid[i][j] is valid in the gridbool isValid(int i, int j, const int grid[][9 ]){   // Check whether grid[i][j] is valid at the i’s row     for (int column = 0; column < 9; column++)      if (column != j && grid[i][column] == grid[i][j])        return false ;   // Check whether grid[i][j] is valid at the j's column     for (int row = 0; row < 9; row++)      if (row != i && grid[row][j] == grid[i][j])          return false ;   // Check whether grid[i][j] is valid in the 3-by-3 box     for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)         for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)          if (row != i 1 to 9

CheckSudokuSolution.cpp

#include using namespace std;void readASolution(int grid[][9 ]);bool isValid(const int grid[][9]);bool isValid(int i, int j, const int grid[][9]);int main(){   // Read a Sudoku puzzle   int grid[9 ][9 ];   readASolution(grid);   cout << (isValid(grid) ? "Valid solution" : "Invalid solution");
col != j && grid[row][col] == grid[i][j])            return false ;return true ; // The current value at grid[i][j] is valid}

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT