tags: Hangdian oj
Sudoku KillerTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12369 Accepted Submission(s): 3580
Problem Description Since the first Sudoku World Championships from March 10 to 11, 2006, the game of Sudoku has become more and more popular and valued by people.
It is said that in the 2008 Beijing Olympic Games, Sudoku will be listed as a separate event for competition, and the champion will likely receive a huge prize-HDU free seven-day tour Plus the opportunity of autographing with lcy and taking a photo with the hdu acm team.
So people from all over the world succeed in training tea and rice for the prize day and night. Of course, it includes beginner linle, but he is too stupid and has little patience. He can only do the most basic Sudoku questions, but he still wants to get those prizes. Can you help him? You only need to tell him the answer, without teaching him how to do it.
The rule of the Sudoku game is this: In a 9x9 square, you need to fill in the numbers 1-9 in the space, and make each row and column of the square contain the nine numbers 1-9. At the same time, it must also be ensured that the space is divided into 9 3x3 squares with thick lines and also contains nine numbers 1-9. For example, there is such a question, you can take a closer look, in which each row, each column, and each 3x3 square contains nine numbers 1-9.
example:
answer:
Input This question contains multiple groups of tests, each group is separated by a blank line. Each set of tests will give you a 9 * 9 matrix, with two adjacent elements in the same row separated by a space. Among them, 1-9 represents the number already filled in the position, and the question mark (?) Indicates the number that needs you to fill in.
Output For each set of tests, please output its solution. Two adjacent numbers on the same line are separated by a space. There should be a blank line between the two sets of solutions.
For each set of test data, ensure that it has only one solution.
Sample Input
7 1 2 ? 6 ? 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
7 1 2 4 6 9 3 5 8
3 6 5 2 8 7 1 9 4
4 9 8 5 1 3 6 7 2
9 2 4 1 5 6 8 3 7
5 7 6 3 9 8 2 4 1
1 8 3 7 2 4 9 6 5
2 3 1 9 7 5 4 8 6
6 4 7 8 3 2 5 1 9
8 5 9 6 4 1 7 2 3
Personal thoughts:
For a depth-first search question, backtracking is still used.
Read in: borrow character type, save as integer when saving.
Record:
1. Record each row and column of ‘?’.
2. Use a b to mark the array (1 ~ 9). When searching for each '?', fill in the same column in the same row and a nine-square grid. (The number recorded in the shudu array), where the existing data k sets b [k] to 1. (Note that a new array of b tags must be opened for each search.)
3. Then fill shudu with 1 ~ 9 that is not set to 1, you can mark it when filling in, and then fill the next "?" fill operation, when you come back Backtrack.
Boundary conditions:
1. If this method of filling is not possible, it will become that when a certain "?" is filled, 1 ~ 9 cannot find a number that is not available, and then it will go back. Find a new strategy.
2. If this method of filling is possible, output it.
output:
1. There must be a line break between each set of answers, use fag_zu to record, if this is the first set of solutions, do not output line breaks, and set fag_zu from 0 to 1, if this If it is not the first set of solutions (fag_zu is not 0), it is necessary to output the line feed first and then output the answer.
2. Use spaces between each number, and also use fag to mark whether it is the first number of each line. The method is the same as above.
Questions when doing:
1. Uh ... At the beginning, there wasn't too much chance in the while loop input. Later, I thought about using the first number as the condition in the while. It becomes like while (cin >> shuru [1] [1]).
2. It is found that many inputs of Hangdian are of an infinite number of times, and then the output requirements become particularly strict, so I have to use fag and fag_zu to mark the output specifically.
3. I feel that there are a lot of things to be recorded in Deep Search.
4. The regret is that I did n’t prune this question ... I did n’t think how to prun it.
Code:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
char shuru[10][10];
int shudu[10][10];
int hang[82];
int lie[82];
int num=0;// Indicates the number of numbers to be filled
int fag_zu=0;// Used to mark blank lines between group solutions
int dfs(int i)// Indicating positioning number
{
if(i>num)// Indicates that the goal has been achieved
{
// output a solution
if(fag_zu==0)// Indicates that this is the first set of solutions
fag_zu=1;
else
{
fag_zu=1;
cout<<endl;
}
int fag;// Used to mark the output space
for(int p=1;p<=9;p++)
{
fag=0;// The first number of each line does not need to output spaces in front
for(int p2=1;p2<=9;p2++)
{
if(fag==0)
{
fag=1;
printf("%d",shudu[p][p2]);
}
else
printf(" %d",shudu[p][p2]);
}
cout<<endl;
}
}
bool b[10];// Can the marker array 1 ~ 9 be used? Every time you fill, you need to see which one can be filled.
int temp;
memset(b,0,sizeof(b));// The first means that these numbers are not occupied now
b[0]=1;// Initialize 0 to occupy
int hang_zan=hang[i];
int lie_zan=lie[i];// Note the rank of this empty position
for(int j=1;j<=9;j++)// Judging the ranks
{
if(shudu[hang_zan][j]!=-1)
{
temp=shudu[hang_zan][j];
b[temp]=1;// Indicates that it is no longer available
}
if(shudu[j][lie_zan]!=-1)
{
temp=shudu[j][lie_zan];
b[temp]=1;
}
}
for(int h=(hang_zan-1)/3*3+1; h<=(hang_zan-1)/3*3+3; h++)// Judging nine grids
{
for(int l=(lie_zan-1)/3*3+1; l<=(lie_zan-1)/3*3+3; l++)
{
if(shudu[h][l]!=-1)
{
temp=shudu[h][l];
b[temp]=1;
}
}
}
for(int k=1;k<=9;k++)// (no pruning)
{
if(b[k]==0)// If it is not occupied
{
b[k]=1;// Take it up and fill in this number
shudu[hang_zan][lie_zan]=k;
dfs(i+1);// Go to the next
b[k]=0;// backtrack
shudu[hang_zan][lie_zan]=-1;
}
}
return 0;
}
int main()
{
while(cin>>shuru[1][1])// Use the input of the first character as each group
{
num=0;// Clear num every time 0
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
if(i!=1||j!=1)
cin>>shuru[i][j];
memset(shudu,0,sizeof(shudu));
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
{
if(shuru[i][j]!='?')
{
shudu[i][j]=int(shuru[i][j]-'0');
}
else
{
shudu[i][j]=-1;
num++;
hang[num]=i;
lie[num]=j;// Record the row and column number of the number to be filled
}
}
dfs(1);
}
return 0;
}
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1271 Accepted Submission(s): 352 Probl...
Link: Here! Ideas: Record all "?", Where they appear, and then $ DFS $ bit, for each position can be filled for $ 9 $ kinds of values, then determine whether to fill a legitimate need for th...
1. The original question link:http://acm.hdu.edu.cn/showproblem.php?pid=1426 Problem Description since2006year3month10Day to11After the first Sudoku World Championship on the day, the game of Sudoku h...
http://acm.hdu.edu.cn/showproblem.php?pid=1426 First of all, pay attention to the input format, and use scanf to automatically ignore line breaks. Problem-solving ideas: enumerate each ‘? &rsquo...
description: The rules of the Sudoku game are as follows: In a 9x9 square, you need to fill in the numbers 1-9 in the blanks, and make each row and column of the square contain the nine numbers 1-9. A...
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1293 Accepted Submission(s): 424 Problem Description Since the first several ...
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8081 Accepted Submission(s): 2494 Prob...
Luogu 1784 Sudoku: The first question is to give you a 9x9 unfilled Sudoku (not filled with 0), asking you to fill in the numbers of these 0s to meet the number. Unique characteristics. parsing: In fa...
Topic connection:http://acm.hdu.edu.cn/showproblem.php?pid=1426 Problem Description Since the first Sudoku World Championships from March 10 to 11, 2006, the game of Sudoku has become more and more po...