Sudoku depth [search]

Sudoku depth [search]

topic

Title Description

The only known digital number is 9 × 9 on the disk, all the remaining reasoning the digital space, and meet each row, each row, each thick line intrauterine 1-9 inclusive, will not be repeated. Every qualified Sudoku puzzle has exactly the only answer, is based on this reasoning, any or no solution solved the problem are unqualified.

Finland is known as a mathematician to design the world's most difficult "Sudoku" and in the newspapers, so that everyone to challenge.

The mathematician said he believed that only "the wisdom of top" talent is likely to crack this "Sudoku mystery."

According to reports, the current level of Sudoku difficulty there is a five, first, entry-level, five more difficult. But the mathematician says he designed Sudoku difficulty levels are eleven, it can be said that Sudoku, the highest level of difficulty, he said, he is currently experiencing solution is not yet out of the Sudoku game, Therefore, he believes that "most challenging" Sudoku game did not occur.

Input Format

Unfilled a Sudoku

Output Format

Completed Sudoku

Sample input and output

Input # 1 replication
. 8 0 0 0 0 0 0 0 0
0 0. 3. 6 0 0 0 0 0
0. 7 0 0. 9 0 2 0 0
0. 5 0 0 0. 7 0 0 0
0 0 0 0. 4. 5. 7 0 0
0 0 0 1 0 0 0. 3 0
0 0 1 0 0 0 0. 6. 8
0 0. 8. 5 0 0 0 1 0
0. 9 0 0 0 0. 4 0 0
output # 1 replication
. 8 1 2. 7. 5. 3. 6. 4. 9
. 9. 4 . 3. 6. 8 2. 1. 7. 5
. 6. 7. 5. 4. 9. 1 2. 8. 3
. 1. 5. 4 2. 3. 7. 8. 9. 6
. 3. 6. 9. 8. 4. 5. 7 2. 1
2. 8. 7. 1. 6. 9. 5. 3. 4
. 5 2. 1. 9. 7. 4. 3 . 6. 8
. 4. 3 2. 8. 6. 9. 1. 5. 7
. 7. 9. 6. 8. 4. 3. 1. 5 2

analysis

Sudoku rules, that each row, each column, each 3 * 3 small boxes can not have duplicate numbers.
In fact, the eight queens problem and do the same, we can consider put digital line by line (actually where to start are possible).

1 reads data disk, here a [i] [j], then starting from row 0, 0, (in fact all right, 0 ranks convenience)
2 used here vx [] [] represents each row, vy [ ] [] represent each column, vc [] every 3x3 small square
(one-dimensional representation which line, column, box) (this time with a two-dimensional representation which digital)
VX [] [], vy [] [ ] nothing to say, look at this vc [] [], which indicate how the box it,
is this: i / 3 * 3 + j / 3, ( specifically his own on behalf of the few points to know)

Other typical deep and found no difference. . . . .

There Road upgrade of difficulty Sudoku problem, called target-shaped Sudoku. Click here to Kazakhstan

	for(int i=0;i<9;i++){
		for(int j=0;j<9;j++){
			cin>>a[i][j];
			vx[i][a[i][j]] = 1;
			vy[j][a[i][j]] = 1;
			vc[i/3*3+j/3][a[i][j]] = 1;
		}
	}

Code

#include<iostream>

using namespace std;

int a[10][10];
int vx[10][10],vy[10][10],vc[10][10];

int f;
void dfs(int x,int y){
	
	
	if(x==9){
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				cout<<a[i][j]<<" ";
			}
			cout<<endl;
		}
		
		return ;
	}
	if(y==9){
		dfs(x+1,0);
		return ;
	}
	if(a[x][y]){
		dfs(x,y+1);
	}	else{
		for(int i=1;i<=9;i++){
			if ( !vx[x][i] && !vy[y][i] && !vc[x/3*3+y/3][i]){
				vx[x][i] =1;
				vy[y][i] =1;
				vc[x/3*3+y/3][i] =1;
				a[x][y]	=i;
											
				dfs(x,y+1);
				vx[x][i] =0;
				vy[y][i] =0;
				vc[x/3*3+y/3][i] =0;
				a[x][y] = 0;
			}
		}
	}
}

int main(){
	
	for(int i=0;i<9;i++){
		for(int j=0;j<9;j++){
			cin>>a[i][j];
				if(a[i][j]){
				vx[i][a[i][j]] = 1;
				vy[j][a[i][j]] = 1;
				vc[i/3*3+j/3][a[i][j]] = 1;
				}
		}
	}
	dfs(0,0);
	
	return 0;
} 
Published 75 original articles · won praise 1 · views 3638

Guess you like

Origin blog.csdn.net/A793488316/article/details/104726475