POJ 2676-Sudoku(DFS)

Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14120   Accepted: 6976   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
深搜。。
题意:就是传统的数独游戏。。找出一种方案即可。。有spj
对 行,列,以及9个3*3的方块哈希。。然后暴力深搜就行了。。实践证明倒搜比较快
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
char ma[11][11];
bool row[11][11],col[11][11],mar[11][11],ok;
int change(int x,int y)
{
	if(x>=1&&x<=3) return y%3==0?y/3:y/3+1;
	else if(x>=4&&x<=6) return 3+(y%3==0?y/3:y/3+1);
	else return 6+(y%3==0?y/3:y/3+1);
}
void dfs(int num)
{
	if(ok) return ;
	if(num==0)
	{
		for(int i=1;i<=9;i++)
		{
			for(int j=1;j<=9;j++)
				putchar(ma[i][j]);
			puts("");
		}
		ok=1;
		return ;
	}
	int x=num%9==0?num/9:num/9+1;
	int y=num%9==0?9:num%9;
	if(ma[x][y]-'0')
		dfs(num-1);
	else
	{
		for(int i=1;i<=9;i++)
		{
			if(!row[x][i]&&!col[y][i]&&!mar[change(x,y)][i])
			{
				ma[x][y]=i+'0';row[x][i]=1;col[y][i]=1;mar[change(x,y)][i]=1;
				dfs(num-1);
				ma[x][y]='0';row[x][i]=0;col[y][i]=0;mar[change(x,y)][i]=0;
			}
		}
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(row,0,sizeof(row));
		memset(col,0,sizeof(col));
		memset(mar,0,sizeof(mar));
		for(int i=1;i<=9;i++)
			scanf("%s",ma[i]+1);
			for(int i=1;i<=9;i++)
				for(int j=1;j<=9;j++)
				if(ma[i][j]-'0')
				{
					row[i][ma[i][j]-'0']=1;
					col[j][ma[i][j]-'0']=1;
					mar[change(i,j)][ma[i][j]-'0']=1;
				}
		ok=0;dfs(81);
	}
	return 0;
}
时间: 2024-10-14 07:37:28

POJ 2676-Sudoku(DFS)的相关文章

poj 2676 Sudoku (dfs)

链接:poj 2676 题意:给定一个未完成的数独,0是待填位置,其他均为已填入的数字.如果能将其 补充完整,则输出补充完整的数独(有多组答案输出任意一组),否则原样输出 数独:一个9行9列的网格,包括9个3*3的子网格,要求每行.每列.每个子网格内 都只能使用一次1-9中的一个数字, 即每行.每列.每个子网格内都不允许出现相同的数字. 分析:对于每一个未填的格,依次判断它所在行.列.子网格是否填了1-9, 若都未填,先填上该值,继续搜索, 若无法填写了,再回溯,填上其他可能的值,继续搜索 看别

POJ 2676 - Sudoku(数独)

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2676 题目大意: 给出一个数字n,表示有n组数据. 每组数据包括一个n*n的数独,0表示未填的数字.数字之间没有空格. 输出填好的数独,使得每行.每列.每个九宫格中的数字都不重复. 分析: 经典搜索题目. 数组一定要开大一点,数组一定要开大一点,数组一定要开大一点. AC代码: 1 #include<cstdio> 2 #include<algorith

poj 1724 ROADS(dfs)

http://poj.org/problem?id=1724 大致题意:N个城市由R条单向路连通,每条路(S,D)之间有两个因素:路的长度L和路的花费T.现要从城市1到达城市N,求花费在K以内的最短路程. 思路:很明显的dfs(他们都说很明显的spfa...).不过dfs有几点注意的地方: 建立邻接表不能用vector存,要用链表的形式,采用头插法. dfs的时候,在递归节点v之前,要先预判断一下到达v之后总花费是否大于k,若大于K就跳过,不必再调用v节点,这样会省很多时间.对于路程的处理也同样

poj 2676 Sudoku (基础DFS)

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

POJ 2676 Sudoku (数独 DFS)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   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

POJ 1190 生日蛋糕(DFS)

生日蛋糕 Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit Status Description 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱.当i < M时,要求Ri > Ri+1且Hi > Hi+1.由于要在蛋糕上抹奶油,为尽

poj2676 Sudoku(DFS)

做了很久还是参考了别人的答案orz,其实也不难啊.我要开始学一下怎么写搜索了... 题目链接:poj2676 Sudoku 题解:暴力搜索,DFS每个空白格子所放数字. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 bool row_f[9][

poj Sudoku(数独) DFS

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

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string