POJ 2676 Sudoku dfs

题意:九宫格游戏。给定一个九宫格,他的每一行每一列以及每一个3*3的子格都必须由1,2,3,4,5,6,7,8,9组成。不能多也不能少。求出一种满足条件的填充方案。

题解:貌似正搜得效率比反搜差不少。不过没关系,道理理解可就ok了。

#include <iostream>
using namespace std;

#define N 11
char str[N];
int map[N][N];
bool row[N][N], col[N][N], sub[N][N];
/* row[i][j]表示第i行的数j已经存在, col[i][j]表示第i列的数j已经存在,sub[i][j]同理 */
bool dfs ( int k )
{
	if ( k < 0 )
		return true;

	int r = k / 9;
	int c = k % 9;
	int sub_id = (r / 3) * 3 + c / 3;

	if ( map[r][c] != -1 )
		return dfs ( k - 1 );

	int temp = map[r][c];
	for ( int i = 0; i < 9; i++ )
	{
		if ( ! row[r][i] && ! col[c][i] && ! sub[sub_id][i] )
		{
			map[r][c] = i;
			row[r][i] = col[c][i] = sub[sub_id][i] = true;
			if ( dfs ( k - 1 ) )
				return true;
			map[r][c] = temp;
			row[r][i] = col[c][i] = sub[sub_id][i] = false;
		}
	}
	return false;
}

int main()
{
	int t, sub_id, i, j;
	scanf("%d",&t);
	while ( t-- )
	{
		memset(row,0,sizeof(row));
		memset(col,0,sizeof(col));
		memset(sub,0,sizeof(sub));

		for ( i = 0; i < 9; i++ )
		{
			scanf("%s",str);
			for ( j = 0; j < 9; j++ )
			{
				map[i][j] = str[j] - '0' - 1; /* 数字取的是0-8,所以输出时要加1 */
				if ( map[i][j] < 0 ) continue;
				sub_id = (i / 3) * 3 + j / 3;
				row[i][map[i][j]] = true;
				col[j][map[i][j]] = true;
				sub[sub_id][map[i][j]] = true;
			}
		}

		if( dfs ( 80 ) )
		{
			for ( i = 0; i < 9; i++ )
			{
				for ( j = 0; j < 9; j++ )
					printf("%d",map[i][j]+1);
				putchar('\n');
			}
		}
	}
	return 0;
}



 

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