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优化搜索)的更多相关文章

  1. POJ3074 Sudoku

    POJ3074 Sudoku 与POJ2676相比,这一题搜索时每一步都找到最好确定的点进行枚举 对于每行.每列.每个九宫格,都分别用一个9位二进制数保存还有那些数还可以填 对于每个位置,将其所在行. ...

  2. U盘便携式hexo&博客搭建&极速纯净低bug主题推荐&部署到coding&SEO优化搜索

    指南:U盘便携式hexo&博客搭建&极速纯净低bug主题推荐&部署到coding&SEO优化搜索   U盘便携式hexo随处写博客 简述:在任意一台联网的电脑上续写he ...

  3. POJ3074 Sudoku 剪枝深(神?)搜

    emm...挺秀的...挺神的? 每行,每列,每宫用一个二进制数表示选或没选的状态,刚开始设没选为1,然后更改状态的时候异或一下就好了: 这样可以通过lowbit取出每一个没有选过的数:(妙啊? 关于 ...

  4. HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)

    pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ; sol:  因为N比较小,我们可以求出素因子,然后容斥.  主要问题就是求1到P的 ...

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

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

  6. POJ3074 Sudoku —— Dancing Links 精确覆盖

    题目链接:http://poj.org/problem?id=3074 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  7. stream,做减法,优化搜索代码。

    做一个搜索,三个输入条件,求这个条件的交集.起初我的思路是按照操作的流程,一步步的来做这三个筛选. let searchResults = []; //step1 根据id搜索,得到一个子集. if ...

  8. POJ3074 Sudoku 舞蹈链 DLX

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的数独,求解. 题解 DLX + 矩阵构建  (两个传送门) 代码 #include & ...

  9. 数独:dfs+剪枝+位运算+排除冗余+优化搜索顺序(未完)

    和蓝桥杯以前一个题一样,但是数据加强了,博主水平有限,没做出来,先在这里记录一下,这里正解,下面是博主的超时做法.最近准备考研,不能深入学习了. 题目描述 数独是一种传统益智游戏,你需要把一个9 × ...

随机推荐

  1. Web Api ——创建WebAPI

    方法在Win10 + VS2017(MVC5)测试通过 1.建立 WebApi项目: 选择菜单 “文件->新建醒目->web ->ASP.NET Web 应用程序” 输入项目名称和位 ...

  2. C#默认OrderBy()函数的排序问题

    昨天在客户现场遇到一个很奇葩的问题,猜下下面代码的排序输出是什么: static void Main() { List<", "1:"}; foreach(stri ...

  3. sql随机时间

    declare @endtime datetime declare @starttime datetime set @starttime='2017-09-01' set @endtime = '20 ...

  4. 尝试Office 2003 VSTO的开发、部署

    转载:http://www.cnblogs.com/oneivan/p/4243574.html 背景:一年前,某项目需要使用到Excel进行数据录入,考虑到很多用户还是使用XP+Office 200 ...

  5. 深入浅出SharePoint2010——请假系统无代码篇之数据框架设计

    文档库SOP:上传用户操作手册等系统相关文档. 员工信息列表EmployeeInfo:用来存储员工基本信息.直属领导和假卡信息. 请假申请列表LeaveRequest:申请人Requester填写请假 ...

  6. 解决问题,链表finish

    从一个不懂链表,到反反复复改了不下50遍,提交该题页数更是突破了五页,从周三下午到周五中午的面向对象课前的20分钟,终于把这道题AC了,其实这题本来是原来C语言综合实验的一道题,但是本次在PAT上的审 ...

  7. rsync 服务器配置过程

    rsync的原理和相关算法不赘述,资料很多 1.准备两台机器并确保都已经安装rsync a机器:192.168.1.150 ,用作客户端测试 b机器:192.168.1.151用作server端 先介 ...

  8. markdown编辑器安装

    打算使用MarkDown了,打算整理自己的知识了. 多年以前,喜欢将自己看到好东西,转载在博客.或者将遇到过的问题以及解决方案,记录在博客.06毕业后为了生活折腾,Tom网上的博客无暇东顾,等稳定闲下 ...

  9. DOM节点中获取文本易混淆的属性

    DOM 节点中对于获取文本易混淆的属性,innerText, innerHTML, outerHTML, textContent, nodeValue. 一个实例: <!DOCTYPE html ...

  10. CSS(层叠样式表)基础知识

     CSS 指层叠样式表 (Cascading Style Sheets).样式定义怎样显示 HTML 元素.它通常存储在样式表中,把样式加入到 HTML 4.0 中,解决内容与表现分离的问题. 当同一 ...