POJ-2676 Sudoku(简单数独-dfs深搜)

Sudoku

Time Limit: 2000MS Memory Limit: 65536K

题目链接http://poj.org/problem?id=2676

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 Sudoku(简单数独-dfs深搜)
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


题目大意:给你一个未完成的数独,让你找到一个方案,用1-9填满整个数独,使得9 *9的方块里每行不重复,每列不重复,每个3 * 3小块中的数不重复

3个不重复,那么我们就定义3个维护的数组,row,c,s(分别为行,列,块),接下来就是dfs填数了。。。块的维护可能有点不太明白怎么写,但没关系,我们直接无脑判断就行了:

int block(int a,int b) {
	if (a<=3) {
		if (b<=3) return 1;      //第一块区域
		else if (b<=6) return 2;
		else return 3;
	} else if (a<=6) {
		if (b<=3) return 4;
		else if (b<=6) return 5;
		else return 6;
	} else {
		if (b<=3) return 7;
		else if (b<=6) return 8;
		else return 9;
	}
}

这样一来s[block(x,y)]就是它的块状区域了(x代表行,y代表列)。之后的填数就变成了普通的搜索了。代码具体如下:

#include <cstdio>
#include <cstring>
int row[10][10],c[10][10],s[10][10],phot[10][10],mark;
int block(int a,int b);
void dfs(int r,int cl);
int main() {
	int n;
	scanf ("%d",&n);
	while (n) {
		n--;
		mark=0;
		memset(row,0,sizeof(row));
		memset(c,0,sizeof(c));
		memset(s,0,sizeof(s));
		memset(phot,0,sizeof(phot));
		for (int i=1; i<=9; i++)
			for (int j=1; j<=9; j++) {
				int ch;
				ch=getchar();
				while (ch<'0' || ch>'9') ch=getchar();
				phot[i][j]=ch-'0';
				if (phot[i][j]) {
					row[i][phot[i][j]]=1;
					c[j][phot[i][j]]=1;
					s[block(i,j)][phot[i][j]]=1;
				}
			}
		dfs(1,1);
		for (int i=1; i<=9; i++) {
			for (int j=1; j<=9; j++)
				printf ("%d",phot[i][j]);
			printf ("\n");
		}
	}
	return 0;
}
int block(int a,int b) {
	if (a<=3) {
		if (b<=3) return 1;
		else if (b<=6) return 2;
		else return 3;
	} else if (a<=6) {
		if (b<=3) return 4;
		else if (b<=6) return 5;
		else return 6;
	} else {
		if (b<=3) return 7;
		else if (b<=6) return 8;
		else return 9;
	}
}
void dfs(int r,int cl) {
	if (cl>9) cl=1,r++;
	if (r>9) {
		mark=1;
		return;
	}
	if (mark) return;
	while (phot[r][cl]) {
		cl++;
		if (cl>9) dfs(r+1,1);
	}
	for (int j=1; j<=9; j++) {
		if (mark) return;
		else if (!phot[r][cl] && !row[r][j] && !c[cl][j] && !s[block(r,cl)][j]) {
			phot[r][cl]=j;
			row[r][j]=1;
			c[cl][j]=1;
			s[block(r,cl)][j]=1;
			dfs(r,cl+1);
			if (mark) return;       //找到一个解后直接返回,不用再回溯了,节约时间
			row[r][j]=0;
			c[cl][j]=0;
			s[block(r,cl)][j]=0;
			phot[r][cl]=0;
		} 
	}
}
上一篇:Android发送mail的源码


下一篇:AlphaFold2代码阅读(四)