Nyoj-722-Sudoku &&hdoj-1426-sudoku Killer

Source: Internet
Author: User
hdoj on the topic:
Problem Description since the first Sudoku World championships from March 10, 2006 to 11th, Sudoku has become more and more popular with people.
It is said that in the 2008 Beijing Olympic Games, the Sudoku will be listed as a separate project to compete, the champion will have the possibility to obtain a huge prize ——— HDU free 7th Tour plus LCY Autograph and HDU with the ACM team photo of the opportunity.
So the world's people fought, for the prize day and night training to not think. Of course, including beginners Linle, but he is too stupid and not much patience, can only do the most basic Sudoku problem, but he still want those prizes, you can help him. You just have to tell him the answer and 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 out the number 1-9 in a space and include 1-9 of these nine digits in each row and column of the square. It is also ensured that the spaces are divided into 9 3x3 squares with a thick line that also contains 1-9 of these nine digits. For example, if you have such a problem, you can look at it in detail, with each row, column, and every 3x3 square containing 1-9 of these nine digits.

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 single space for the two elements that are adjacent to the same line. 1-9 represents the completed number of the position, and the question mark (?) indicates the number you want to fill in.

Output for each set of tests, please print out its solution, two digits adjacent to the same row are separated by a single space. A blank line is needed between the two sets of solutions.
For each set of test data it is guaranteed and has only one solution.

Sample Input
7 1 2? 4} 3 5 8? 6 5 2? 7 1? 4?? 8 5 1 3 6 7 2 9 2 4? 5 6? 3 7 5? 6??? 2 4 1 1? 3 7 2? 9? 5?? 1 9 7 5 4 8 6 6? 7 8 3? 5 1 9 8 5 9? 4?? 2 3
Sample Output
The 7 1 2 4 6 9 3 5 8 3 6 5 2 8 7 1-----------------------+--- 3 1 9 7 5 4 8 6 6 4 7 8 3 2 5 1 9 8 5 9 6 4 1 7 the 2 3
In fact, nyoj the above problem and this is similar to this, just input and this is not the same, than the hdoj above simple I will not write again

#include <stdio.h>
#include <string.h>
const int n=9;
int map[n][n];
BOOL F=1;
BOOL flag=0;
The number used in the line of the bool row[n][n+1];//mark.
BOOL col[n][n+1];//mark that number in that column.
BOOL block[3][3][n+1];//mark the number that's been used.

This kind of tag array can effectively improve the efficiency of the future to remember a lot of use

void put (int x)
{
if (flag)
Return
if (x>80)//will be filled 81 times (0-80) means that the whole number of the single completed
{
if (!f)
printf ("\ n");
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
if (j==0)
printf ("%d", map[i][0]);
Else
printf ("%d", map[i][j]);
}
printf ("\ n");
}
flag=1;
f=0;
Return
}
int ro=x/9;//x This number represents the line
int co=x%9;//x The column represented by this number
if (!map[ro][co])//The square lattice is not counted, just fill it in.
{
for (int i=1;i<=n;i++)
{
if (!) ( row[ro][i]| | col[co][i]| | Block[ro/3][co/3][i]))
{
Map[ro][co]=i;
Row[ro][i]=col[co][i]=block[ro/3][co/3][i]=1;
Put (x+1);
map[ro][co]=0;
row[ro][i]=col[co][i]=block[ro/3][co/3][i]=0;
}
}
Return
}
else//The square is numbered, keep going down.
Put (x+1);
}
int main ()
{
Char ch[2];
int i=0,j=0;
while (scanf ("%s", ch)!=eof)//small routines, in order not to read the space between each digit
{
if (ch[0]== '? ')
map[i][j]=0;
Else
{
map[i][j]=ch[0]-' 0 ';
Row[i][map[i][j]]=1;
Col[j][map[i][j]]=1;
Block[i/3][j/3][map[i][j]]=1;
}
j + +;
if (j==n)
{
i++; j=0;
}
if (i==n)
{
Put (0);
i=0;j=0; flag=0;
memset (row,0,sizeof (row));
memset (col,0,sizeof (col));
Memset (block,0,sizeof (block));
}
}
return 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.