Project requirement: Use a program to randomly construct n answered Sudoku boards
Input: Number of sudoku board questions N (0 <n <= 1000000)
Output: randomly generate N non-repeated answer-completed Sudoku boards, and output them to sudotiku.txt, each of which is separated by one row.
# Include <iostream> # include <cstdlib> # include <ctime> # include <fstream> using namespace STD; ofstream ocout; int sudo [9] [9]; bool set (int x, int y, int Val) {If (SUDO [x] [Y]! = 0) // non-empty return false; int x0, y0; For (Y0 = 0; y0 <9; y0 ++) // line conflict {If (SUDO [x] [y0] = Val) return false;} For (X0 = 0; x0 <9; x0 ++) // column conflict {If (SUDO [x0] [Y] = Val) return false;} For (X0 = x/3*3; x0 <X/3*3 + 3; x0 ++) // grid conflict {for (Y0 = y/3*3; y0 <Y/3*3 + 3; y0 ++) {If (SUDO [x0] [y0] = Val) return false ;}} sudo [x] [Y] = val; return true ;} void current (int * cur) // 0 ~ 9 random sequence {int I, j, temp; For (INT I = 0; I <9; I ++) {cur [I] = I ;} for (INT I = 0; I <9; I ++) {J = rand () % 9; temp = cur [J]; cur [J] = cur [I]; cur [I] = temp;} void reset (INT X, int y) {sudo [x] [Y] = 0 ;} bool fill (int x, int Val) {int cur [9]; current (cur); // generates the scanning sequence of the current row for (INT I = 0; I <9; I ++) {int y = cur [I]; If (SET (X, Y, Val) // you can enter 1 in each row first, enter 2 in each row... and so on {If (x = 8) // to the last row {If (val = 9 | fill (0, Val + 1) // It ends when the current value is 9, otherwise, enter the next number from the first line, return Tru. E;} else {If (fill (x + 1, Val) // return true for the next row;} reset (x, y ); // backtracking} return false;} void clear (INT temp [9] [9]) // clear {for (INT I = 0; I <9; I ++) {for (Int J = 0; j <9; j ++) {temp [I] [J] = 0 ;}} void printsudo () // output to the screen {for (INT x = 0; x <9; X ++) {(X % 3 = 0 )? (Cout <"--------------------- \ n") :( cout <""); cout <"|"; for (INT y = 0; y <9; y ++) {cout <sudo [x] [Y] <""; (Y % 3 = 2 )? (Cout <"|") :( cout <"") ;}cout <Endl;} cout <"--------------------- \ n" <Endl;} void printsudotxt () // output to sudotiku.txt {for (INT x = 0; x <9; X ++) {(X % 3 = 0 )? (Ocout <"--------------------- \ n") :( ocout <""); ocout <"|"; for (INT y = 0; y <9; y ++) {ocout <sudo [x] [Y] <""; (Y % 3 = 2 )? (Ocout <"|") :( ocout <"") ;}ocout <Endl ;}ocout <"------------------- \ n" <Endl ;}int main () {srand (unsigned) Time (null); // This is the seed function that provides different seeds for the rand function to generate different random numbers for each program running, otherwise, the random numbers generated by the rand function each time the program is run are the same ocout. open ("sudotiku.txt"); cout <"Enter N (0 <n <= 1000000):" <Endl; int N; CIN> N; cout <"randomly generate" <n <"the answer-not-repeated Sudoku Board is as follows:" <Endl; ocout <"randomly generate" <n <"a number of non-repeated answer-completed Sudoku boards are as follows:" <Endl; For (INT I = 0; I <n; I ++) {Clear (SUDO); While (! Fill (0, 1); printsudo (); printsudotxt ();} ocout. Close (); Return 0 ;}
The program running result is as follows:
The following figure shows a random generation of three non-repeated answer-completed Sudoku boards:
-----------------------
| 4 1 7 | 3 8 2 | 9 5 6 |
| 2 9 3 | 6 4 5 | 8 7 1 |
| 6 8 5 | 7 9 1 | 2 4 3 |
-----------------------
| 1 7 6 | 5 3 8 | 4 9 2 |
| 8 2 4 | 9 6 7 | 3 1 5 |
| 3 5 9 | 2 1 4 | 6 8 7 |
-----------------------
| 9 3 1 | 4 5 6 | 7 2 8 |
| 5 6 2 | 8 7 9 | 1 3 4 |
| 7 4 8 | 1 2 3 | 5 6 9 |
-----------------------
-----------------------
| 6 7 3 | 1 9 2 | 5 4 8 |
| 2 8 1 | 6 5 4 | 9 7 3 |
| 5 4 9 | 8 7 3 | 2 6 1 |
-----------------------
| 3 9 8 | 4 6 1 | 7 5 2 |
| 4 6 7 | 2 8 5 | 3 1 9 |
| 1 5 2 | 7 3 9 | 4 8 6 |
-----------------------
| 8 2 4 | 3 1 7 | 6 9 5 |
| 7 1 5 | 9 2 6 | 8 3 4 |
| 9 3 6 | 5 4 8 | 1 2 7 |
-----------------------
-----------------------
| 3 4 9 | 6 7 8 | 5 2 1 |
| 7 8 1 | 2 4 5 | 9 3 6 |
| 6 2 5 | 3 1 9 | 8 4 7 |
-----------------------
| 8 5 3 | 4 6 2 | 1 7 9 |
| 2 9 7 | 8 5 1 | 4 6 3 |
| 4 1 6 | 9 3 7 | 2 5 8 |
-----------------------
| 1 3 2 | 7 9 4 | 6 8 5 |
| 5 6 8 | 1 2 3 | 7 9 4 |
| 9 7 4 | 5 8 6 | 3 1 2 |
-----------------------
After testing, the program can run correctly and the running time is short.
Through this assignment, I not only learned about the sudoku game, but also had a deeper understanding of the use of backtracking, some of the knowledge points I usually do not know well, and the errors I encountered during the running of the program, can be well corrected. It took me several days to complete this program. Because I had no idea, I had to go online to check the information. During the subsequent operation, I encountered the following problems:
1. srand (unsigned) Time (null) must be added at the beginning of the main function. This seed function provides different seeds for the rand function. Different random numbers are generated each time a program is run, otherwise, the rand function will generate the same random number each time it runs the program;
2. output the program running result to sudotiku.txt and ofstream ocout. The statement must start;
3. The use of backtracking methods requires clear thinking.
These problems are solved through the experience of Baidu and others.
I think the skills that are particularly important to me include the following:
1. Program understanding (how to understand existing programs by reading, analyzing, and debugging)
2. architecture design, modular design, and interface design
3. Implement and Gradually refine modules
4. Performance Analysis and Improvement
5. Between threads/between processes on different platforms
6. Personal software process: Estimate, record workload, and gradually increase
At present, my professional knowledge, skills, and abilities are all in the basic stage. I have mastered the basic written knowledge and practical skills. I hope that after completing this course, you can master the basic theoretical and practical knowledge and pass the interviews of General related enterprises.
Use a program to randomly construct n answered Sudoku boards