In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,

. 2 7 3 8 . . 1 .
. 1 . . . 6 7 3 5
. . . . . . . 2 9
3 . 5 6 9 2 . 8 .
. . . . . . . . .
. 6 . 1 7 4 5 . 3
6 4 . . . . . . .
9 5 1 8 . . . 7 .
. 8 . . 6 5 3 4 .

Given some of the numbers in the grid, your goal is to determine the remaining numbers such that the numbers 1 through 9 appear exactly once in (1) each of nine 3 × 3 subgrids, (2) each of the nine rows, and (3) each of the nine columns.

Input

The input test file will contain multiple cases. Each test case consists of a single line containing 81 characters, which represent the 81 squares of the Sudoku grid, given one row at a time. Each character is either a digit (from 1 to 9) or a period (used to indicate an unfilled square). You may assume that each puzzle in the input will have exactly one solution. The end-of-file is denoted by a single line containing the word “end”.

Output

For each test case, print a line representing the completed Sudoku puzzle.

Sample Input

.2738..1..1...6735.......293.5692.8...........6.1745.364.......9518...7..8..6534.
......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.
end

Sample Output

527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936

思路:DFS解这道题有点极限,需要优化的东西比较复杂,参考了网上的位运算优化,终于以700ms的解决了,如果不位运算优化很难去不超时的去解这道问题

附上原超时代码,此代码修改一下应该可以过F题,单纯的求数独的题

代码:


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<stack>
#include<map>
#define MAX 10005 typedef long long ll; using namespace std; int cnt,flag;
int Map[10][10],vistrow[10][10],vistcol[10][10],vistkaui[10][10][10];
struct node
{
int x,y;
}a[100]; void DFS(int x)
{
if(x == cnt)
{
flag = 1;
return;
}
int I = a[x].x;
int J = a[x].y;
for(int i = 1; i < 10; i++)
{
if(!vistrow[I][i] && !vistcol[J][i] && !vistkaui[I/3][J/3][i])
{
Map[I][J] = i;
vistrow[I][i] = 1;
vistcol[J][i] = 1;
vistkaui[I/3][J/3][i] = 1;
DFS(x+1);
if(!flag)
{
Map[I][J] = '.';
vistrow[I][i] = 0;
vistcol[J][i] = 0;
vistkaui[I/3][J/3][i] = 0;
}
}
}
} int main()
{
int T; while(1)
{
memset(vistrow,0,sizeof(vistrow));
memset(vistcol,0,sizeof(vistcol));
memset(vistkaui,0,sizeof(vistkaui)); cnt = 0;
flag = 0; for(int i = 0; i < 9; i++)
{ for(int j = 0; j < 9; j++)
{
scanf("%c",Map[i][j]); if(Map[i][j] == '.')
{
a[cnt].x = i;
a[cnt++].y = j;
}
else
{
vistrow[i][Map[i][j]-'] = 1;
vistcol[j][Map[i][j]] = 1;
vistkaui[i/3][j/3][Map[i][j]] = 1;
}
}
}
DFS(0);
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
{
if(j == 8)
printf("%c\n",Map[i][j]);
else
printf("%c",Map[i][j]);
}
}
return 0;
} ​

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<stack>
#include<map>
#define MAX 10005 using namespace std;
char Map[10][10];
int vistrow[10],vistcol[10];
int grid[10],rec[512],num[512]; inline int g(int x,int y)
{
return (x/3)*3+y/3;
}
inline void flip(int x,int y,int to)
{
vistrow[x]^=1<<to;
vistcol[y]^=1<<to;
grid[g(x,y)]^=1<<to;
}
bool DFS(int x)
{
if(x==0) return 1;
int minn=10,xx,yy;
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++)
{
if(Map[i][j]=='.')
{
int val=vistrow[i]&vistcol[j]&grid[g(i,j)];
if(!val) return 0;
if(rec[val]<minn) {
minn=rec[val],xx=i,yy=j;
}
}
}
}
int val=vistrow[xx]&vistcol[yy]&grid[g(xx,yy)];
for(;val;val-=val&-val) {
int to=num[val&-val];
Map[xx][yy]=to+'1';
flip(xx,yy,to);
if(DFS(x-1)) return 1;
flip(xx,yy,to);
Map[xx][yy]='.';
}
return 0;
}
int main() {
for(int i=0;i<1<<9;i++) {
for(int j=i;j;j-=j&-j)
rec[i]++;
}
for(int i=0;i<9;i++) {
num[1<<i]=i;
}
char s[100];
while(~scanf("%s",s)&&s[0]!='e') {
for(int i=0;i<9;i++)
vistrow[i]=vistcol[i]=grid[i]=(1<<9)-1;
int tot=0;
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++) {
Map[i][j]=s[i*9+j];
if(Map[i][j]!='.') flip(i,j,Map[i][j]-'1');
else ++tot;
}
}
DFS(tot);
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
printf("%c",Map[i][j]); printf("\n");
}
return 0;
}

