POJ 2676(DFS)

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17100   Accepted: 8323   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. 
POJ 2676(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




题意:给你一个数字矩阵,每行每列每个小矩阵都不允许有重复的数字出现,请用1-9填充这个矩阵



题解:先说思路,因为之前做过这一题,现在又重新写一遍,使用三个数组标记每行每列每个小矩阵数字出现的情况,然后就是暴力枚举填充了


这里有2点个比较不好处理


1:每个矩阵序号不好表示,这里给出公式:((i - 1) / 3) * 3 + (j - 1) / 3 + 1;实际还是不难的,只要慢慢推就好啦


2:这里我们如果直接在矩阵上找0元素的位置,会非常的慢,我们还是使用一个数组记录下来就0元素的位置就好啦,如果递归到数组的大小那么说明已经填充完毕。





#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;
#define N 20
int ok;
int col[N][N], row[N][N],mat[N][N],mp[N][N];
vector<pair<int, int> >point;

int getnum(int i, int j)
{
	return ((i - 1) / 3) * 3 + (j - 1) / 3 + 1;
}

bool judge(int i, int j,int x)
{
	int p = getnum(i, j);
	return !(mat[p][x] || col[i][x] || row[j][x]);
}

void print()
{
	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= 9; j++)
		{
			printf("%d", mp[i][j]);
		}
		puts("");
	}
}

void dfs(int dep)
{
	if (ok)return;
	if (dep == point.size())
	{
		print();
		ok = 1;
		return;
	}
		int x = point[dep].first;
		int y = point[dep].second;
		int p= getnum(x, y);
		for (int k = 1; k <= 9; k++)
		{
			if (judge(x, y, k))
			{
				mat[p][k] = col[x][k] = row[y][k] = 1;
				mp[x][y] = k;
				dfs(dep + 1);
				mat[p][k] = col[x][k] = row[y][k] = 0;
				mp[x][y] = 0;
			}
		}
}

int main()
{
#ifdef CDZSC  
	freopen("i.txt", "r", stdin);
	//freopen("o.txt","w",stdout);  
	int _time_jc = clock();
#endif  
	int t,x;
	char c,g[N][N];
	scanf("%d", &t);
	while (t--)
	{
		ok = 0;
		point.clear();
		memset(col, 0, sizeof(col));
		memset(row, 0, sizeof(row));
		memset(mat, 0, sizeof(mat));
		for (int i = 1; i <= 9; i++)
			scanf("%s", g[i] + 1);
		for (int i = 1; i <= 9; i++)
		{
			for (int j = 1; j <= 9; j++)
			{
				x = g[i][j] - '0';
				mp[i][j] = x;
				if (x != 0)
				{
					col[i][x] = 1;
					row[j][x] = 1;
					int p = getnum(i, j);
					mat[p][x] = 1;
				}
				else
				{
					point.push_back(make_pair(i, j));
				}
			}
		}
		dfs(0);
	}
	return 0;
}






你可能感兴趣的:(POJ 2676(DFS))