POJ-2676 Sudoku (深搜枚举一种)

Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23374   Accepted: 10937   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. 

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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int  sudoku[15][15];
bool square[15][15];     //标记每个小方格出现的数字
bool checkrow[15][15];   //标记没行出现的数字
bool checkcol[15][15];   //标记没列出现的数字
bool isdone;
char str[15][15];
void dfs(int a, int b)   //a,b传递的就是第a行,第b列的地址
{
    int i,j;
    int num;
    if(isdone)
    return;
	if(a==10)             //终止条件就是已经构造完9行。
	{
		isdone=true;
		for(i=1;i<=9;i++)
		{
			for(j=1;j<=9;j++)
            cout<<sudoku[i][j];
			cout<<endl;
		}
		return;
	}
	if(sudoku[a][b])   //从左到右,从上到下搜索。
	{
		if(b==9)
        dfs(a+1,1);
		else
        dfs(a,b+1);
	}
	else
	{
		for(num=1;num<=9;num++)           //从1到9这9个数进行枚举
		{
			int k=3*((a-1)/3)+(int)ceil(1.0*b/3); //该点位于哪个小方格
                                         //妙。
                                         //一行三个,自然就要乘三。
			if(!checkrow[a][num]&&!checkcol[b][num]&&!square[k][num]) //大行,大列,3*3的矩阵
			{                                                         //这些都是下标表示其含义。
				sudoku[a][b]     = num;
				checkrow[a][num] = 1;
			    checkcol[b][num] = 1;
				square[k][num]   = 1;
				if(b==9)
                dfs(a+1,1);
				else
                dfs(a,b+1);

				sudoku[a][b]=0;
				checkrow[a][num]=0;
			    checkcol[b][num]=0;
				square[k][num]=0;    //因为这不是搜索,是在套数,所以要枚举所有情况
                                     //自然要回溯重置为0.
			}                        //枚举所有情况的话,找到一个满足条件,就要全return;
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	int i,j;
	while(t--)
	{
	    isdone=false;
		memset(square,0,sizeof(square));
		memset(checkcol,0,sizeof(checkcol));
		memset(checkrow,0,sizeof(checkrow));
		memset(sudoku,0,sizeof(sudoku));
	    for(i=1;i<=9;i++)
        {
          getchar();
          for(j=1;j<=9;j++)
          {
              scanf("%c",&str[i][j]);
              sudoku[i][j]=str[i][j]-'0';
              if(sudoku[i][j]!=0)
              {
                int k=3*((i-1)/3)+(int)ceil(1.0*j/3);
                checkrow[i][sudoku[i][j]]=1;
                checkcol[j][sudoku[i][j]]=1;
                square[k][sudoku[i][j]]=1;
              }
           }
         }
		 dfs(1,1);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xigongdali/article/details/81198101