5 October
位运算 + 搜索。更好的优化方法:方案数最小的空格先填。
把某一位 置为 0:a &=~ (1<<n)
把某一位 置为 1:a |= (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的更多相关文章
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- [luogu4860][Roy&October之取石子II]
题目链接 思路 这个题和上个题类似,仔细推一下就知道这个题是判断是否是4的倍数 代码 #include<cstdio> #include<iostream> #define f ...
- [luogu4018][Roy&October之取石子]
题目链接 思路 这个题思路挺巧妙的. 情况一: 首先如果这堆石子的数量是1~5,那么肯定是先手赢.因为先手可以直接拿走这些石子.如果石子数量恰好是6,那么肯定是后手赢.因为先手无论怎样拿也无法直接拿走 ...
- Codechef October Challenge 2018 游记
Codechef October Challenge 2018 游记 CHSERVE - Chef and Serves 题目大意: 乒乓球比赛中,双方每累计得两分就会交换一次发球权. 不过,大厨和小 ...
- Laravel项目October安装
October是一个免费,开源,自托管的基于laravel PHP框架CMS平台.在github平台上laravel应用排名第二,可以拿来研究一下.官方介绍:October是一个内容管理系统(CMS) ...
随机推荐
- 【FICO系列】SAP 关于SAP中的记账码的解释
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP 关于SAP中的记账码的解 ...
- 【MM系列】SAP MM中的委外加工与信息记录
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM中的委外加工与信息记录 ...
- C++笔记(6)——关于OJ的单点测试和多点测试
单点测试 PAT使用的就是单点测试(LeetCode应该也是单点测试).单点测试中系统会判断每组数据的输出结果是否正确,正确则通过测试并获得这则测试的分值.题目的总得分等于通过的数据的分值之和. 代码 ...
- github javascript相关项目star数排行榜(前30,截止2016.11.18):
github javascript相关项目star数排行榜(前30,截止2016.11.18): 前端开源框架 TOP 100 前端 TOP 100:::::https://www.awesomes. ...
- 两台电脑使用ROS通讯
一.ROS分布式多机通讯简介 ROS是一种分布式软件框架,节点之间通过松耦合的方式组合,在很多应用场景下,节点可以运行在不同的计算平台上,通过Topic,Service通信. 但是各个节点只能共同拥有 ...
- 实验报告2&&第四周课程总结
实验报告: 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类 ...
- AcWing 831. KMP字符串(模板)
给定一个模式串S,以及一个模板串P,所有字符串中只包含大小写英文字母以及阿拉伯数字. 模板串P在模式串S中多次作为子串出现. 求出模板串P在模式串S中所有出现的位置的起始下标. 输入格式 第一行输入整 ...
- 山区建小学(区间dp+前缀和+预处理)
[题目描述] 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为di(为正整数),其中,0 < i ...
- C# 中常见的控件以及功能
1.StatusBar控件——显示各种状态信息. StatusBar控件可以有状态栏面板(用于显示图标以指示状态)或一系列动画图标(用于指示某个进程正在工作,例如,表示正在保存文档的 Microsof ...
- win32 socket编程(二)——TCP/IP
一.大端.小端法定义 1.1小端法(Little-Endian)就是低位字节排放在内存的低地址端即该值的起始地址,高位字节排放在内存的高地址端. (主机字节顺序) 1.2 大端法(Big-Endian ...