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. 类ThreadGroup

    Java中使用ThreadGroup来表示线程组,它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制. 默认的情况下,所有的线程都属于主线程组. public final Thread ...

  2. 16/7/8_PHP-字符串介绍

    不知道为什么慕课网还这个都要介绍.不过还是一个新的知识点. PHP开发中,我们遇到最多的可能就是字符串. 字符串变量用于包含字符串的值. 一个字符串 通过下面的3种方法来定义: 1.单引号2.双引号3 ...

  3. 【ABAP系列】SAP ABAP ALV中的TOP_OF_PAGE添加任意图标

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP ALV中的TOP_ ...

  4. 关于存储过程的一些sql

    1.关于事务的回滚Set XACT_ABORT ON; 开启为on时 ,如果事务语句产生运行错误 ,将整个事务终止进行回滚,Off时只回滚产生错误的语句. 2.获取事务语句中上一次插入值的行号@@ID ...

  5. 第四周课程总结&试验报告

    实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性 ...

  6. vue 上传文件 并以表格形式显示在页面上

    先上代码 <label for="file" class="btn">多文件上传</label> <input type=&quo ...

  7. webpack基本介绍及使用

    1.什么是webpack webpack是一个前端资源加载/打包工具.它根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. 官网地址:https://www.webpac ...

  8. wxpython中复选框的基本使用源码实例

    #coding=utf-8 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1, ...

  9. c# 实现ComboBox自动模糊匹配

    ComboBox自带有属性可以实现自动匹配,但是它有一个弊端,只能从头开始匹配,例如"李四LS",只能输入“李四”或"李"才能匹配出来,而输入"LS& ...

  10. 正则表达式RegExp对象

    3.1 正则表达式对象的创建方式 字面量的方式   var patt = /匹配规则/修饰符;     / --> 边界的意思 new关键字     var patt = new RegExp( ...