POJ - 3074 Sudoku (搜索)剪枝+位运算优化
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,
. | 2 | 7 | 3 | 8 | . | . | 1 | . |
. | 1 | . | . | . | 6 | 7 | 3 | 5 |
. | . | . | . | . | . | . | 2 | 9 |
3 | . | 5 | 6 | 9 | 2 | . | 8 | . |
. | . | . | . | . | . | . | . | . |
. | 6 | . | 1 | 7 | 4 | 5 | . | 3 |
6 | 4 | . | . | . | . | . | . | . |
9 | 5 | 1 | 8 | . | . | . | 7 | . |
. | 8 | . | . | 6 | 5 | 3 | 4 | . |
Given some of the numbers in the grid, your goal is to determine the remaining numbers such that the numbers 1 through 9 appear exactly once in (1) each of nine 3 × 3 subgrids, (2) each of the nine rows, and (3) each of the nine columns.
Input
The input test file will contain multiple cases. Each test case consists of a single line containing 81 characters, which represent the 81 squares of the Sudoku grid, given one row at a time. Each character is either a digit (from 1 to 9) or a period (used to indicate an unfilled square). You may assume that each puzzle in the input will have exactly one solution. The end-of-file is denoted by a single line containing the word “end”.
Output
For each test case, print a line representing the completed Sudoku puzzle.
Sample Input
.2738..1..1...6735.......293.5692.8...........6.1745.364.......9518...7..8..6534.
......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.
end
Sample Output
527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936
最后终于碰上了位运算优化的题目,虽然学会了发现不难,我们就来看看这个题怎么做!
首先明确一件事,&代表两个状态取交集,这样就更快的到两个状态的共有部分,详解在代码中!
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n) memset(n,0,sizeof(n))
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1e6+;
const double esp=1e-;
//-------------------------------------------------------// const int N=;
char a[N*N+N];
int R[],L[],C[][];
int one[<<N],mp[<<N];//通过lowbit,找到每一位1对应的位置
inline int lowbit(int n)
{
return n&-n;
}
inline void init()//初始化,每个状态位赋值为1
{ for(int i=; i<N; i++){
R[i]=L[i]=(<<N)-;
} for(int i=; i<; i++)
for(int j=; j<; j++)
C[i][j]=(<<N)-;
}
inline int get(int x,int y) // 找到当前位置能填的状态
{
return L[y]&R[x]&C[x/][y/];
}
bool dfs(int n)
{
if(n==)
return ;
//cout<<n<<' ';
int x,y,mini=;
for(int i=; i<N; i++) //寻找最少填数点
{
for(int j=; j<N; j++)
{
if(a[i*N+j]!='.')
continue;
int minT=one[get(i,j)];
if(minT<mini)
{
x=i;
y=j;
mini=minT;
}
}
}
//cout<<x<<' '<<y<<endl;
int T=get(x,y);
//cout<<T<<endl;
for(int i=T; i; i-=lowbit(i)) //遍历每一位1所代表的的状态,看不懂状态,打个表就行了。
{
int w=mp[lowbit(i)];
a[x*N+y]=w+'';
R[x]-=<<w;
L[y]-=<<w;
C[x/][y/]-=<<w;
if(dfs(n-))
return ;
a[x*N+y]='.';
R[x]+=<<(w);
L[y]+=<<(w);
C[x/][y/]+=<<w;
}
return ;
}
int main()
{
for(int i=; i< <<N; i++)
{
int s=;
for(int j=i; j; j-=lowbit(j))s++;
one[i]=s; //统计2的N次方内所有的数的1的个数,这样用到时就不用算了
}
for(int i=;i<N;i++) mp[<<i]=i;
while(cin>>a&&a[]!='e')
{
int k=,cnt=;
init();
for(int i=; i<N; i++) //预处理,得到还有多少数需要填,处理一下状态
{
for(int j=; j<N; j++,k++)
{
if(a[i*N+j]=='.')
{
cnt++;
continue;
};
int T=(<<(a[k]-''));
R[i]-=T;//代表这一行的状态中填了a[k]这个数了
L[j]-=T;
C[i/][j/]-=T;
}
}
if(dfs(cnt))
cout<<a<<endl;
}
}
POJ - 3074 Sudoku (搜索)剪枝+位运算优化的更多相关文章
- 模拟赛T5 : domino ——深搜+剪枝+位运算优化
这道题涉及的知识点有点多... 所以还是比较有意思的. domino 描述 迈克生日那天收到一张 N*N 的表格(1 ≤ N ≤ 2000),每个格子里有一个非 负整数(整数范围 0~1000),迈克 ...
- 数独求解问题(DFS+位运算优化)
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...
- N皇后解法以及位运算优化
N皇后解法以及位运算优化 观察棋盘,要求皇后之间不能处在同行同列同一条斜线,求使得每行都有一个皇后的放置方法共有多少种. 每尝试放置一个皇后,都可以把该位置所在的行.列标号用一个数组标记,含义表示该行 ...
- N皇后-位运算优化
N皇后问题 时间限制: 5 Sec 内存限制: 128 MB 题目描述 魔法世界历史上曾经出现过一个伟大的罗马共和时期,出于权力平衡的目的,当时的政治理论家波利比奥斯指出:“事涉每个人的权利,绝不应 ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- POJ 3074 Sudoku (DLX)
Sudoku Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- [poj]开关类问题 枚举 位运算
poj 1222 EXTENDED LIGHTS OUT 开关只有两种方案 按和不按,按两次相当于关 只用枚举第一排开关的按法即可,剩下的行为使上一排的灯全部关闭,按法可以确定,并且是唯一的. 最后 ...
- POJ 1166 The Clocks [BFS] [位运算]
1.题意:有一组3*3的只有时针的挂钟阵列,每个时钟只有0,3,6,9三种状态:对时针阵列有9种操作,每种操作只对特点的几个时钟拨一次针,即将时针顺时针波动90度,现在试求从初试状态到阵列全部指向0的 ...
- 数独:dfs+剪枝+位运算+排除冗余+优化搜索顺序(未完)
和蓝桥杯以前一个题一样,但是数据加强了,博主水平有限,没做出来,先在这里记录一下,这里正解,下面是博主的超时做法.最近准备考研,不能深入学习了. 题目描述 数独是一种传统益智游戏,你需要把一个9 × ...
随机推荐
- MySQL入门,第二部分,必备基础知识点
一.数据类型 日期和时间数据类型 date 字节 日期,格式:2014-09-18 日期和时间数据类型 time 字节 时间,格式:08:42:30 日期和时间数据类型 datetime 字节 日期时 ...
- 计算机网络协议,TCP数据报的分析
一.TCP协议的特点 TCP是面向连接的运输层协议:即应用程序在使用TCP协议通信之前,要先建立TCP连接,通信结束后必须释放已建立的TCP连接 每一条TCP连接只能有两个端点:即TCP是点对点(一对 ...
- Linux系统安装Dos系统(虚拟机里装)
结合以下两篇优秀的文章就能完成任务. 1.https://www.jb51.net/os/609411.html 2.http://blog.51cto.com/6241809/1687361 所需要 ...
- redis 练习 a的数据库数据迁移到b数据库
思路 1.从a redis中获取所有的key 2.判断key的类型 3.根据key的类型,判断使用的是set/hset类型 4.set到b redis中(写入到b redis中)
- JS 浏览器BOM-->open() 方法
1.定义和用法 open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口. 语法: window.open(URL,name,specs,replace) 参数: URL:打开指定的页面的U ...
- Julia的基本知识
知识来源 1.变量.整数和浮点数 Julia和Matllab挺像的,基本的变量,数值定义都差不多,所以就没必要记录了. 2.数学运算 3.函数
- testlink的api
testlink可以做很多你想象得到的事情,如API测试参数管理,Excel导入导出,快速模板创建测试用例,集成Jenkins. TestLink API第三方库: TestLink-API-Pyth ...
- SpringCloud(二)笔记之Eureka
Eureka包含两个组件:Eureka Server和Eureka Client Eureka Server:提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册 Eureka ...
- Java 解析 xml 常见的4中方式:DOM SAX JDOM DOM4J
Java 四种解析 XML 的特点 1.DOM 解析: 形成了树结构,有助于更好的理解.掌握,且代码容易编写. 解析过程中,树结构保存在内存中,方便修改. 2.SAX 解析: 采用事件驱动模式,对内存 ...
- SeleniumHQ
下载地址:http://www.seleniumhq.org/download/