Backtracking algorithm--Sudoku problem __ Algorithm

Source: Internet
Author: User
or on the problem encountered, as if to become the first love general and hate ...
Sudoku Problem


You must have heard of the Sudoku game.
As shown in the figure, the player needs to infer all the remaining space figures according to the known numbers on the 9x9 disk, and satisfy each row, each column, each nine within the same color contains 1-9, do not repeat.

The answer to Sudoku is unique, so many solutions are also called no solutions.

The figures in this figure are said to be the difficult subjects that the Finnish mathematician has spent 3 months designing. But for you who can use computer programming, I'm afraid it's a breeze.

The requirements of this topic is the input Sudoku problem, the program output Sudoku unique solution. We guarantee that all known data are in a valid format and that the problem has a unique solution.

Format requirements, enter 9 lines, 9 characters per line, 0 is unknown, and other numbers are known.
The output is 9 lines, and 9 digits per line represent the solution of Sudoku.



Train of thought: still violence + backtracking, in order to determine the number of rows per row from 0-9 can be put in, and then recursive.

0-9 when shaking does not meet the condition, backtrack to the previous.

Code:

Package Lanqiaobei;
Import java.io.*;
Import Java.util.Scanner; public class Shudu {public int a[][] = new INT[9][9]; public boolean is_row_col_repeat (int row,int col,int N) {for (int i
	=0;i<9;i++) {if (a[row][i] = n) return false;
	for (int i=0;i<9;i++) {if (A[i][col] = n) return false;
return true;
	The public int get_row_range (int row) {//judge the same color lattice when judged for which large lattice if (0<=row && row<=2) return 0;
	else if (3<=row && row<=5) return 3;
else return 6;
	The public int get_col_range (int col) {//judge the same color lattice when judged for which large lattice if (0<=col && col<=2) return 0;
	else if (3<=col && col<=5) return 3;
else return 6; public boolean is_block_repeat (int x,int y,int N) {for (int i=x;i<=x+2;i++) for (int j=y;j<=y+2;j++) {{if
			(A[i][j] = = N) return false;
} return true; public void Dfs (int row,int col) {if (row >= 9) {for (int i=0;i<=8;i++) {for (int j=0;j<=8;j++) {Syste
			M.out.print (A[i][j]);
		}System.out.println ("");
	int x = Get_row_range (row);
	int y = get_col_range (col); if (a[row][col] = = 0) {for (int i=1;i<=9;i++) {//Digital 1-9 if (is_row_col_repeat (row,col,i) && is_block_repeat (x,y
			, i)) {A[row][col] = i;
		DFS (row + (col + 1)/9, (col + 1)% 9);
	}//If 1-9 is not a[row][col] = 0;
	Return
	}else {dfs (row + (col + 1)/9, (col + 1)% 9);
		} public static void Main (string[] args) {//TODO auto-generated method stub Shudu s = new Shudu ();
		Scanner Scanner = new Scanner (system.in);
		String str;
		Char[] AI;
			for (int i = 0; i < 9; i++) {str = Scanner.nextline ();
			ai = Str.tochararray ();
			for (int j = 0; J < 9; J +) {S.a[i][j] = Integer.parseint (Ai[j] + "");
		} s.dfs (0, 0);
 }/* 005300000 800000020 070010500 400005300 010070006 003200080 060500009 004000030 000009700 */}

Input

005300000

800000020

070010500

400005300

010070006

003200080

060500009

004000030

000009700

Output Answer:

145327698

839654127

672918543

496185372

218473956

753296481

367542819

984761235

521839764

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.