Solution to Sudoku problem -- HDU -- 1426 -- Sudoku killer

Keywords: PHP Programming

Topic connection: http://acm.hdu.edu.cn/showproblem.php?pid=1426

Problem Description

Since the first Sudoku World Championship from March 10 to 11, 2006, Sudoku has been more and more loved and valued by people.
It is said that at the 2008 Beijing Olympic Games, Sudoku will be listed as a separate event for competition, and the champion will probably get a huge prize - HDU free seven day tour plus lcy's autograph and the opportunity to take photos with hdu acm team.
So people all over the world train day and night for prizes. Of course, it also includes the beginner linle, but he is too stupid and has little patience. He can only do the most basic Sudoku, but he still wants to get those prizes. Can you help him? You just have to tell him the answer. You don't have to teach him how to do it.

The rule of Sudoku game is as follows: in a 9x9 square, you need to fill in the numbers 1-9 into the space, and make each row and column of the square contain the nine numbers 1-9. At the same time, make sure that the space is divided into nine 3 x 3 squares with thick lines, which also contain the nine numbers of 1-9. For example, if you have a question like this, you can carefully observe that each row, column, and each box of 3 x 3 contains nine numbers of 1-9.
 

Input

This question consists of several groups of tests separated by a blank line. Each group 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 filled number of the location, and the question mark (?) indicates the number you need to fill in.

Output

For each group of tests, please output its solution. Two adjacent numbers in the same line are separated by a space. There should be a blank line between the two groups of solutions.
For each group of test data, it is guaranteed 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 

Solutions:

1. What we need to learn is the input problem. See the code for details

2. First define a structure, including x and y, and then define an array with this structure. When inputting, if it is "?" Put the left side into the array (I think it's a little tricky)

3. Write a check function, which can be used to judge whether there is any repetition in the same row and the same small nine palace lattice when filling in a certain number

4. Write a deep search dfs() function. If it's all filled in, print it out (pay attention to the output format); if it's not filled in, fill in 1-9 one by one and start deep search.

Code:

#include<iostream>
using namespace std;
struct dd
{
	int x;
	int y;
}a[100];//Coordinates used to store the location to be filled 
int map[10][10];//Used to store numbers on Sudoku. If it is "?" It is 0 
int num;//It is used to count how many numbers need to be filled in 
int  check(int k,int step)//k represents the number to be judged, and step represents the number to be filled in 
{
	int i,j,p,q;
	for(i=0;i<9;i++)//Judge whether the same line and the same column have duplicate 
		if(map[a[step].x][i]==k||map[i][a[step].y]==k)
			return 0;
	p=(a[step].x/3)*3;
	q=(a[step].y/3)*3;//Think about what these two lines are for 
	for(i=p;i<p+3;i++)//Judge whether a 3 * 3 square is repeated 
		for(j=q;j<q+3;j++)
			if(map[i][j]==k)
				return 0;
	return 1;
}
void dfs(int step)
{
	int i,j; 
	if(num==step)//All have been filled in 
	{
		for(i=0;i<9;i++)//Print out, pay attention to the output format 
		{
			for(j=0;j<8;j++)
				cout<<map[i][j]<<" ";
			cout<<map[i][8]<<endl;
		}
		return ; 
	}
	for(i=1;i<=9;i++)//1-9 judge whether it can be filled in
	{
		if(check(i,step)==1)//i can be filled in 
		{
			map[a[step].x][a[step].y]=i;//Fill in i 
			dfs(step+1);
			map[a[step].x][a[step].y]=0;//reduction 
		}
	}
	return ;
}
int main()
{
	int i,j,t=0;
	char s;
	while(cin>>s)
	{
		num=0;
		if(s=='?')
		{
			a[num].x=0;
			a[num].y=0;//Because the first "?" The coordinates of are (0, 0)
			num++;
			map[0][0]=0; 
		}
		else
			map[0][0]=s-'0';
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
			{
				if(i==0&&j==0)
					continue;
				else
				{
					cin>>s;
					if(s=='?')
					{
						a[num].x=i;
						a[num].y=j;
						num++;
						map[i][j]=0;
					}
					else
						map[i][j]=s-'0';
				}
			}
		}
		if(t++)
			cout<<endl;//Control line feed
		dfs(0);//From the first 
	}
	return 0;
}

Now that I've seen it all, why don't you give me some praise and go!!!

14 original articles published, 10 praised, 522 visited
Private letter follow

Posted by raheel on Tue, 28 Jan 2020 20:38:51 -0800