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的更多相关文章

  1. POJ3074 Sudoku(lowbit优化搜索)

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

  2. POJ3074 Sudoku —— Dancing Links 精确覆盖

    题目链接:http://poj.org/problem?id=3074 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  3. POJ3074 Sudoku 舞蹈链 DLX

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的数独,求解. 题解 DLX + 矩阵构建  (两个传送门) 代码 #include & ...

  4. POJ3074 Sudoku 剪枝深(神?)搜

    emm...挺秀的...挺神的? 每行,每列,每宫用一个二进制数表示选或没选的状态,刚开始设没选为1,然后更改状态的时候异或一下就好了: 这样可以通过lowbit取出每一个没有选过的数:(妙啊? 关于 ...

  5. [poj3074]Sudoku(舞蹈链)

    题目链接:http://poj.org/problem?id=3074 舞蹈链精确覆盖的经典题目,一个数独每个位置的要求,可以得到以下四个约束1.每个位置有且只有一个数字2.每个位置的数字在一行只能出 ...

  6. 【转】Dancing Links题集

    转自:http://blog.csdn.net/shahdza/article/details/7986037 POJ3740 Easy Finding [精确覆盖基础题]HUST1017 Exact ...

  7. Dancing Links 专题总结

    算法详细:Dancing Links博客 1.精确覆盖: ZOJ3209 Treasure Map HUST1017 Exact cover POJ3074 Sudoku 2.可重复覆盖: HDU22 ...

  8. dancing links 题集转自夏天的风

    POJ3740     Easy Finding [精确覆盖基础题] HUST1017    Exact cover [精确覆盖基础] HDOJ3663 Power Stations [精确覆盖] Z ...

  9. 【POJ3074】Sudoku DLX(Dancing Links)

    数独就要DLX,不然不乐意. 数独的DLX构造:9*9个点每一个点有9种选择,这构成了DLX的729行,每行.列.阵有限制,均为9行(/列/阵),然后每行(/列/阵)都有九种数的情况.于是就有了3*9 ...

随机推荐

  1. Navicat修改查询保存路径

    mysql使用navicat查询时有时候会有很多sql语句, ctrl+s时自动保存在C:\Users\Administrator\Documents\Navicat\MySQL\servers\lo ...

  2. [Plan]计划

    1. scala 2. kafka 1. lua 2. openResty 1. 日志收集 - python 2. 代码生成 3. 权限系统

  3. Openssl ecparam命令

    一.简介 椭圆曲线密钥参数生成及操作 二.语法 openssl ecparam [-inform DER|PEM] [-outform DER|PEM] [-in filename] [-out fi ...

  4. easyui 获取当前页签选中的名称

    parent.parent.$("#tabs").tabs('getSelected').panel('options').title == "收藏夹管理"

  5. hra控件自动绑定

    1.前台js代码 $.ajax({ type: "post", url: 'AlmMarketScenarioDetailManage.aspx?_method=queryPane ...

  6. maven配置logback

    [背景] 刚接触大数据项目,在生产环境中经常需要使用日志来判定一些问题的原因. 一直以来都在使用System.out.println的标准输出来往控制台上打印日志.这种方法对性能影响很大不说,查看日志 ...

  7. DESC和 ACS

    用 DESC 表示按倒序排序(即:从大到小排序)用 ACS 表示按正序排序(即:从小到大排序)

  8. C# WebService中任务处理线程创建子线程后

    protected void WriteLog(string message) { lock (lockObject) { var file = System.IO.File.AppendText(& ...

  9. CodeForces 681D Gifts by the List (树上DFS)

    题意:一个家庭聚会,每个人都想送出礼物,送礼规则是, 一个人,先看名单列表,发现第一个祖先 就会送给他礼物,然后就不送了,如果他没找到礼物 他会伤心的离开聚会!告诉你m个祖先关系, 和每个人想给谁送! ...

  10. ScreenCapturePro2 for Joomla_3.4.7-tinymce4x

    1.1. 与Joomla_3.4.7-tinymce4x整合 示例下载:Joomla_3.4.7,   1.1.1. 添加screencapture文件夹   1.1.2. 2.添加插件文件夹 路径: ...