题意:给定一个9*9的数独,要求判断是否为简单数独。
数独:对于每一行每一列或者子方格内,只能填1~9这几个数,并且每个数字只能出现一次,比如说:

如果一个9*9的数独是简单数独的话,这个数独的解是独一无二的
也就是说,每次可以推导出一个点,这个点的值是确定的,直到数独被填满。
对于下图,x只能填2

对于下图,x只能填1

判断数独是否是简单数独。如果是输出1,不是输出0.

这道题坑爹,质疑标程写错,数据出错。
其实仔细思考一番可以发现使用位运算来做,因为只有9个数,那么我们可以使用9位二进制来代表该数是否已经使用,若已经使用则为1,否则为0
那么我们可以很容易得到一个01的状态,它其实是个整数。
显然,可以容易每个点的可以放置的状态,第i位为1说明该点可以放置i,否则不能放置,若该状态只能放置1个点,也就是二进制中1的个数为1,那么就必然放置该点。
(这题训练赛的时候没有A,还被队友嘲讽了一发,队友敲了20分钟就过了,我勒个擦!)
这个代码其实早就出来了,因为我两种情况都考虑到了,诡异的一直wa
代码1:

//author: CHC
//First Edit Time:  2015-09-24 21:03
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int bs[10][10],h[10],l[10],tmp[10][10],xs[4][4];
int check(){for(int i=1;i<=9;i++)for(int j=1;j<=9;j++)if(!bs[i][j])return 0;return 1;
}
int count1(int x){int cnt=0;while(x>0){x-=x&-x;++cnt;}return cnt;
}
void print(int x){for(int i=0;i<9;i++)if((x>>i)&1)printf("%d ",i+1);puts("");
}
const int X = (1<<9)-1;
int main()
{int T;scanf("%d",&T);while(T--){for(int i=1,x;i<=9;i++){for(int j=1;j<=9;j++){scanf("%d",&x);if(x)bs[i][j]=(1<<(x-1));else bs[i][j]=0;}}int flag=0;while(1){if(check())break;for(int i=1;i<=9;i++)h[i]=X,l[i]=X;for(int i=1;i<=3;i++)for(int j=1;j<=3;j++)xs[i][j]=X;for(int i=1;i<=9;i++)for(int j=1;j<=9;j++){h[i]&=~bs[i][j];l[j]&=~bs[i][j];xs[(i-1)/3+1][(j-1)/3+1]&=~bs[i][j];}for(int i=1;i<=9;i++)for(int j=1;j<=9;j++){if(bs[i][j])tmp[i][j]=X&bs[i][j];else tmp[i][j]=X&h[i]&l[j]&xs[(i-1)/3+1][(j-1)/3+1];}int rcnt=0;for(int i=1;i<=9&&!rcnt;i++)for(int j=1;j<=9&&!rcnt;j++){if(bs[i][j])continue;int tx=tmp[i][j];int ct=count1(tx);if(ct==1){bs[i][j]=tx;++rcnt;break;}for(int ii=(i-1)/3*3+1;ii<=((i-1)/3+1)*3;ii++){for(int jj=(j-1)/3*3+1;jj<=((j-1)/3+1)*3;jj++){if(ii!=i&&jj!=j)tx&=~tmp[ii][jj];}}int tt=count1(tx);if(tt==1){bs[i][j]=tx;++rcnt;break;}}if(rcnt==0){flag=1;break;}}printf("%d",!flag);}puts("");return 0;
}

这个代码我无意中删掉部分,就过了,发现其中只考虑了第一种情况。
代码2:

//author: CHC
//First Edit Time:  2015-09-24 21:03
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int bs[10][10],h[10],l[10],tmp[10][10],xs[4][4];
int check(){for(int i=1;i<=9;i++)for(int j=1;j<=9;j++)if(!bs[i][j])return 0;return 1;
}
int count1(int x){int cnt=0;while(x>0){x-=x&-x;++cnt;}return cnt;
}
void print(int x){for(int i=0;i<9;i++)if((x>>i)&1)printf("%d ",i+1);puts("");
}
const int X = (1<<9)-1;
int main()
{int T;scanf("%d",&T);while(T--){for(int i=1,x;i<=9;i++){for(int j=1;j<=9;j++){scanf("%d",&x);if(x)bs[i][j]=(1<<(x-1));else bs[i][j]=0;}}int flag=0;while(1){if(check())break;for(int i=1;i<=9;i++)h[i]=X,l[i]=X;for(int i=1;i<=3;i++)for(int j=1;j<=3;j++)xs[i][j]=X;for(int i=1;i<=9;i++)for(int j=1;j<=9;j++){h[i]&=~bs[i][j];l[j]&=~bs[i][j];xs[(i-1)/3+1][(j-1)/3+1]&=~bs[i][j];}for(int i=1;i<=9;i++)for(int j=1;j<=9;j++){if(bs[i][j])tmp[i][j]=X&bs[i][j];else tmp[i][j]=X&h[i]&l[j]&xs[(i-1)/3+1][(j-1)/3+1];}int rcnt=0;for(int i=1;i<=9&&!rcnt;i++)for(int j=1;j<=9&&!rcnt;j++){if(bs[i][j])continue;int tx=tmp[i][j];int ct=count1(tx);if(ct==1){bs[i][j]=tx;++rcnt;break;}}if(rcnt==0){flag=1;break;}}printf("%d",!flag);}puts("");return 0;
}
   数据(需要能正确的推导出哪些点是一定的):0 8 0 3 0 0 0 0 90 0 0 0 8 0 0 0 33 0 0 5 0 0 0 8 21 9 8 4 5 7 3 2 65 2 3 9 6 1 8 4 74 7 6 2 3 8 1 9 59 3 4 8 2 5 7 6 16 5 2 0 0 4 9 3 88 1 7 6 9 3 2 5 47 8 0 3 0 0 0 0 90 0 0 0 8 0 0 0 33 0 0 5 0 0 0 8 21 9 8 4 5 7 3 2 65 2 3 9 6 1 8 4 74 7 6 2 3 8 1 9 59 3 4 8 2 5 7 6 16 5 2 0 0 4 9 3 88 1 7 6 9 3 2 5 41 6 0 0 0 0 7 0 00 0 0 0 0 1 0 0 00 0 0 4 0 9 0 0 00 0 0 0 3 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 5 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 8 0 0 0 00 0 0 0 0 0 0 0 01 0 0 0 0 0 7 0 00 0 0 0 0 1 0 0 00 0 0 0 0 0 6 0 00 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 01 2 3 0 0 0 0 0 04 5 6 0 0 0 0 0 07 8 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0

