Java using backtracking method to solve Sudoku example _java

Source: Internet
Author: User

Copy Code code as follows:

Import Java.util.Calendar;
Import Java.util.Date;

public class Matrix {

private int matrix[][];
Private long timeafter=0;
Private long Timebefore = 0;
Public Matrix (int m[][]) {
Matrix = new Int[9][9];
for (int i=0; i<9; i++)
for (int j= 0; j<9; j + +)
MATRIX[I][J]=M[I][J];
This.timebefore = Calendar.getinstance (). Gettimeinmillis ();
}
public void BackTrack (int i, int j)
{
Reclaim System memory Resources
System.GC ();
if (i==8 && j>=9)
{
This.timeafter = Calendar.getinstance (). Gettimeinmillis ();
Successful output matrix
This.showmatrix ();
Return
}

if (j = = 9) {j = 0; i++;}

if (matrix[i][j] = = 0)
{
Number is zero
for (int k=1; k<=9; k++)
{
if (bound (i,j,k))
{
MATRIX[I][J] = k;
Match the criteria to find the next square
BackTrack (i,j+1);
MATRIX[I][J] = 0;
}
}
}else
{
Number is not zero, directly find the next
BackTrack (i, j+1);
}
}
/**
* To determine whether the numbers to be filled and the number of peers and figures in the same 19 house are duplicated
*/
Private Boolean bound (int i, int j, int k) {
int m = I/3;
int n = J/3;
for (int p = 0; p<9; p++)
{
if (k = = Matrix[i][p])
{
return false;
}
if (k = = Matrix[p][j])
{
return false;
}
if (k = = matrix[3*m+p/3][3*n+p%3])
{
return false;
}
}
return true;
}
/**
* Print Solution Time
* @return
*/
Public long Printtime ()
{
return this.timeafter-this.timebefore;
}
/**
* Print Matrix
*/
public void Showmatrix ()
{
for (int i=0; i<9; i++)
{
for (int j=0; j<9; j + +)
{
System.out.print (matrix[i][j]+ "");
}
System.out.println ();
}
System.out.println ();
SYSTEM.OUT.PRINTLN ("Solution Time:" +printtime () + "millisecond");
System.out.println ();
}
public static void Main (string[] args) {
int matrix[][] = {
{3,0,6,0,5,7,0,0,0},
{7,9,0,0,2,4,0,0,0},
{0,5,0,6,0,0,9,7,4},
{8,0,1,0,0,9,0,0,0},
{0,2,0,3,0,8,0,0,7},
{4,0,0,0,6,0,5,0,0},
{0,0,4,0,3,6,0,5,0},
{2,0,3,7,0,5,0,0,1},
{0,0,7,4,1,0,6,0,0}};

int ma1[][]={
{0,3,0,0,0,5,0,6,0},
{0,1,0,0,0,3,0,8,0},
{0,4,0,0,0,0,0,0,7},
{0,0,7,0,2,4,0,0,0},
{5,0,0,0,9,0,0,0,0},
{0,8,0,3,0,0,5,0,0},
{0,0,0,8,0,0,0,0,0},
{0,0,9,0,0,0,0,7,3},
{0,5,0,9,0,0,0,0,2}};

int ma2[][]={
{0,0,0,0,8,4,0,0,0},//8
{0,0,0,2,0,3,0,8,0},
{8,3,0,9,0,0,0,5,0},
{0,5,3,0,9,0,7,0,0},
{0,0,0,6,3,7,0,4,5},//7
{0,7,0,5,0,0,0,0,0},
{0,0,6,8,0,0,0,0,0},
{3,0,0,0,2,9,0,0,0},
{2,0,9,3,0,0,0,0,1}};//3

The most difficult Sudoku in the world
Int[][] Sudoku = {
{8, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 3, 6, 0, 0, 0, 0, 0},
{0, 7, 0, 0, 9, 0, 2, 0, 0},
{0, 5, 0, 0, 0, 7, 0, 0, 0},
{0, 0, 0, 0, 4, 5, 7, 0, 0},
{0, 0, 0, 1, 0, 6, 0, 3, 0},
{0, 0, 1, 0, 0, 0, 0, 6, 8},
{0, 0, 8, 5, 0, 0, 0, 1, 0},
{0, 9, 0, 0, 0, 0, 4, 0, 0}};

Matrix M = new matrix (sudoku);
M.backtrack (0, 0);
}

}

Related Article

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.