Sudoku is one of the metaphysical techniques. If you understand the essence of it, you will have the feeling of being immortal, laughing and laughing with joy.............................................................(audience:"We need't such a geomancer!You can get out!!")

  Oh,I'm sorry.Then let's getting down to business as quickly as possible.

The meaning of the problem isn't hard for us to understand.And it is more friendly to those who have played sudoku.But don't worry if you didn't heard about this kind of game,for there's few skills of formal competition.What you have to do is to memorize its basic rules.

  OK.Now,let's think of the solution of this problem.You may want to enumerate all the alternative numbers in the blanks,then check if it's lawful.Good!it's the first algorithm we've thought of.But there's great room for progress.In fact,when people play sudoku,they will write down candidates in each blank.We can record which number we can use in one blank and try using it.To realize it,we can use state compression to save alternative numbers in each row,column and gong.After that,we can choose the blank which has the least candidates to have test-fill until all the blanks are filled.Don't forget to flash back after updating.

  Up to now,we are able to pass POJ2676,but there's still some distance from passing POJ3074.Think of how we search for the blank which has the least candidates and the use of lowbit.We can use an array to record the position of 1 in lowbit(x),and another one array to preprocess the number of 1 in each binary number.My code is below.

  After you get AC in these two sudokus,you can have a try in a harder promblem POJ3076.But you need a more skillful way.Can you find it?

 #include<bits/stdc++.h>

 using namespace std;

 int row[],column[],house[][],h[],cnt[];
char a[][],c;
bool flag; inline int getchoice(int x,int y){
return row[x]&column[y]&house[x/+][y/+];
} inline int lowbit(int x){
return x&-x;
} void dfs(int x,int y){
if(flag) return;
if(x==){
for(int i=;i<=;i++)
for(int j=;j<=;j++) cout<<a[i][j];
cout<<endl;
flag=true;
return;
}
int hx=x/+,hy=y/+;//判断(x,y)在哪一宫
int choice=getchoice(x,y);
while(choice){
int num=lowbit(choice);
choice-=num;
int row0=row[x],column0=column[y],house0=house[hx][hy];
row[x]&=~num;column[y]&=~num;house[hx][hy]&=~num;
a[x][y]=h[num]+'';
int minn=,xx=,yy;
for(int i=;i<=;i++)
for(int j=;j<=;j++){
if(a[i][j]!='.') continue;
int cal=cnt[getchoice(i,j)];
if(cal<minn){
xx=i;yy=j;
minn=cal;
}
}
dfs(xx,yy);
row[x]=row0;column[y]=column0;house[hx][hy]=house0;
}
a[x][y]='.';
} int main(){
for(int i=;i<=;i++) h[<<i]=i;//预处理lowbit(x)后x中1的位置(即2^n中n的值)
for(int i=;i<;i++)
for (int j=i;j;j-=lowbit(j))
cnt[i]++;//题解中的好方法,预处理每一个二进制数中1的数量;
cin>>c;
while(c!='e'){
for(int i=;i<=;i++)
for(int j=;j<=;j++)
row[i]=column[j]=house[i/+][j/+]=;
a[][]=c;
for(int j=;j<=;j++) scanf("%c",&a[][j]);
for(int i=;i<=;i++)
for(int j=;j<=;j++) scanf("%c",&a[i][j]);
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(a[i][j]!='.'){
int num=a[i][j]-'';
num=pow(,num);
row[i]&=~num;
column[j]&=~num;
house[i/+][j/+]&=~num;//预处理每行、每列、每宫可用的数字
}
flag=false;
int minn=,xx=,yy;
for(int i=;i<=;i++)
for(int j=;j<=;j++){
if(a[i][j]!='.') continue;
int cal=cnt[getchoice(i,j)];
if(cal<minn){
xx=i;yy=j;
minn=cal;
}
}
dfs(xx,yy);
cin>>c;
}
return ;
}