UVALive 3351 Easy and Not Easy Sudoku Puzzles 位运算~判断简单数独相关推荐

  1. c语言easy,C语言easy….doc

    Problem 1 表格问题 #include void output(const int* a) { printf("%d %d %d\n",a[0], a[1], a[2]); ...

  2. POJ-2676 Sudoku(简单数独-dfs深搜)

    Sudoku Time Limit: 2000MS Memory Limit: 65536K 题目链接http://poj.org/problem?id=2676 Description Sudoku ...

  3. (每日一练c++)CC114 有效的数独

    描述 根据数独的规则Sudoku Puzzles - The Rules.判断给出的局面是不是一个符合规则的数独局面 数独盘面可以被部分填写,空的位置用字符'.'.表示 这是一个部分填写的符合规则的数 ...

  4. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  5. i2p源码笔记-KBucketSet.java

    文章目录 说明 sourcecode 详细笔记 成员变量 方法 构造函数 其他函数 获取lock相关函数 添加节点 其他函数 不知道怎么命名的另外一坨参数 range类和dummybucket类 ra ...

  6. 算法实践:数独的基本解法

    数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复. 每一道合格的数独谜 ...

  7. 算法实践——数独的基本解法

    数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复. 每一道合格的数独谜 ...

  8. LeetCode刷题记录1——717. 1-bit and 2-bit Characters(easy)

    LeetCode刷题记录1--717. 1-bit and 2-bit Characters(easy) LeetCode刷题记录1--717. 1-bit and 2-bit Characters( ...

  9. PacketTracer 5.2基于AAA的Easy ×××实验

    在博客中的<Cisco PacketTracer 5.2模拟器的Easy ×××实验指南>,所才用接入用户认证是路由器上的用户名密码的本地认证.今天的实验我使用PacketTracer 5 ...

最新文章

  1. MySQL的log_bin和sql_log_bin 到底有什么区别?
  2. 社会工程学到底有多可怕
  3. MariaDB数据库介绍三、MHA(Master HA)实现主节点故障转移
  4. hdu4861 找规律了
  5. 24个笔画顺序表_400个生字笔顺表,孩子照着写,家长省心省力!
  6. Java之开发工具(1) - Eclipse 如何设置注释的模板
  7. Ubuntu安装特定版本安装包
  8. AOP 你想干什么 IOC 你服务什么
  9. ASP.NET----GridView控件设置超链接
  10. C语言 qq自动点赞程序,qq自动无限点赞脚本
  11. 数据通信技术初级工程师证题库
  12. 菜鸟CTO谈物流科技:科学家也要贴地飞行
  13. js动画 无缝轮播 进度条 文字页面展示 div弹窗遮罩效果
  14. 华为发布八核处理器Kirin 920
  15. 软件开发测试男友花束,观察这四束花束凭直觉选一束花?测一测别人都在羡慕你的什么?...
  16. shell脚本case传递参数
  17. AD账号被频繁锁定的解决方案
  18. 4线电子围栏安装示意图_电子围栏系统安装教程(图文)
  19. 五大方案,教你关于微信公众号的内容定位
  20. Tushare使用分享(二)

热门文章

  1. 让小程序搭上百度AI的快车
  2. BindingException: Invalid bound statement (not found)
  3. project2010如何插入子任务?
  4. 【180930】C#纸牌小游戏源码
  5. [诈骗]“中国移动”发送诈骗短信,china mobile 是骗子吗?
  6. 简单的VGA字符模式驱动(一)
  7. 2012第三届国信蓝点杯全国软件设计大赛编程大题
  8. Maven系列第5篇:私服详解
  9. 红雪iOS6.1.3不完美越狱教程
  10. 计算机专业就业正规军干不过游击队?