In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to realize Sudoku Game in C language". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to realize Sudoku Game in C language" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
target
Write a Sudoku game with the following functions:
1: can randomly generate questions and give answers.
2: solve the input problem and output the answer.
Implementation description
With reference to Baidu encyclopedia and other data, we can know that the main algorithms for solving Sudoku are: 1. Determine the number that can be filled in by row, column, and grid. two。 Fill in all the feasible figures one by one to get the results. This program also uses this algorithm to solve the Sudoku part. The algorithm for generating questions is:
Randomly generate an one-dimensional array of length 9, and the elements are randomly generated different numbers from 1 to 9.
For example, root = [1, 4, 3, 5, 6, 7, 8, 9, 2].
1. Get a full Nine Palace accord first.
2. If the first behavior of the nine-grid accord [6, 4, 5, 7, 3, 9, 8, 1, 2], the first row of elements of the nine-grid squa can be generated in this way: if the corresponding element of accord is 6, then the number of the last digit of 6 in root is 7, then the number obtained is 7. and so on.
3. The randomly generated Sudoku problem is obtained by randomly removing a certain number of spaces according to the difficulty.
The main difficulties and innovations of this program are described below:
1. The recursive method of solving Sudoku. First of all, we need to define a check function judge to determine whether a number is appropriate in a certain position, and then divide whether it is 0 or not according to the recursive header found, that is, after the last position, otherwise, continue recursion.
2. Randomly generate a number between 1 and 9. Because the compiler has its own function rand, the exact point of element invariance will appear, so the system time is used as the seed, and the global variable index is introduced to increase the time difference and avoid being fixed.
3. Define various functions to simplify the program. This procedure defines fprintf for printing Nine Palace grids, defines reRank to get the location of elements in the array, etc., simplifies the procedure.
Result
The following is the result diagram after selecting 1 (generating problems) and 2 (calculating Sudoku) respectively:
Code:
Language: C
/ / final experiment report-simple Sudoku calculator # include # include # include long index = 0; / / Global variable index guarantees the randomness of random elements generated each time: void main () {void solve (int squa [9] [9], int num); void fprintf (int squa [9] [9]); int reRank (int root [9], int numb); int myRand (int range) Int squa [9] [9], accord [9] [9] = {/ / define the reference array {6, 4, 5, 7, 3, 9, 8, 1, 2}, {2, 1, 8, 6, 6, 7, 3}, {7, 3, 9, 2, 8, 1, 6, 4, 5}, {5, 9, 6, 3, 7, 4, 8. 1}, {4, 8, 7, 5, 1, 2, 3, 6, 9}, {3, 2, 1, 8, 9, 6, 7, 5, 4}, {9, 5, 3, 4, 6, 7, 1, 2, 8}, {8, 7, 2, 1, 5, 3, 4, 9, 6}, {1, 6, 4, 9, 2, 8, 5, 3, 7}} Int root [9], answ [9] [9]; / / define one-dimensional array and temporary array int I, j, k, p = 0; int row; int choose, rtemp, diff; / / define selection variables, respectively, difficulty char result; printf ("- Please select * 1* to generate a Sudoku-- * 2* to calculate a Sudoku--\ n") Scanf ("% d", & choose); / / * Select 1 to generate a filled Sudoku * * / / first deposit a Sudoku if (1 = = choose) {/ / generate an one-dimensional array root for (I = 0; I < 9) in the range 1-9 according to the generating Sudoku problem algorithm For +) {root [I] = myRand (9);} for (I = 0; I < 9; iBo +) {for (j = 0; j < I; jot +) {if (root [I] = = root [j]) / / make the elements in root different root [I] = myRand (9) }} / / randomly generate a complete Nine Miyagi for
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.