Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me write a program to solve this Sudoku using C++ language rules are as follows: 1. In a 9 by 9 matrix containing

Please help me write a program to solve this Sudoku using C++ language

rules are as follows:

1. In a 9 by 9 matrix containing 81 cells, number to enter are from 1 to 9

2. The numbers in each column must not be repeated

3. The number in each row must not be repeated

4. When the square is divided into nine blocks of 3 by 3 cells, the number in each block must be unique.

5. The given numbers (beside 0) in sudo.txt cannot be changed (check below).

Functions part:

1. Validity check: Check if the "current cell" numbers meets all the rules of Sudoku.

2. The next number:

Increases the number of existing "Current cell" by 1. And test whether it can pass the Validity check.

If it fails to pass, increase it by 1 and check again.

If you find one of them can pass the Validity check, return true.

If it has exceeded 9 and passed the correctness check, return false.

3. Next cell:

From the "current cell" move to the next cell. If the value of the next cell is already specified for the topic, move one more step to the next one.

If the scope of all 81 cells is exceeded, return false; otherwise,return true.

4. Previous cell: Set the value of Current Cell to 0, then back to the previous cell.

If the value of the previous cell is already specified for the topic, then go back one more step.

Return false if it has returned more than the original one, otherwise return true.

5. Solve:

(1)- In the workspace, fill in the numbers thar has been assigned .

If he topic does not specify the number for a cell, fill in 0.

(2)- Set (0, 0) to "Current cell"

(3)- Repeat this step until the end: If the "next number" returns true, then call "Next cell". If "Next cell" returns false, the solution is completed; the end after the output.

On the other hand, if the "next number" return false, call "previous cell".

If the "previous cell" returns false, it is judged that it cannot be solved and it ends.

Sudo.txt

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

Students also viewed these Databases questions