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 ...
随机推荐
- 远程连接MySQL数据库报错:is not allowed to connect to this MYSQL server的解决办法
1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入MySQL后,更改 "mysql" 数据库里的 " ...
- Video组件:控制视频的播放与暂停
来自<sencha touch权威指南>第10章,315页开始 app.js代码如下: Ext.require(['Ext.Video','Ext.MessageBox','Ext.Too ...
- .net 自定义AOP,透明代理与真实代理
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- STM32F4库函数里面的RCC_DeInit(void)函数分析
void RCC_DeInit(void) { /*Set HSION bit,内部高速时钟使能HSI振荡器打开*/ RCC->CR |=(uint32_t)0x00000001; /*Rese ...
- Ubuntu下安装配置java及环境变量
这里的办法不是在线安装,因为需要更新源(你懂的,费时费事~),so这里介绍在Ubuntu上手动下载安装配置Java环境变量 *系统:Ubuntu 16.4 1.下载jdk,直接用系统的Firefox浏 ...
- 重叠IO
一. 异步IO 说到重叠模型首先还是提一下异步IO比较好,因为从本质上讲,重叠模型也是一种异步IO模型. 我们知道,相对于计算机执行的其他操作而言,设备IO(文件.管道.套接 ...
- GC分析中提到的根对象是什么
一些文章在分析GC时,不可逾越的说到要先从根对象扫描出不可达对象,然后标记那些不可达对象为垃圾.那么源头根对象是什么玩意呢? 几分钟后google到比较可信源是http://stackoverflow ...
- 卸载超级兔子后,word打不开(无法创建工作文件),VS2010也没法用(找不到CL.exe)。
又折腾了一上午,昨天用优化大师和超级兔子整理了电脑,今天来到实验室,vs打开后报错,提示"找不到CL.exe,"(具体提示忘记了,就是找不到CL.exe),打开word2010也是 ...
- 关于提交表单时添加自定义值的方式:data中值可绑定function
表单提交时新增自定义值: $.ajaxForm(){ data:{aaa:"12"} } 但是这个是在初始化的时候就绑定进去的,所以值是固定的初始化时候的值,若想添加动态值,可以这 ...
- Retrofit+Rxjava observable转javabean失败
报错提示: Caused by: java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex. ...