POJ3074 Sudoku(lowbit优化搜索)
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 3 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.
输入
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”.
输出
For each test case, print a line representing the completed Sudoku puzzle.
样例输入
.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
样例输出
527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936
来源
Stanford Local 2006
题解:
这里使用了lowbit来优化当前的方案,存入二进制数后可以用Lowbit搜索每一位。
所以这说不定就是除了某d开头算法外数独较快的解法了吧。
#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
using namespace std;
char ch[12][12];
int hang[12], lie[12], gong[12], cnt[1200], num[1200], tot;
int get(int x, int y) { return (x / 3) * 3 + y / 3; }
void flip(int x, int y, int z) {
hang[x] ^= (1 << z);
lie[y] ^= (1 << z);
gong[get(x, y)] ^= (1 << z);
return;
}
bool dfs(int now) {
if (!now)
return 1;
int mn = 10, x, y;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
if (ch[i][j] != '.')
continue;
int val = hang[i] & lie[j] & gong[get(i, j)];
if (!val)
return 0; //矛盾,回退
if (cnt[val] < mn) {
mn = cnt[val];
x = i, y = j;
}
}
int val = hang[x] & lie[y] & gong[get(x, y)];
for (int i = val; i; i -= lowbit(i)) {
int which = num[lowbit(i)];
ch[x][y] = '1' + which;
flip(x, y, which);
if (dfs(now - 1))
return 1;
flip(x, y, which);
ch[x][y] = '.';
}
return 0;
}
char yyh[12000];
signed main() {
for (int i = 0; i < (1 << 9); i++)
for (int j = i; j; j -= lowbit(j))
cnt[i]++; //有几个1
for (int i = 0; i < 9; i++)
num[1 << i] = i;
while (scanf("%s", yyh) && yyh[0] != 'e') {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
ch[i][j] = yyh[i * 9 + j];
for(int i=0;i<9;i++) hang[i]=lie[i]=gong[i]=(1<<9)-1;
tot=0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (ch[i][j]!='.') flip(i,j,ch[i][j]-'1');
else tot++;
dfs(tot);
for(int i=0;i<9;i++) for(int j=0;j<9;j++) cout<<ch[i][j];
puts("");
}
return 0;
}
POJ3074 Sudoku(lowbit优化搜索)的更多相关文章
- POJ3074 Sudoku
POJ3074 Sudoku 与POJ2676相比,这一题搜索时每一步都找到最好确定的点进行枚举 对于每行.每列.每个九宫格,都分别用一个9位二进制数保存还有那些数还可以填 对于每个位置,将其所在行. ...
- U盘便携式hexo&博客搭建&极速纯净低bug主题推荐&部署到coding&SEO优化搜索
指南:U盘便携式hexo&博客搭建&极速纯净低bug主题推荐&部署到coding&SEO优化搜索 U盘便携式hexo随处写博客 简述:在任意一台联网的电脑上续写he ...
- POJ3074 Sudoku 剪枝深(神?)搜
emm...挺秀的...挺神的? 每行,每列,每宫用一个二进制数表示选或没选的状态,刚开始设没选为1,然后更改状态的时候异或一下就好了: 这样可以通过lowbit取出每一个没有选过的数:(妙啊? 关于 ...
- HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)
pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ; sol: 因为N比较小,我们可以求出素因子,然后容斥. 主要问题就是求1到P的 ...
- HDU - 5547 Sudoku(数独搜索)
Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself ...
- POJ3074 Sudoku —— Dancing Links 精确覆盖
题目链接:http://poj.org/problem?id=3074 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- stream,做减法,优化搜索代码。
做一个搜索,三个输入条件,求这个条件的交集.起初我的思路是按照操作的流程,一步步的来做这三个筛选. let searchResults = []; //step1 根据id搜索,得到一个子集. if ...
- POJ3074 Sudoku 舞蹈链 DLX
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的数独,求解. 题解 DLX + 矩阵构建 (两个传送门) 代码 #include & ...
- 数独:dfs+剪枝+位运算+排除冗余+优化搜索顺序(未完)
和蓝桥杯以前一个题一样,但是数据加强了,博主水平有限,没做出来,先在这里记录一下,这里正解,下面是博主的超时做法.最近准备考研,不能深入学习了. 题目描述 数独是一种传统益智游戏,你需要把一个9 × ...
随机推荐
- 推荐js库: underscore
Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率. 之间没用他之前,自己写,那是相当的酸爽. 如循环处理: for(var i=0;i<data.length; ...
- Attribute+Reflection,提高代码重用
这篇文章两个目的,一是开阔设计的思路,二是实例代码可以拿来就用. 设计的思路来源于<Effective c#>第一版Item 24: 优先使用声明式编程而不是命令式编程.特别的地方是,希望 ...
- N点虚拟主机管理系统如何使用?
有朋友问起N点虚拟主机管理系统怎么用呢?下面大概整理下他的使用方法,咱们来看看吧. 在讲如何使用N点虚拟主机管理系统之前,我们先来了解一下N点虚拟主机管理系统的介绍. N ...
- 一分钟在云端快速创建MySQL数据库实例
本教程将帮助您了解如何使用Azure管理门户迅速创建,连接,配置MySQL 数据库 on Azure.完成本教程后,您将在Azure上拥有一个示例MySQL数据库服务器,并了解如何使用管理门户执行基本 ...
- cocos ide使用binding-generator导出来的c++类
time:2015/03/19 cocos版本:3.2 描述:用了ide运行一个实例[1]的时候需要增加c++类,正确导出来之后,直接使用vs2012启动是没有问题的,但是使用ide启动却提示找不到模 ...
- Mysql进阶-day3
多实例介绍: mysql多实例就是一台服务器开启多个不同的服务端口(3306,3307),运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供各自的服务端口. 这些m ...
- [BZOJ 2186][SDOI 2008] 莎拉公主的困惑
2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 4519 Solved: 1560[Submit][S ...
- BZOJ 1015 星球大战starwar 逆向并查集
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1015 题目大意: 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个 ...
- Tableau10.4中智能显示点击后消失的解决方案
如果你的电脑是Win10,并且是高分屏,可能会出现和我一样的问题,就点击智能显示后,发现找不到了. 那么解决方案就是: 这样就能找到智能显示了.
- 问题:android学习内容破碎,我个人关于如何学习android的一些个人经历
android学习两个月心得 我于大三下学期,开始准备学习android,在寒假期间,学了毕向东的java视频的前10天,觉得还不错,上网找评论,他们都说,只要学到多线程就可以学习android了, ...