Java implementation to solve the world's most difficult nine problem _java

Source: Internet
Author: User

The Finnish mathematician has spent 3 months designing the most difficult Sudoku game in the world to date, and it has only one answer. Carla says only the fastest-thinking, smartest-minded person can crack the game.

Today, a Tencent news that the Chinese old man three days to crack the world's most difficult nine Sudoku, although the last old man is changed a number, but aroused my interest, want to solve the problem by computer program, so in the dormitory stayed one afternoon, finally successfully solved, the program source code is as follows.

Copy Code code as follows:

Package numbergame;

public class Point {
private int col;//line number
private int row;//column number
Private Boolean flag;//is really not set.
private int value;
Construction Point
Public point (int col, int row, Boolean flag, int value) {
Super ();
This.col = col;
This.row = row;
This.flag = Flag;
This.value = value;
}

public void Changeflag () {
flag =!flag;
}

public Boolean Getflag () {
return flag;
}

public int GetValue () {
return value;
}

public void SetValue (int value) {
This.value = value;
}

public boolean canhere (point[][] pArr) {
Boolean cb = Cancol (PARR);
Boolean CR = Canrow (PARR);
Boolean Cminiarr = Canminiarr (PARR);
Return CB && CR && Cminiarr;
}
To determine if there are identical elements in the small 3*3 lattice.
Private Boolean Canminiarr (point[][] pArr) {
int coltemp = this.col% 3;
int rowtemp = this.row% 3;

for (int i = This.col-coltemp I < col + (3-coltemp); i++) {
for (int j = This.row-rowtemp J < row + (3-rowtemp); j + +) {
if (i = = This.col && j = = This.row) {
Continue
}else{
if (This.value = = Parr[i][j].getvalue ()) {
return false;
}
}
}
}
return true;
}

Determine if the same element is on the column
Private Boolean Canrow (point[][] pArr) {
for (int i = 0; i < 9; i++) {
if (i = = This.col) {
Continue
} else {
if (this.value = = Parr[i][this.row].value) {//row change, column unchanged
return false;
}
}
}
return true;
}

Determine if the same element is on the line
Private Boolean Cancol (point[][] pArr) {
for (int i = 0; i < 9; i++) {
if (i = = This.row) {
Continue
} else {
if (this.value = = Parr[this.col][i].value) {//column edge, row unchanged
return false;
}
}
}
return true;
}
}

-– Main Program

Copy Code code as follows:

Package numbergame;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;

public class Number99 {

public static void Main (string[] args) throws ioexception{
point[][] Nummat = new POINT[9][9];
Arraylist<point> al = new Arraylist<point> ();

Initnummat (Nummat,al);

Setnum (Nummat,al);
Printmat (Nummat);
}

private static void Setnum (point[][] nummat,arraylist<point> al) {
int i = 0;
int j = 0;
do{
if (Nummat[i][j].getflag ()) {
for (int v = nummat[i][j].getvalue () +1; v <= 9; v++) {//The value of the position to fall back to plus one.
Nummat[i][j].setvalue (v);
if (Nummat[i][j].canhere (Nummat)) {//satisfies the condition, does not conflict.
Nummat[i][j].changeflag ()//change mark as false. Indicates that it has been set.
Break
}else{//satisfies not the condition, the conflict. Value is added from one time
}

while (v = = 9) {//If 1-9 does not meet the requirements, first resets the standard to 0, and then returns a space, giving the value of the fallback position plus one (when the value of the fallback position is not 9 o'clock, and it is not the original point of nine Sudoku).
Nummat[i][j].setvalue (0);
j--;
if (j==-1) {
i--;j=8;
}
while (Al.contains (Nummat[i][j])) {//If the fallback position is nine sudoku The original point, continue to rollback until it is not its own point to jump out while.
j--;
if (j==-1) {
i--;j=8;
}
}
Nummat[i][j].changeflag ();//will mark
v = nummat[i][j].getvalue ();
}
}
}
j + +;
if (j==9) {
j=0;i++;//here i++ may make I from 9, so the following needs to i!=9 judgment
}
if (i!=9) {
while (Al.contains (Nummat[i][j])) {
j + +;
if (j==9) {
j=0;i++;
}
}
}
}while (i!=9);

}

public static void Initnummat (point[][] nummat,arraylist<point> al) throws IOException {
for (int i = 0; i < nummat.length; i++) {
for (int j = 0; J < Nummat[i].length; J + +) {
NUMMAT[I][J] = new Point (I, J, true, 0);
}
}
InitNumMat2 (Nummat, AL);

}

    public static void InitNumMat2 (point[][] Nummat, Arraylist<point> al) throws IOException {         BufferedReader br = new BufferedReader (new InputStreamReader (system.in) );
        string[] p = new STRING[3];
        String line=null;
        System.out.println ("Enter the point information (i line number, J column number V), enter end input over:i J V");

        while (line = Br.readline ())!=null) {
             if (line.equals ("Over"))
                 break;
            p = Line.trim (). Split ("+");
            Nummat[integer.parseint (p[0]) [ Integer.parseint (P[1])].setvalue (Integer.parseint (p[2));
            Nummat[integer.parseint (p[0]) [ Integer.parseint (P[1])].changeflag ();
            Al.add (Nummat[integer.parseint (p[0)] [ Integer.parseint (p[1]));
       }
   }

public static void Printmat (point[][] nummat) {
System.out.println ("--------┬---------┬---------┐");

for (int i = 0; i < nummat.length; i++) {
for (int j = 0; J < Nummat[i].length; J + +) {
if ((j + 1)% 3 = 0)
System.out.print (Nummat[i][j].getvalue () + "|");
Else
System.out.print (Nummat[i][j].getvalue () + "");
}
if ((i + 1)% 3 = 0)
System.out.println ("\ r \ n--------┼---------┼---------┤");
Else
System.out.println ();
}
}

}

——-Run the program

Enter the point information in the format (I line number, J column v value), enter end input Over:i J V
0 0 8
1 2 3
1 3 6
2 1 7
2 4 9
2 6 2
3 1 5
3 5 7
4 4 4
4 5 5
4 6 7
5 3 1
5 7 3
6 2 1
6 7 6
6 8 8
7 2 8
7 3 5
7 7 1
8 1 9
8 6 4
Over
--–┬ ——— ┬ ——— ┐
8 1 2 | 7 5 3 | 6 4 9 |
9 4 3 | 6 8 2 | 1 7 5 |
6 7 5 | 4 9 1 | 2 8 3 |
--–┼ ——— ┼ ——— ┤
1 5 4 | 2 3 7 | 8 9 6 |
3 6 9 | 8 4 5 | 7 2 1 |
2 8 7 | 1 6 9 | 5 3 4 |
--–┼ ——— ┼ ——— ┤
5 2 1 | 9 7 4 | 3 6 8 |
4 3 8 | 5 2 6 | 9 1 7 |
7 9 6 | 3 1 8 | 4 5 2 |
--–┼ ——— ┼ ——— ┤

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.