Design of Sudoku games

Source: Internet
Author: User

A few days ago, I saw an interview book with a question about the generation of the sudoku game. So I wanted to implement it myself, read the code of others, and modified some of it myself, the core idea of the code is to use deep priority search. When the searched node meets the condition, search for the next point. Otherwise, the code will be rolled back. Program running diagram:


Public void getSudoku (int [] [] array, LEVEL level) {clear (array); int row = 1, check1 = 10, check2 = 10, check3 = 10; int time = 0, I = 0, j = 0; for (I = 0; I <9; I ++) {// number of attempts to fill in time = 0; // fill in the number for (j = 0; j <9; j ++) {row ++; // generate the number array [I] [j] = generateNum (time ); // if the return value is 0, it indicates that it is stuck. // the principle of return processing is: if it is not the first column, it is first regressed to the previous column, otherwise, go back to the last column of the previous row if (array [I] [j] = 0) {// solves the endless loop, it is not necessary to model 13if (row % 13 = 0) {check1 = I;} else if (row % 13 = 5) {check2 = I ;} else if (row % 13 = 11) {check3 = I;} if (check1 = check2 & check2 = check3) {I = 0; j =-1; clear (array); // clears the generated number time = 0; row = 0; continue;} // if (j> 0) is not the first column) {j-= 2; continue;} else {// is the first column, then go back to the last column I-= 1; j = 8; continue;} in the previous row ;}} // if (checkValid (array, I, j) {// initialization time, prepare for the next fill time = 0 ;} else {// continue filling // The number of times is increased by 1 time + +; // continue filling the current grid j --;}}}

The following code determines the rule of sudoku:

Public Boolean checkvalid (INT [] [] array, int row, int col) {for (INT I = 0; I <row; I ++) {// check the row if (array [I] [col] = array [row] [col]) {return false ;}} for (INT I = 0; I <Col; I ++) {// check the column if (array [row] [I] = array [row] [col]) {return false ;}} int minrow = row/3*3; int mincol = COL/3*3; for (INT I = minrow; I <= row; I ++) {// check the small square for (Int J = mincol; j <mincol + 3; j ++) {if (I! = Row & J! = Col) {If (array [I] [J] = array [row] [col]) {return false ;}} return true ;}

Program:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.