Sudoku-The problem

Source: Internet
Author: User

you must have heard of the Sudoku game.
such as "Figure 1.png", the player needs to infer the number of all remaining spaces according to the known numbers on the 9x9 disk surface, and to satisfy each row, each column, each color nine within the number contains 1-9, does not repeat.
The answer to Sudoku is unique, so multiple solutions are also known as non-solutions.
The figures in this figure are said to be the more difficult topic that Finnish mathematicians have spent 3 months designing. But I'm afraid it's a breeze for you to use computer programming.
The requirement of the topic is to enter Sudoku, the only solution of the program output Sudoku. We guarantee that all known data formats are legal and that the title has a unique solution.
Format requirements, enter 9 lines, 9 characters per line, 0 for unknown, and other numbers to be known.
Output 9 lines, 9 numbers per line represents 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 contract:
Peak memory Consumption < 256M

CPU Consumption < 2000ms



The problem-solving idea: The data volume is small, the retrospective method efficiency is considerable.

Import java.util.scanner;/** * * Sudoku game * Test Data 1:0 0 5 3 0 0 0 0 08 0 0 0 0 0 0 2 00 7 0 0 1 0 5 0 04 0 0 0 0 5 3 0 0 0 1 0 0 7 0 0 0 60 0 3 2 0 0 0 8 00 6 0 5 0 0 0 0 90 0 4 0 0 0 0 3 00 0 0 0 0 9 7 0 0 test data 2:8 0 0 0 0 0 0 0 00 0 0 00 7 0 0 9 0 2 0 00 5 0 0 0 7 0 0 00 0 0 0 4 5 7 0 00 0 0 1 0 0 0 3 00 0 1 0 0 0 0 6 80 0 8 5 0 0 0 1 00   4 0 0 * */public class Main{public static Boolean flag = FALSE;        Whether the token has been found public static void main (string[] args) {Scanner sc = new Scanner (system.in);        int arr[][] = new int [10][10]; for (int i = 1, i < arr.length; i++) for (int j = 1; j < Arr[i].length; J + +) Arr[i][j] = Sc.nexti        NT ();                Sc.close (); DFS (1, 1, arr);}    private static void Dfs (int x, int y, int[][] arr) {if (flag) return; if (x > 9) {output (arr); flag = True;return;}     if (Y > 9) {dfs (x+1, 1, arr);    } else if (arr[x][y]! = 0) dfs (X,y+1,arr); else {for (int i = 1; i < ten; i++) {if (check (x, y, I, arr)) {arr[x][y] = i;    DFS (x, y+1, arr);    Arr[x][y] = 0;  }}}} private static Boolean check (int x, int y, int num, int[][] arr) {//check x-axis for (int i = 1; i <; i++) {if (Arr[x][i] = = num) return false;} Check y-axis for (int i = 1; i < i++) {if (arr[i][y] = = num) return false;} Check nine for (int i = (x-1)/3*3+1, I <= (x-1)/3*3+3; i++) {for (int j = (y-1)/3*3+1; J <= ( y-1)/3*3+3;         J + +) {if (arr[i][j] = = num) return false; }}return true;} private static void output (int[][] arr) {for (int i = 1; i < arr.length; i++) {for (int j = 1; j < Arr[i].length; j  + +) {System.out.print (Arr[i][j] + ""); } System.out.println (); }}}




Sudoku-The problem

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.