Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Fast solution of Sudoku in C++

Shulou Source: shulou.com Published: 2022-06-01 03:45:06 09月18日 Update

This article mainly introduces the relevant knowledge of "how to realize the Fast solution of Sudoku by C++". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to realize the Fast solution of Sudoku on C++" can help you solve the problem.

What is Sudoku?

Sudoku is a math game originated in Switzerland in the 18th century. It is a logic game that uses paper and pen for calculus. Players need to deduce the numbers of all the remaining spaces according to the known numbers on the 9 × 9 disk, and satisfy that the numbers in each row, each column, and each thick line house (3x3) contain 1-9, and do not repeat.

The Sudoku disk is a ninth house, and each house is divided into nine compartments. Some known numbers and problem-solving conditions are given in these 81 boxes, and the numbers of 1-9 are filled in other blanks by logic and reasoning. So that each number from 1 to 9 appears only once in every row, column and palace, so it is also called "Nine Palace Grid".

Solution idea

1. Traverse the Sudoku table to find the table whose number is empty (filled with 0)

2. Find out the numbers that can be filled in each table with hollow data

3. Find the table with the least number of numbers that can be filled in

4. Fill in each number into the table separately

5. Repeat steps 1-4 recursively until there is no table with the number 0 in the table

# include # include using namespace std;struct Position {int row; int col; int * res;}; Position* findMinBlank (int board [] [9]) {int * validNums (int board [] [9], int row, int col); Position* pos = new Position (); pos- > res = 0; int * res; int total=0, minum = 10; for (int iTuno; ires = res; minum = total) } else delete [] res;} return pos;} int * validNums (int board [] [9], int row, int col) {int * res = new int [9] {1, 2, 3, 4, 5, 7, 8, 9}; for (int I = 0; I < 9; iBoard +) {res [row] [I]-1] = 0 Res [col [I] [col]-1] = 0;} int p = col / 3 * 3; int Q = col / 3 * 3; for (int x = p; x < p + 3; x int +) for (int y = Q; y < Q + 3; YBO +) {res [Boardx] [y]-1] = 0;} return res } void printResult (int result [] [9]) {for (int I = 0; I < 9; iTunes +) {for (int j = 0; j < 9; jacks +) {cout

Tags: Number table floor knowledge row ninth house disk space industry logic reasoning different practical century number content also known as practical practical small grid Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Shulou Information NVidia Docker