HDU 1426 Sudoku Killer (sudoku, dividing area is key)

Source: Internet
Author: User

Sudoku KillerTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6283 Accepted Submission (s): 1981


Problem description Since the first Sudoku World Championship from March 10, 2006 to 11th, Sudoku has become increasingly popular and valued.
It is said that at the 2008 Beijing Olympic Games, the Sudoku will be listed as a separate project to compete, the winner will be likely to receive a huge prize ——— HDU free 7th-day Tour plus LCY Autograph and a photo with HDU ACM team.
So the people of the world fought, for prizes day and night training Chafanbusi do not think. Of course also includes beginners Linle, but he is too stupid and not much patience, can only do the most basic Sudoku problem, but he still want to get those prizes, can you help him? You just have to tell him the answer so you don't have to teach him how to do it.

The rule of Sudoku is this: In a 9x9 box, you need to fill in the number 1-9 into a space, and make each row and column of the square contain 1-9 of these nine numbers. Also ensure that the space in the thick line divided into 9 3x3 squares also contains 1-9 of these nine numbers. For example, if you have such a problem, you can take a closer look at each line, each column, and each 3x3 square contains 1-9 of these nine numbers.

Examples:


Answer:


The input subject contains multiple sets of tests, separated by a blank line between each group. Each set of tests gives you a 9*9 matrix, separated by a space of two elements adjacent to the same line. Where 1-9 represents the already filled number of the position, the question mark (?) indicates the number you want to fill.

Output for each set of tests, print its solution, separated by a space of two numbers adjacent to the same row. There is a blank line between the two sets of solutions.
For each set of test data it is guaranteed to have only one solution.

Sample Input
7 1 2? 4} 3 5 8? 6 5 2? 7 1? 4?? 8 5 1 3 6 7 29 2 4? 5 6? 3 75? 6??? 2 4 11? 3 7 2? 9? 5?? 1 9 7 5 4 8 66? 7 8 3? 5 1 98 5 9? 4?? 2 3

Sample Output
7 1 2 4 6 9 3 5 83 6 5 2 8 7 1 9 44 9 8 5 1 3 6 7 29 2 4 1 5 6 8 3 75 7 6 3 9 8 2 4 11 8 3 7 2 4 9 6 52 3 8 66 4 7 8 3 2 5 1 98 5 9 6 4 1 7 2 3

Authorlinle
SOURCEACM Summer Training Team Practice Competition (III)
original title link: http://acm.hdu.edu.cn/showproblem.php?pid=1426
idea: A look is a deep search. But the location of the unknown element is saved, and it is critical to determine whether the elements in a small nine lattice are valid in the search!The coordinates of points in the 9*9 large squares are related to the coordinates of the 3*3 small squares:(x, y) represents the coordinates of the 9*9 square, (x, y) in the first square of the 3*3 Square where the point is located;x=x/3*3y=y/3*3
AC Code:
#include <iostream>using namespace std;struct point{int x; int y;}    Point[100];int map[10][10];int num;bool flag;bool Check (int k,int cur) {int i,j,x,y; for (i=0; i<9; i++)//Determine the column and row {if (map[point[cur].x][i]==k| |        Map[i][point[cur].y]==k) {return false;    }} x= (POINT[CUR].X/3);    Y= (POINT[CUR].Y/3);                For (i=x, i<x+3; i++) {for (j=y; j<y+3; J + +) {if (map[i][j]==k) {            return false; }}} return true;            void DFS (int step) {if (Step==num) {for (Int. i=0; i<9; i++) {for (int j=0; j<8; j + +)            {cout<<map[i][j]<< "";        } cout<<map[i][8]<<endl;        } flag=true;    return;    } if (flag) return;            for (int i=1; i<=9; i++) {if (check (i,step)) {map[point[step].x][point[step].y]=i; DFS (STEP+1);        map[point[step].x][point[step].y]=0; }} return;    int main () {int cas=0;    char s;        while (cin>>s) {num=0;        if (s== '? ')            {point[num].x=0;            point[num].y=0;            num++;        map[0][0]=0;        } else {map[0][0]=s-' 0 '; } for (int i=0, i<9; i++) {for (int j=0; j<9; J + +) {if (i==0&&                amp;j==0) continue;                cin>>s;                if (s== '? ')                    {point[num].x=i;                    Point[num].y=j;                    num++;                map[i][j]=0;                } else {map[i][j]=s-' 0 ';        }}} Flag=false;        if (cas++) cout<<endl;    DFS (0); } return 0;}
AC Code 2
#include <stdio.h>struct point{int x; int y;}    Point[100];int map[10][10];int flag,num;int Check (int k,int cur) {int i,j,x,y; for (i=0;i<9;i++) {if (map[point[cur].x][i]==k| |        map[i][point[cur].y]==k) {return 0;    }} x= (POINT[CUR].X/3);    Y= (POINT[CUR].Y/3); for (i=x;i<x+3;i++) {for (j=y;j<y+3;j++) {if (map[i][j]==k) {R            Eturn 0; }}} return 1;}    void DFS (int step) {int i,j; if (Step==num) {for (i=0;i<9;i++) {for (j=0;j<8;j++) {printf ("            %d ", map[i][j]);        } printf ("%d\n", map[i][8]);        } flag=1;    return; } else {for (i=1;i<=9;i++) {if (check (i,step) &&!flag) {map [Point[step].x]               [Point[step].y]=i;               DFS (step+1);   map[point[step].x][point[step].y]=0;        }}} return;}    int main () {int i,j,cas=0;    Char s[3];        while (scanf ("%s", s)!=eof) {num=0;        if (s[0]== '? ')            {point[num].x=0;            point[num].y=0;            num++;        map[0][0]=0;        } else {map[0][0]=s[0]-' 0 '; } for (i=0, i<9; i++) {for (j=0; j<9; J + +) {if (i==0&&j==0                ) {continue;                } scanf ("%s", s);                if (s[0]== '? ')                    {point[num].x=i;                    Point[num].y=j;                    num++;                map[i][j]=0;                } else {map[i][j]=s[0]-' 0 ';        }}} flag=0;        if (cas++) {printf ("\ n");    } DFS (0); } return 0;}




HDU 1426 Sudoku Killer (sudoku, dividing area is key)

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.