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. 07 归档模式 Active redo log丢失或损坏的恢复

    环境同上一篇 模拟处于active状态的redo log损坏 sesion 1 SYS@ orcl >/ GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEM ...

  2. Java 基础-类与面向对象

    类 Object 类(java.lang.Object)是所有 Java 类的直接或间接父类. 成员方法及变长参数 方法定义: [public | protected | private] [stat ...

  3. SoapUI学习之SOAP和REST的区别

    一.Soap和Rest的定义 SOAP(Simple Object Access Protocol 简单对象访问协议),用于在Web Service中把远程调用和返回封装成机器可读的格式化数据,事实上 ...

  4. 刷题——有重复元素的全排列(Permutations II)

    题目如上所示. 我的解决方法(参考了九章的答案!): class Solution { public: /* * @param : A list of integers * @return: A li ...

  5. Codeforces 1159D The minimal unique substring(构造)

    首先我们先观察三个串 10,1110,11101110,答案都是红色部分,我们可以下一个结论,形如 1,1101,111101,那么答案为红色部分.我们可以发现,通过我们末尾添加的1,导致之前红色部分 ...

  6. P5504 [JSOI2011]柠檬

    传送门 显然考虑 $dp$ ,发现从右往左和从左往右是一样的,所以只考虑一边就行 发现对于切的左右端点,选择的 $s0$ 一定要为左右端点的贝壳大小,不然这个端点不产生贡献还不如分开来单个贡献 所以设 ...

  7. Java OO知识总结

    接着上一集    https://www.cnblogs.com/pdev/p/11288014.html       5. OO中的一些基本概念 继承 父类的非private方法自动被子类继承 cl ...

  8. Java缓存Ehcache-Ehcache的Cache在SSM框架中的配置

    需要在Spring配置文件中配置: <!-- 配置缓存管理器工厂 --> <bean id="cacheManager" class="org.spri ...

  9. 用jquery制作的简单轮播图

    我也是进入H5前端的小菜鸟一枚,最近才进入jquery的学习,所以打算对自己的学习进行记录. 今天分享的是一个简单的轮播图,这个轮播图的特效很简单,能够进行图片的轮播以及点击相应图片,图片能够跳转到相 ...

  10. 2018-2-13-win-10-UWP-标签

    title author date CreateTime categories win 10 UWP 标签 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:2 ...