Sudoku(POJ2676/3074)的更多相关文章

  1. POJ 3074 Sudoku (Dancing Links)

    传送门:http://poj.org/problem?id=3074 DLX 数独的9*9的模板题. 具体建模详见下面这篇论文.其中9*9的数独怎么转化到精确覆盖问题,以及相关矩阵行列的定义都在下文中 ...

  2. POJ 2676 Sudoku(深搜)

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

  3. HDU - 5547 Sudoku(数独搜索)

    Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself ...

  4. UESTC - 1222 Sudoku(深搜)

    Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks l ...

  5. HDU 3111 Sudoku(精确覆盖)

    数独问题,输入谜题,输出解 既然都把重复覆盖的给写成模板了,就顺便把精确覆盖的模板也写好看点吧...赤裸裸的精确覆盖啊~~~水一水~~~然后继续去搞有点难度的题了... #include <cs ...

  6. LeetCode 36 Valid Sudoku(合法的数独)

    题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description   给出一个二维数组,数组大小为数独的大小,即9*9  其中,未填入 ...

  7. Sudoku(简单DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547 数据比较少,直接暴力DFS,检验成立情况即可 AC代码:但是不知道为什么用scanf,print ...

  8. 【POJ - 2676】Sudoku(数独 dfs+回溯)

    -->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...

  9. sudoku 心得 视觉消除法(Visual Elimination)

    虽然我是程序员,但这里只介绍人类的思维方法. 这个方法我是从这里看到的: https://www.learn-sudoku.com/visual-elimination.html Most peopl ...

随机推荐

  1. keepalived添加服务自启动报错分析

    安装完keepalived后设置为服务自启动 将路径为/usr/local/src/keepalived-1.3.4/keepalived/etc/init.d的文件keepalived拷贝到/etc ...

  2. 堡垒机升级V3.2.14

  3. WMS程序部署

    UI部署UI-20190107-landor-修改什么BUG.JAR162\163 APP部署 外部JSP部署 备份META这个SCHEMA

  4. NIO学习笔记

    零.前言 这里整理摘录了我了解NIO的一些笔记. 参考资料: 1.深入浅出NIO之Channel.Buffer 2.深入浅出NIO之Selector实现原理 3.Java NIO vs. IO 一.N ...

  5. FM(Factorization Machines)模型详解

    优点 FM模型可以在非常稀疏的数据中进行合理的参数估计,而SVM做不到这点 在FM模型的复杂度是线性的,优化效果很好,而且不需要像SVM一样依赖于支持向量. FM是一个通用模型,它可以用于任何特征为实 ...

  6. ABAP的匹配

    ABAP的匹配 通配符 字符串操作中的通配符 *:多位字符的通配符 +:一位字符的通配符 #:字符操作中的转义符 REPORT ztest_placeholder. DATA:l_name(8) TY ...

  7. ReentrantLock 实现

    ReentrantLock 实现:   关于锁的操作都是依赖于state 的值,当state =0 时候,表示 线程可以获取锁,state =1 表示锁已经占用,等待释放 获取锁的方法: protec ...

  8. Solidity-让合约地址 接受ETH的转账充值的 三种方式

    以太坊智能合约开发:让合约接受转账 在以太坊智能合约开发中,通常会有向合约地址进行转账的需求,那么有几种向合约地址进行转账的方式呢? 有三种方式: 部署合约时转账 调用合约提供的方法 直接向合约地址进 ...

  9. dotnet run 提示System.Net.Sockets.SocketException (10049): 在其上下文中,该请求的地址无效。

    更换端口号试一下. 查看官方文档 PS: 使用帮助命令 -h,可以指定启动配置文件: dotnet run --launch-profile  xxx 例如下面的配置文件,假如我们要使用codes-t ...

  10. windows -休眠

    查询服务器执行的睡眠状态 powercfg -a 开始休眠方法:手工键入如下命令: powercfg -hibernate on 命令执行之后立即就可以生效,无需要重新启动系统,再次执行“powerc ...