Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report