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. 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...

  2. 关于staticmethod() 函数

    说实话,我就不知这个是干什么的. 菜鸟教程写的无需实例化, 自己可以调用自己. 在同一个类面我使用到了 因为一个类了, 我可能会方法间互相调用. 类中间使用.不加这个,就会报错.无法识别这个 orig ...

  3. Leaflet

    https://leafletjs.com/ https://github.com/Leaflet/Leaflet

  4. ugui代码设置ui锚点

    using UnityEngine; public enum AnchorPresets { TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCente ...

  5. Bootstrap 学习笔记 项目实战 响应式轮播图

    左右两个箭头可以随浏览器缩放进行移动 保持在图片中间 Html代码: <!DOCTYPE html> <html lang="zh-cn"> <hea ...

  6. python绘制五角星

    问题描述: python中运用turtle图形模块绘制五角星 问题分析: turtle绘制图形时,得知图形中重要点的坐标非常重要. 于是,绘制五角星问题转化成为一个数学问题,计算五个顶点坐标即可. 已 ...

  7. 003/node.js--代理服务(解决跨域问题)

    业务描述: 1.web前端发送http请求 2.web后端为https协议 如何保证web前端发送http请求到web后端(跨域问题:域名不一致即跨域), 因此用node.js写了个代理服务,转发前端 ...

  8. win

    /*-------------------------------------------------------------- HelloMsg.c -- Displays "Hello, ...

  9. 前端表格选中列合计,select-chosen,set集合,display隐藏

    业务涉及到table选中列合计,同时隐藏未选中列.为了减少后端请求数据,前端获得所有数据后筛选计算. 1.select下拉框初始化 $(function() { $('.chosen-select') ...

  10. [随笔重写] Python3 的深拷贝与浅拷贝

    1. Python3 关于深浅拷贝的官方文档 文档地址:Python3.7.2 源码地址:lib/copy.py 2. 先说结论 深拷贝与浅拷贝是对复合对象而言的 深拷贝会构造一个新的复合对象,然后递 ...