ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-06-05 19:07:08  阅读:204  来源: 互联网

标签:cl Sudoku phot 10 int each dfs 2676 row


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.

在这里插入图片描述
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;
		} 
	}
}

标签:cl,Sudoku,phot,10,int,each,dfs,2676,row
来源: https://blog.51cto.com/u_15249461/2870441

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有