数独(DFS)


Link:http://poj.org/problem?id=2676


Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14696   Accepted: 7248   Special Judge

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 
数独(DFS)_第1张图片

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Source

Southeastern Europe 2005



AC  code:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<queue>
#define LL long long
#define MAXN 1000010
#define EPS 1e-9
using namespace std;
int g[11][11],r[11][11],c[11][11],cb[11][11];
char m[11][11];
int fg;
void dfs(int k)
{
	if(k==81)
	{
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				printf("%d",g[i][j]);
			}
			printf("\n");
		}
		fg=1;
	}
	int row=k/9;
	int col=k%9;
	if(g[row][col])
	{
		dfs(k+1);
	}
	else
	{
		int num=row/3*3+col/3;
		for(int i=1;i<=9;i++)
		{
			if(!r[row][i]&&!c[col][i]&&!cb[num][i])
			{
				r[row][i]=1;
				c[col][i]=1;
				cb[num][i]=1;
				g[row][col]=i;
				dfs(k+1);
				if(fg)
					return;
				r[row][i]=0;
				c[col][i]=0;
				cb[num][i]=0;
				g[row][col]=0;
			}
		}
	}
}
int main()
{
	
	int t;
	//freopen("in.txt","r",stdin);
	scanf("%d",&t);
	while(t--)
	{
		memset(g,0,sizeof(g));
    	memset(c,0,sizeof(c));
    	memset(r,0,sizeof(r));
    	memset(cb,0,sizeof(cb));
    	for(int i=0;i<9;i++)
    	{
    		cin>>m[i];
    		for(int j=0;j<9;j++)
    		{
    			g[i][j]=m[i][j]-'0';
    			if(g[i][j])
    			{
    				int num=i/3*3+j/3;
    				cb[num][g[i][j]]=1;
    				c[j][g[i][j]]=1;
    				r[i][g[i][j]]=1;
    				
				}
			}
		}
		fg=0;
		dfs(0);
	}
	return 0;
}


注意:以下这种做法会OLE!!!

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<queue>
#define LL long long
#define MAXN 1000010
#define EPS 1e-9
using namespace std;
int g[11][11],r[11][11],c[11][11],cb[11][11];
char m[11][11];
void dfs(int k)
{
	if(k==81)
	{
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				printf("%d",g[i][j]);
			}
			printf("\n");
		}
		return;
	}
	int row=k/9;
	int col=k%9;
	if(g[row][col])
	{
		dfs(k+1);
	}
	else
	{
		int num=row/3*3+col/3;
		for(int i=1;i<=9;i++)
		{
			if(!r[row][i]&&!c[col][i]&&!cb[num][i])
			{
				r[row][i]=1;
				c[col][i]=1;
				cb[num][i]=1;
				g[row][col]=i;
				dfs(k+1);
				r[row][i]=0;
				c[col][i]=0;
				cb[num][i]=0;
				g[row][col]=0;
			}
		}
	}
}
int main()
{
	
	int t;
	//freopen("in.txt","r",stdin);
	scanf("%d",&t);
	while(t--)
	{
		memset(g,0,sizeof(g));
    	memset(c,0,sizeof(c));
    	memset(r,0,sizeof(r));
    	memset(cb,0,sizeof(cb));
    	for(int i=0;i<9;i++)
    	{
    		cin>>m[i];
    		for(int j=0;j<9;j++)
    		{
    			g[i][j]=m[i][j]-'0';
    			if(g[i][j])
    			{
    				int num=i/3*3+j/3;
    				cb[num][g[i][j]]=1;
    				c[j][g[i][j]]=1;
    				r[i][g[i][j]]=1;
    				
				}
			}
		}
		dfs(0);
	}
	return 0;
}


你可能感兴趣的:(算法,ACM,DFS)