POJ2676 Sudoku

位运算 + 搜索。更好的优化方法:方案数最小的空格先填。

把某一位 置为 0a &=~ (1<<n)

把某一位 置为 1a |= (1<<n)

#include <cstdio>
#include <cstring>
int T, f[9], g[9], h[3][3];
char buf[10];
int v[9][9], tot; inline int lowbit(int x) { return x&(-x); }
inline int num(int x) {
register int cnt=0; while (x) ++cnt, x>>=1; return cnt-1;
} bool search(int x, int y) {
int res=f[x]&g[y]&h[x/3][y/3];
if (!res) return 0;
while (res) {
int t=lowbit(res); res&=~t; --tot;
f[x]&=~t, g[y]&=~t, h[x/3][y/3]&=~t; v[x][y]=num(t);
if (!tot) return 1;
for (int i=0, bk=0; i<9; ++i) {
for (int j=0; j<9; ++j)
if (!v[i][j]) {if (search(i, j)) return 1; bk=1; break; }
if (bk) break;
}
f[x]|=t, g[y]|=t, h[x/3][y/3]|=t; v[x][y]=0; ++tot;
}
return 0;
} int main() {
scanf("%d", &T);
while (T--) {
for (int i=0; i<9; ++i) for (int j=0; j<9; ++j)
f[i]=g[i]=(1<<10)-2;
for (int i=0; i<3; ++i) for (int j=0; j<3; ++j)
h[i][j]=(1<<10)-2;
memset(v, 0, sizeof v); tot=0;
for (int i=0; i<9; ++i) {
scanf("%s", buf);
for (int j=0; j<9; ++j) if (buf[j]-='0') {
v[i][j]=buf[j];
f[i]&=~(1<<buf[j]), g[j]&=~(1<<buf[j]),
h[i/3][j/3]&=~(1<<buf[j]);
} else ++tot;
}
for (int i=0, bk=0; i<9; ++i) {
for (int j=0; j<9; ++j)
if (!v[i][j]) {search(i, j); bk=1; break; }
if (bk) break;
}
for (int i=0; i<9; ++i) {
for (int j=0; j<9; ++j) printf("%d", v[i][j]);
printf("\n");
}
}
return 0;
}

5 October的更多相关文章

  1. October 14th 2016 Week 42nd Friday

    Who am I? Coming October 18, 2016! 我是谁?2016.10.18 拭目以待! Don't worry. You will be a wow. Don't worry. ...

  2. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?

    I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...

  3. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  4. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  5. [Under the hood]---Matt Pietrek October 1996 MSJ

    Matt Pietrek October 1996 MSJ Matt Pietrek is the author of Windows 95 System Programming Secrets (I ...

  6. October 23, 2013 - Fires and smoke in eastern China

    October 23, 2013 - Fires and smoke in eastern China Satellite: Aqua Date Acquired: 10/12/2013 Resolu ...

  7. [luogu4860][Roy&October之取石子II]

    题目链接 思路 这个题和上个题类似,仔细推一下就知道这个题是判断是否是4的倍数 代码 #include<cstdio> #include<iostream> #define f ...

  8. [luogu4018][Roy&October之取石子]

    题目链接 思路 这个题思路挺巧妙的. 情况一: 首先如果这堆石子的数量是1~5,那么肯定是先手赢.因为先手可以直接拿走这些石子.如果石子数量恰好是6,那么肯定是后手赢.因为先手无论怎样拿也无法直接拿走 ...

  9. Codechef October Challenge 2018 游记

    Codechef October Challenge 2018 游记 CHSERVE - Chef and Serves 题目大意: 乒乓球比赛中,双方每累计得两分就会交换一次发球权. 不过,大厨和小 ...

  10. Laravel项目October安装

    October是一个免费,开源,自托管的基于laravel PHP框架CMS平台.在github平台上laravel应用排名第二,可以拿来研究一下.官方介绍:October是一个内容管理系统(CMS) ...

随机推荐

  1. Redis数据类型:Hashes、Geo操作指令

    Redis数据类型:Hashes.Geo操作指令 Hashes常用操作指令 Redis Hashes是一个键值对的映射表,最对能存储2^32-1(约40亿)个键值对. HSET HGET HSET:将 ...

  2. 解决浏览器打开网页后提示“dns_probe_possible”的方法

    使用浏览器浏览网页时偶尔会遇到无法上网且浏览器提示:DNS_PROBE_POSSIBLE 一般有三种情况会导致这样的故障: 1.网络协议出现故障,也就是常说的 DNS 设置问题 2.浏览器中设置问题, ...

  3. 如何使用IDEA将项目上传到GitHub中

    上传之前先规定上传的格式: 1 . 以后所有上传的项目,都只上传 src文件集以及pom.xml文件,不要带有自己的 .idea配置文件或者target运行文件之类的(就算是测试文件也一样,从开始就养 ...

  4. 循环结构 :while

    循环结构 :while 循环四要素: 1.初始化条件 2.循环条件 3.循环体 4.迭代条件 格式: 1.初始化条件 while(2.循环条件){ 3.循环体 4.迭代条件 } public clas ...

  5. 【接口工具】mac环境下使用Charles抓包Https请求

    Charles支持针对Https包解析.具体安装导航请参考“[接口工具]接口抓包工具之Charles” 操作流程 电脑安装证书: Help-SSL Proxying-Install Charles R ...

  6. asp.net 关于SessionId

    原文:https://www.cnblogs.com/zhang1999/p/7278020.html 登陆页面使用Session存储验证码,导致会话产生SessionId,从而导致会话固定,登陆后用 ...

  7. 图片,word,Excel等附件上传

    @ResponseBody @RequestMapping("/upload") public String upload(HttpServletRequest request, ...

  8. Window Operations

    Window Operations 有点类似于Storm中的State,可以设置窗口的大小和滑动窗口的间隔来动态的获取当前Steaming的允许状态,可以对一段时间的数据进行处理. 如图window ...

  9. Dubbo源码学习总结系列七---注册中心

    Dubbo注册中心是框架的核心模块,提供了服务注册发现(包括服务提供者.消费者.路由策略.覆盖规则)的功能,该功能集中体现了服务治理的特性.该模块结合Cluster模块实现了集群服务.Dubbo管理控 ...

  10. Windows下的Linux子系统安装,WSL 2下配置docker

    Windows下的Linux子系统安装,WSL 2下配置docker 前提条件: 安装WSL 2需要Windows 10版本是Build 18917或更高,首先先确认系统版本已升级. 在“启用或关闭W ...