Sudoku(POJ2676/3074)
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)的更多相关文章
- POJ 3074 Sudoku (Dancing Links)
传送门:http://poj.org/problem?id=3074 DLX 数独的9*9的模板题. 具体建模详见下面这篇论文.其中9*9的数独怎么转化到精确覆盖问题,以及相关矩阵行列的定义都在下文中 ...
- POJ 2676 Sudoku(深搜)
Sudoku Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submi ...
- HDU - 5547 Sudoku(数独搜索)
Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself ...
- 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 ...
- HDU 3111 Sudoku(精确覆盖)
数独问题,输入谜题,输出解 既然都把重复覆盖的给写成模板了,就顺便把精确覆盖的模板也写好看点吧...赤裸裸的精确覆盖啊~~~水一水~~~然后继续去搞有点难度的题了... #include <cs ...
- LeetCode 36 Valid Sudoku(合法的数独)
题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入 ...
- Sudoku(简单DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547 数据比较少,直接暴力DFS,检验成立情况即可 AC代码:但是不知道为什么用scanf,print ...
- 【POJ - 2676】Sudoku(数独 dfs+回溯)
-->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...
- sudoku 心得 视觉消除法(Visual Elimination)
虽然我是程序员,但这里只介绍人类的思维方法. 这个方法我是从这里看到的: https://www.learn-sudoku.com/visual-elimination.html Most peopl ...
随机推荐
- gitlab中批量删除本地以及远程tag的操作
git 批量删除标签# 删除所有远程标签git show-ref --tag | awk '{print ":" $2}' | xargs git push origin # 删除 ...
- “AS3.0高级动画编程”学习:第二章转向行为(上)
因为这一章的内容基本上都是涉及向量的,先来一个2D向量类:Vector2D.as (再次强烈建议不熟悉向量运算的童鞋,先回去恶补一下高等数学-07章空间解释几何与向量代数.pdf) 原作者:菩提树下的 ...
- 简单的页面互点Javascript代码
简单的页面互点Javascript代码,可以适用于前端$(function(){ $('.ip_b_con_item li,.pro_index_list li').mouseover(functio ...
- TT-付款方式
付款方式 一般T/T是可以的,上述客户我们采用的付款方式就是定金加TT,如果是信用证方式,那么需要资信良好的开证行: 中国工商银行 汇丰银行 渣打银行 花旗银行 MUSLIM COMMERCIAL B ...
- 小强学渲染之Unity Shader噪声应用
之前玩Tencent的仙剑4手游时,杀死boss会看到boss有“消融”的效果,就是身体上有多个洞洞然后往四周扩散直至尸体完全消失,但效果是没有关闭背面剔除的“穿帮”效果,可能也是考虑性能因素. em ...
- “新智认知”杯上海高校程序设计竞赛暨第十七届上海大学程序设计春季联赛(D题,贪心+栈)
链接:https://ac.nowcoder.com/acm/contest/551/D来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10485 ...
- 通信导论-IP数据网络基础(4)
IP地址的编址方法--IP地址+掩码地址=网络地址 分类的IP地址 每一类地址都由两个固定长度的字段组成,其中一个字段是网络号 net-id,标志主机或路由器所连接到的网络,另一个字段则是主机号 ho ...
- POJ—1321(棋盘问题)
题目链接:https://cn.vjudge.net/contest/65959#problem/A 入门dfs,给一张地图,由“#”和“.”组成,“#”处可以放棋子,且棋子不能同行同列,问放满所有“ ...
- Jenkins+Gradle+Docker打docker镜像包上传至s3
gradle打包跟maven打包的环境搭建有相似之处,可参考maven打包https://www.cnblogs.com/chenchen-tester/p/6408815.html 进入Jenkin ...
- Tomcat7 目录详解
1.bin:该目录下存放的是二进制可执行文件,如果是安装版,那么这个目录下会有两个exe文件:tomcat6.exe.tomcat6w.exe,前者是在控制台下启动Tomcat,后者是弹出UGI窗口启 ...