The 5th Blue Bridge Cup JavaA Group 6

Keywords: Programming

You must have heard of Sudoku.
For example [Figure 1.png], players need to deduce all the remaining space numbers according to the known numbers on the 9*9 disk, and satisfy that the numbers in each row, column and nine palaces of the same color contain 1-9, without duplication.

Sudoku's answer is unique, so multiple solutions are also called no solutions.

The figures in this picture are said to be the more difficult problem that Finnish mathematicians spent three months designing. But for you who can use computer programming, I'm afraid it's easy.

The requirement of this problem is to input Sudoku and output the only solution of Sudoku. We guarantee that all known data formats are legitimate and that the title has a unique solution.

Format requirement, input 9 lines, each line 9 characters, 0 represents unknown, other numbers are known.
Output 9 lines, each line 9 digits for Sudoku solution.

For example:
Input (i.e. the title in the figure):
005300000
800000020
070010500
400005300
010070006
003200080
060500009
004000030
000009700

The program should output:
145327698
839654127
672918543
496185372
218473956
753296481
367542819
984761235
521839764

For example, input:
800000000
003600000
070090200
050007000
000045700
000100030
001000068
008500010
090000400

The program should output:
812753649
943682175
675491283
154237896
369845721
287169534
521974368
438526917
796318452

Resource agreements:
Peak memory consumption (including virtual machines) < 256M
CPU consumption < 2000 ms

Please output strictly according to the requirements, and do not print something like "Please input..." Redundant content.

All the code is placed in the same source file. After debugging, the source code is copied and submitted.
Note: Do not use package statements. Do not use jdk1.7 or above features.
Note: The name of the main class must be Main, otherwise it will be treated as invalid code.

public class BB0506 {
private int[][] matrix;

public BB0506(int[][] matrix) {  
    this.matrix = matrix;  
}  

public static void main(String[] args) {  
    // Known as 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, 0, 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}};  
    BB0506 s = new BB0506(sudoku);  
    s.backTrace(0, 0);  
}  

/** 
 * Sudoku algorithm 
 * 
 * @param i Line number 
 * @param j Column number 
 */  
private void backTrace(int i, int j) {  
    if (i == 8 && j == 9) {  
        //Successful. Print the array.  
        System.out.println("Getting the Right Solution");  
        printArray();  
        return;  
    }  

    //It's at the end of the column. Before it's at the end of the line, it's time to change.  
    if (j == 9) {  
        i++;  
        j = 0;  
    }  

    //If row i, column j, is blank, then the logic to fill in the blank is entered  
    if (matrix[i][j] == 0) {  
        for (int k = 1; k <= 9; k++) {  
            //Determine whether any number of columns 1-9 in row i satisfies the rule  
            if (check(i, j, k)) {  
                //Assign the value to the space, and then go to the next space.  
                matrix[i][j] = k;  
                backTrace(i, j + 1);  
                //Initialize the space  
                matrix[i][j] = 0;  
            }  
        }  
    } else {  
        //If the location is already valid, go to the next space for calculation.  
        backTrace(i, j + 1);  
    }  
}  

/** 
 * Determine whether assigning a column to a row conforms to the rules 
 * 
 * @param row    The assigned line number 
 * @param line   The assigned column number 
 * @param number Value of Fu 
 * @return 
 */  
private boolean check(int row, int line, int number) {  
    //Determine whether the column in the row has duplicate numbers  
    for (int i = 0; i < 9; i++) {  
        if (matrix[row][i] == number || matrix[i][line] == number) {  
            return false;  
        }  
    }  
    //Judging whether there is duplication in Xiaojiu Gong Ge  
    int tempRow = row / 3;  
    int tempLine = line / 3;  
    for (int i = 0; i < 3; i++) {  
        for (int j = 0; j < 3; j++) {  
            if (matrix[tempRow * 3 + i][tempLine * 3 + j] == number) {  
                return false;  
            }  
        }  
    }  

    return true;  
}  

/** 
 * Print matrix 
 */  
public void printArray() {  
    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();  
}  

}

Posted by gisan on Sat, 30 Mar 2019 00:51:28 -0700