POJ3074 Sudoku
POJ3074 Sudoku
- 与POJ2676相比,这一题搜索时每一步都找到最好确定的点进行枚举
- 对于每行、每列、每个九宫格,都分别用一个9位二进制数保存还有那些数还可以填
- 对于每个位置,将其所在行、列、九宫格所对应的二进制数进行或运算即可得到该位置能填哪些数,用lowbit运算(取出最低的为1的数位)即可吧能填的数字取出。
- 其他见代码
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
#include <cctype>
using namespace std; #define res register int
inline int read() {
int x(),f(); char ch;
while(!isdigit(ch=getchar())) if(ch=='-') f=-;
while(isdigit(ch)) x=x*+ch-'',ch=getchar();
return f*x;
}
int id[][];//打表算出i,j所在的九宫格
inline void pre_work()
{
for(res i= ; i< ; i++)
{
for(res j= ; j< ; j++) id[i][j]=;
for(res j= ; j< ; j++) id[i][j]=;
for(res j= ; j< ; j++) id[i][j]=;
}
for(res i= ; i< ; i++)
{
for(res j= ; j< ; j++) id[i][j]=;
for(res j= ; j< ; j++) id[i][j]=;
for(res j= ; j< ; j++) id[i][j]=;
}
for(res i= ; i< ; i++)
{
for(res j= ; j< ; j++) id[i][j]=;
for(res j= ; j< ; j++) id[i][j]=;
for(res j= ; j< ; j++) id[i][j]=;
}
} char str[][];
int row[],col[],grid[],cnt[],num[],tot; inline void flip(int x,int y,int z)
{
row[x]^=<<z; col[y]^=<<z;
grid[id[x][y]]^=<<z;
} bool dfs(int now)
{
if(!now) return ;
int tmp=,x,y;
//找最好算的位置
for(res i= ; i< ; i++)
for(res j= ; j< ; j++)
{
if(str[i][j]!='.') continue;
int val=row[i]&col[j]&grid[id[i][j]];//不能确定的数的个数
if(cnt[val]<tmp) {
tmp=cnt[val],x=i,y=j;
}
}
int val=row[x]&col[y]&grid[id[x][y]];
for(;val;val-=val&-val)
{
int z=num[val&-val];//要填的数
str[x][y]=''+z; //注意是1
flip(x,y,z);
if(dfs(now-)) return true;
flip(x,y,z);
str[x][y]='.';
}
return false;
} int main()
{
pre_work();
//为了减少空间,用0~8表示1~9
for(res i= ; i<<< ; i++)
for(res j=i ; j ; j-=j&-j) cnt[i]++;
for(res i= ; i< ; i++) num[<<i]=i;
char s[];
while(~scanf("%s",s) && s[]!='e')
{
for(res i= ; i< ; i++)
for(res j= ; j< ; j++) str[i][j]=s[i*+j];
for(res i= ; i< ; i++) row[i]=col[i]=grid[i]=(<<)-;
tot=;
for(res i= ; i< ; i++)
for(res j= ; j< ; j++)
if(str[i][j]!='.') flip(i,j,str[i][j]-'');//注意这里是-1,因为是0~8
else tot++;
dfs(tot);
for(res i= ; i< ; i++)
for(res j= ; j< ; j++) s[i*+j]=str[i][j];
puts(s);
} return ;
}
POJ3074 Sudoku的更多相关文章
- POJ3074 Sudoku(lowbit优化搜索)
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...
- POJ3074 Sudoku —— Dancing Links 精确覆盖
题目链接:http://poj.org/problem?id=3074 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- POJ3074 Sudoku 舞蹈链 DLX
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的数独,求解. 题解 DLX + 矩阵构建 (两个传送门) 代码 #include & ...
- POJ3074 Sudoku 剪枝深(神?)搜
emm...挺秀的...挺神的? 每行,每列,每宫用一个二进制数表示选或没选的状态,刚开始设没选为1,然后更改状态的时候异或一下就好了: 这样可以通过lowbit取出每一个没有选过的数:(妙啊? 关于 ...
- [poj3074]Sudoku(舞蹈链)
题目链接:http://poj.org/problem?id=3074 舞蹈链精确覆盖的经典题目,一个数独每个位置的要求,可以得到以下四个约束1.每个位置有且只有一个数字2.每个位置的数字在一行只能出 ...
- 【转】Dancing Links题集
转自:http://blog.csdn.net/shahdza/article/details/7986037 POJ3740 Easy Finding [精确覆盖基础题]HUST1017 Exact ...
- Dancing Links 专题总结
算法详细:Dancing Links博客 1.精确覆盖: ZOJ3209 Treasure Map HUST1017 Exact cover POJ3074 Sudoku 2.可重复覆盖: HDU22 ...
- dancing links 题集转自夏天的风
POJ3740 Easy Finding [精确覆盖基础题] HUST1017 Exact cover [精确覆盖基础] HDOJ3663 Power Stations [精确覆盖] Z ...
- 【POJ3074】Sudoku DLX(Dancing Links)
数独就要DLX,不然不乐意. 数独的DLX构造:9*9个点每一个点有9种选择,这构成了DLX的729行,每行.列.阵有限制,均为9行(/列/阵),然后每行(/列/阵)都有九种数的情况.于是就有了3*9 ...
随机推荐
- Quartz 官网翻译(转载)
Paths中的几个重要元素 Points void CGContextMoveToPoint ( CGContextRef c, CGFloat x, CGFloat y ); 指定 ...
- 深入理解mysql的隔离级别
建表插入测试数据A> create table test(id int ,num int) ;Query OK, 0 rows affected (0.53 sec) A> insert ...
- Anaconda 安装和配置
Anaconda 安装和配置 1. Anaconda 安装 Anaconda说明及安装过程:Anaconda详细安装使用教程 2. Anaconda和Pip源修改 Anaconda源修改:打开Anac ...
- Eclipse集成tomcat
1.window --> Preferences 2.搜索runtime,选择Runtime Environments,点击add按钮 3.选择tomacat的版本,我的版本是7.x的,所以选择 ...
- MySQL 存储过程和存储函数学习
#一.存储过程和存储函数的创建案例 CREATE PROCEDURE myprocedure(in a int,in b int ,OUT c INT) BEGIN set c=a+b; end; c ...
- 【转】Java多线程编程(十)-并发编程原理(分布式环境中并发问题)
转载地址:http://blog.csdn.net/leicool_518/article/details/42268947 在分布式环境中,处理并发问题就没办法通过操作系统和JVM的工具来解决,那么 ...
- UVALive 7752 Free Figurines (瞎搞)
题意:给定 n 个盒子,然后告诉你每个盒子在哪个盒子里,数值越大,盒子越大,给定你初态,和末态,问你最少要几步能完成,只有两种操作,一种是把一个盒子连同里面的小盒子放到一个空盒子里,另一种是把一个堆盒 ...
- jquery cookie用法
jquery cookie用法(获取cookie值,删除cookie) cookie在jquery中有指定的cookie操作类,下面我先来介绍我们在使用cookie操作类时的一些问题,然后介绍正确的使 ...
- copymemory()数组赋值
在各网站的文章里面,见复制数据的方法中,有move的,有system.copy的,而要实际应用中,这两种方法,并不是很完美,会遇到一些问题,比如copy在记录里面的复制时,编译都过不去,而CopyMe ...
- [LeetCode 题解]: LetterCombinations
Given a digit string, return all possible letter combinations that the number could represent. A map ...