数独求解问题(DFS+位运算优化)的更多相关文章

  1. N皇后解法以及位运算优化

    N皇后解法以及位运算优化 观察棋盘,要求皇后之间不能处在同行同列同一条斜线,求使得每行都有一个皇后的放置方法共有多少种. 每尝试放置一个皇后,都可以把该位置所在的行.列标号用一个数组标记,含义表示该行 ...

  2. N皇后-位运算优化

    N皇后问题 时间限制: 5 Sec  内存限制: 128 MB 题目描述 魔法世界历史上曾经出现过一个伟大的罗马共和时期,出于权力平衡的目的,当时的政治理论家波利比奥斯指出:“事涉每个人的权利,绝不应 ...

  3. POJ - 3074 Sudoku (搜索)剪枝+位运算优化

    In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...

  4. UVa 818Cutting Chains (暴力dfs+位运算+二进制法)

    题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...

  5. 模拟赛T5 : domino ——深搜+剪枝+位运算优化

    这道题涉及的知识点有点多... 所以还是比较有意思的. domino 描述 迈克生日那天收到一张 N*N 的表格(1 ≤ N ≤ 2000),每个格子里有一个非 负整数(整数范围 0~1000),迈克 ...

  6. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  7. UVA-818 dfs + 位运算

    暴力枚举一些圆环,将这些圆环解开,看能否成为单链.判断单链的三个条件: 除了这些删除的圆环之外,其他圆环还连接着的圆环不能超过两个. 剩下的环没有连成圈. 剩下的圆环共分成m堆,每堆之间无连接,m必须 ...

  8. [noip模拟题]科技节 - 搜索 - 位运算优化

    [问题描述] 一年一度的科技节即将到来.同学们报名各项活动的名单交到了方克顺校长那,结果校长一看皱了眉头:这帮学生热情竟然如此高涨,每个人都报那么多活动,还要不要认真学习了?!这样不行!……于是,校长 ...

  9. POJ 1164 城堡问题【DFS/位运算/种子填充法/染色法】

    1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # | ...

随机推荐

  1. LoadRunner Controller

    1.Controller的引入 1)需要Controller的原因?需要多个用户来模拟并发的时候. 2)一种强大的.成熟的工具的体现. 2. Controller的启动方式 1)LoadRunner ...

  2. java代理模式与装饰者模式

    静态代理和装饰者模式的区别: 先来看一下装饰者模式的定义:装饰者模式动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 总结一下采用装饰者模式是为了增强或拓展原对象的功能. ...

  3. mysql GROUP_CONCAT 可以将分组的字段进行拼接处理.

    GROUP_CONCAT 可以将分组的字段进行拼接处理. SELECT g.id, g.merchant_id, g. NAME, g.introduction, g.cover_pic, g.pla ...

  4. windows7 Sql server 2012 尝试读取或写入受保护的内存。这通常指示其他内存已损坏的修复

    项目中,使用了sql server2012数据库,服务端是2012,客户端如果是2008的话,就会报错: 索引错误. 没办法,就安装了sql server2012客户端.但是还是报错,无法连上数据库服 ...

  5. 459. Repeated Substring Pattern 判断数组是否由重复单元构成

    [抄题]: Given a non-empty string check if it can be constructed by taking a substring of it and append ...

  6. Java c3p0 连接 MySQL

    <?xml version="1.0" encoding="UTF-8"?> <!-- 需要导入c3p0驱动jar包和mysql驱动jar包 ...

  7. c语言实践 创建两个包含8个元素的double类型数组,第二个元素的每个元素的值都是对应前一个元素的前n个元素的和

    意思就是第二个元素的num[2]等于第一个元素的num[0]+num[1]+num[2] #define COUNT 8 int main(void) { double num1[COUNT]; do ...

  8. Python基础 之列表、字典、元组、集合

    基础数据类型汇总 一.列表(list) 例如:删除索引为奇数的元素 lis=[11,22,33,44,55] #第一种: for i in range(len(lis)): if i%2==1: de ...

  9. SpringMVC——文件的上传

    一.加入依赖 commons-io-2.0.jar commons-fileupload-1.2.1.jar 二.接口MultipartResolver Spring MVC 为文件上传提供了直接的支 ...

  10. SpringMVC——映射请求参数

    Spring MVC 通过分析处理方法的签名,将 HTTP 请求信息绑定到处理方法的相应人参中. @PathVariable @RequestParam @RequestHeader 等) Sprin ...