Source code of the sudoku algorithm (recursion is not used)

Source: Internet
Author: User
/**
* Sudoku Program
*/
Public class shudu {
/** Array for storing numbers */
Static int [] [] n = new int [9] [9];
/** Generate the source array of random numbers. Random numbers are generated from this array */
Static int [] num = {1, 2, 4, 5, 6, 7, 8, 9 };
Public static void main (string [] ARGs ){
// Generate a number
For (INT I = 0; I <9; I ++ ){
// Number of attempts to fill
Int time = 0;
// Fill in the number
For (Int J = 0; j <9; j ++ ){
// Generate a number
N [I] [J] = generatenum (time );
// If the returned value is 0, it indicates that the request is stuck and the request is returned.
// The principle of return processing is: if it is not the first column, first go backwards to the previous column; otherwise, go backwards to the last column of the previous row.
If (N [I] [J] = 0 ){
// If the column is not the first, a backward column is returned.
If (j> 0 ){
J-= 2;
Continue;
} If else {// is the first column, it is regressed to the last column of the previous row.
I --;
J = 8;
Continue;
}
}
// Filled
If (iscorret (I, j )){
// Initialize time to prepare for the next fill
Time = 0;
} Else {// continue filling
// Increase by 1
Time ++;
// Continue filling the current grid
J --;
}
}
}
// Output result
For (INT I = 0; I <9; I ++ ){
For (Int J = 0; j <9; j ++ ){
System. Out. Print (N [I] [J] + "");
}
System. Out. println ();
}
}

/**
* Whether the row, column, and 3x3 fields are not repeated
* @ Param row
* @ Param Col column number
* @ Return true indicates that the request meets the requirements.
*/
Public static Boolean iscorret (INT row, int col ){
Return (checkrow (ROW) & checkline (COL) & checknine (row, col ));
}

/**
* Check whether the row meets the requirements.
* @ Param row indicates the row number for the row check.
* @ Return true indicates that the request meets the requirements.
*/
Public static Boolean checkrow (INT row ){
For (Int J = 0; j <8; j ++ ){
If (N [row] [J] = 0 ){
Continue;
}
For (int K = J + 1; k <9; k ++ ){
If (N [row] [J] = N [row] [k]) {
Return false;
}
}
}
Return true;
}

/**
* Check whether the column meets the requirements.
* @ Param Col check column number
* @ Return true indicates that the request meets the requirements.
*/
Public static Boolean checkline (INT col ){
For (Int J = 0; j <8; j ++ ){
If (N [J] [col] = 0 ){
Continue;
}
For (int K = J + 1; k <9; k ++ ){
If (N [J] [col] = N [k] [col]) {
Return false;
}
}
}
Return true;
}

/**
* Check whether the 3x3 region meets the requirements
* @ Param row indicates the row number for the row check.
* @ Param Col check column number
* @ Return true indicates that the request meets the requirements.
*/
Public static Boolean checknine (INT row, int col ){
// Obtain the coordinates in the upper left corner.
Int J = row/3*3;
Int K = COL/3*3;
// Compare Loops
For (INT I = 0; I <8; I ++ ){
If (N [J + I/3] [K + I % 3] = 0 ){
Continue;
}
For (int m = I + 1; m <9; m ++ ){
If (N [J + I/3] [K + I % 3] = N [J + M/3] [K + M % 3]) {
Return false;
}
}
}
Return true;
}

/**
* Generate random numbers between 1 and 9
* Rule: the generated random numbers are placed at the bottom of the array 8-time. As time increases, the number you have tried will not be retrieved.
* Note: The first time is random from all digits, the second time is random from the first eight digits, and so on,
* In this way, the random and non-conforming numbers are ensured and the program efficiency is improved.
* This rule is the core of this algorithm.
* @ Param time indicates the number of times the data is filled. 0 indicates the first time the data is filled.
* @ Return
*/
Public static int generatenum (INT time ){
// Initialize the random number source array during the first attempt
If (time = 0 ){
For (INT I = 0; I <9; I ++ ){
Num [I] = I + 1;
}
}
// If the position is stuck for 10th times, 0 is returned, Which is returned by the main program
If (time = 9 ){
Return 0;
}
// Not the first filling
// Generate a random number. The number is the subscript of the array. The number corresponding to the subscript in the array num is a random number.
Int rannum = (INT) (math. Random () * (9-Time ));
// Place the number at the last time of the array,
Int temp = num [8-time];
Num [8-time] = num [rannum];
Num [rannum] = temp;
// Return a number
Return num [8-time];
}
}

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.