Question 6 of the 5th Java Provincial Competition of the Blue Bridge Cup: Sudoku

topic:

You must have heard of the "Sudoku" game.
As shown in [Figure 1.png], the player needs to deduce the numbers of all remaining spaces according to the known numbers on the 9×9 disk, and satisfy that each row, each column, and each number in the nine palaces of the same color contain 1-9. Not repeating.
The answer to Sudoku is unique, so multiple solutions are also called no solutions.
The numbers in this picture are said to be difficult problems that Finnish mathematicians spent 3 months devising. But for you who can use computer programming, I am afraid it will be a piece of cake.
The requirement of this problem is to input the Sudoku problem, and the program outputs the unique solution of Sudoku. We guarantee that all known data formats are legal and that the problem has a unique solution.
Format requirements, input 9 lines, each line is 9 characters, 0 means unknown, other numbers are known.
Output 9 lines, each line of 9 numbers represents the solution of Sudoku.
For example:
input (that is, the title in the picture):
005300000
800000020
070010500
400005300
010070006
003200080
060500009
004000030

000009700

Program should output:
145 327 698
839 654 127
672 918 543
496 185 372
218 473 956
753 296 481
367 542 819
984 761 235
521 839 764

As another example, input:
800 000 000
003 600 000
070 090 200
050 007 000
000 045 700
000 100 030
001 000 068
008 500 010
090 000 400

program should output:
812,753,649

943682175

Answer:

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		int[][] arr=new int[9][9];
		String[] ar=new String[9];
		for(int i=0;i<9;i++){
			ar[i]=in.next();
		}
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				arr[i][j]=(ar[i].charAt(j)-48);
			}
		}
		//The above is the output data to a two-dimensional array. Note that the input data has no spaces, so it can only be converted from a string to a number
		p(arr,0,0);//Recursion
	}
	public static void p(int[][] arr,int i,int j){
		if(j==9){//If the last line is reached, skip to the first line of the next column
			p(arr,i+1,0);
			return;
			}
		if(i==9){//If the last column is reached, output
			for(int i1=0;i1<9;i1++){
				for(int j1=0;j1<9;j1++){
					System.out.print(arr[i1][j1]);
				}
				System.out.println();
			}
			return;
		}
		if(arr[i][j]!=0){//If there is a number in this grid, skip to the next grid
			p(arr,i,j+1);
			return;
			}
		for(int x=1;x<=9;x++){//Start filling in numbers
			boolean bo=true;
			for(int i1=0;i1<9;i1++){//Determine whether the row and column have the same number
				if(arr[i][i1]==x||arr[i1][j]==x){
					bo = false;
					break;
				}
			}
			int i1,j1;
			if(i<3)
				i1=0;
			else if(i<6)
				i1=3;
			else i1=6;
			
			if(j<3)
				j1=0;
			else if(j<6)
				j1=3;
			else j1=6;
			for(int ii=0;ii<3&&bo;ii++){//Determine if there is the same number in the nine-square grid
				for(int jj=0;jj<3&&bo;jj++){
					if(arr[i1+ii][j1+jj]==x){
						bo = false;
					}
				}
			}
			if(bo){
				arr[i][j]=x;
				p(arr,i,j+1);
				arr[i][j]=0;
			}
		}
		return;//This cell cannot be filled, the current thread ends
	}
}

Analysis: Because the memory and time will not time out, the problem is not big, just recursive exhaustive.

Summarize:

1. Pay attention to the format of input and output, otherwise it is very likely that the input data cannot be successfully read, or the output error is zero.

2. When there is no excess of time and space, then boldly write code and make correct results.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324773598&siteId=291194637
Recommended