Waterloo Cup Sudoku Java

Source: Internet
Author: User

You must have heard of the Sudoku game.

such as "Figure 1.png", the player needs to be based on the known number on the 9x9 disk, to infer all the remaining space figures, and meet each row, each column, each of the same color nine within the number 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.


For example:

Input (that is, the title in the picture):

005300000

800000020

070010500

400005300

010070006

003200080

060500009

004000030

000009700

The program should output:

145327698

839654127

672918543

496185372

218473956

753296481

367542819

984761235

521839764

Again, for example, enter:

800000000

003600000

070090200

050007000

000045700

000100030

001000068

008500010

090000400

The program should output:

812753649

943682175

675491283

154237896

369845721

287169534

521974368

438526917

796318452


Resource conventions:

Peak memory consumption (including virtual machines) < 256M

CPU Consumption < 2000ms

Please output strictly according to the requirements, do not superfluous print similar: "Please enter ..." superfluous content.

All the code is placed in the same source file, after debugging, the copy is submitted to the source.

Note: Do not use package statements. Do not use jdk1.7 and the above version of the features.

Note: The name of the main class must be: main, otherwise it will be handled in an invalid code.


Ideas:

Traverse the entire array, if the current position is 0, then fill in 1, Judge the row and nine have no number and 1 repeat, if there is no repetition, then enter the next position, if there is duplication, then fill in the 2,3,4,... 9. By analogy

Import Java.util.Scanner;
	public class Sudoku {long end;
	Long start;

	Static int[][] map = new INT[9][9];
	Public Sudoku () {super (); /** * To determine whether the ranks have duplicates * * @param row * @param column * @param value * @return/Boolean Rowcolumnisnotrepe at (int row, int column, int value) {//Judge column for (int i = 0; i < 9; i++) {if (map[i][column] = = value) {RET
			Urn false;
			}//Judgment line for (int i = 0; i < 9; i++) {if (map[row][i] = = value) {return false;
	} return true;
		/** * To determine whether there is a duplicate/boolean sectionisnotrepeat (int row, int column, int value) {int sectionrow in the same 19 house;
		int Sectioncolumm;
		if (row <= 2) {sectionrow = 0;
		else if (row <= 5) {sectionrow = 3;
		else {sectionrow = 6;
		} if (column <= 2) {sectioncolumm = 0;
		else if (column <= 5) {sectioncolumm = 3;
		else {Sectioncolumm = 6; for (int i = Sectionrow I < Sectionrow + 3; i++) {for (int j = Sectioncolumm; J < Sectioncolumm + 3;
				J + +) {if (map[i][j] = = value) {return false;
	}} return true;
			} void Dfs (int row, int column) {//After filling out results and time (milliseconds) if (row >= 9) {print ();
			End = System.currenttimemillis ();
			System.out.println (End-start + "MS");
		System.exit (0); } if (map[row][column] = = 0) {//from 1 to 9 try for (int i = 1; I <= 9; i++) {If rowcolumnisnotrepeat (row, Colum
					n, i) && sectionisnotrepeat (row, column, i)) {Map[row][column] = i;
				DFS (row + (column + 1)/9, (column + 1)% 9);
			}//Not after the test, return to the upper layer of map[row][column] = 0;
		Return
		else {//If the current position has a number, go to the next location Dfs (row + (column + 1)/9, (column + 1)% 9); } void Print () {for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; J +) {System.out.print (Map[i][j])
			;
		} System.out.println ();
		} public static void Main (string[] args) {Sudoku Sudoku = new Sudoku ();
		Scanner Scanner = new Scanner (system.in); String Str
		Char[] A;
		SYSTEM.OUT.PRINTLN ("please input");
			for (int i = 0; i < 9; i++) {str = Scanner.nextline ();
			A = Str.tochararray ();
			for (int j = 0; J < 9; J +) {Map[i][j] = Integer.parseint (A[j] + "");
			} if (scanner!= null) {try {scanner.close ();
			catch (Exception e) {e.printstacktrace ();
		} Sudoku.start = System.currenttimemillis ();
	Sudoku.dfs (0, 0); }
}

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.