——by A Code Rabbit

Description

有p个LED灯,可以组成一个灯牌。

灯牌上可以显示一些有意义的符号,比如显示数字啥的。

现在有n个灯牌,显示的符号各不相同。

问你最少用几个LED灯,就可以区别这些符号。

输入n和p。

输出最少LED灯数。

Types

Brute Force :: Elementary Skills

Analysis

从题目的描述和输入数据的方式,我们都可以得到暗示——

这题应该用二进制数来做。

正如输入那样,我们用二进制数表示一个符号(或者说表示一个灯牌的显示情况)。

二进制数的每个数位对应着一个位置的LED灯,0表示灭,1表示亮。

例如,数字8在灯牌上的显示,

对应二进制数——

1111111。

而下面这个灯牌的显示,

可以表示成二进制数——

1111110。

而现在重点来了。

我们使用了1~5号LED灯,不使用6号LED灯,这种LED的使用情况,也可以用二进制数表示。

同样,1表示使用,0表示不使用——

1111110。

这时候我们发现,如果第一个灯牌是我们要区分的符号,而第二个灯牌是实际显示的情况,同时我们有LED灯的显示情况,那么有式子——

符号原始显示 & LED使用情况 = 灯牌实际显示

即      1111111    &     1111110      =     1111110

我们也可以举题目中的例子,数字5不使用最下面的LED灯——

1101011   &      1111110       =       1101011

得到 1101011,就是下面这个鸟样。

将所有情况转化为二进制后,我们就可以很方便得利用二进制去搜索——

枚举各种LED灯的使用情况(使用LED灯的数量从少到多),去并每一个符号,如果并完后的每个二进制数互异,那么就可以在只使用部分LED灯的情况下区分各个符号,即符合题目的条件。

顺便说一下,如何去枚举。

你可以规规矩矩地去BFS,也可以直接枚举每一种情况去暴力(反正这个专题就是暴力专题)。

由于题目数据规模可能没那么大,暴力也是可以AC的。

当然如果你觉得不踏实,也可以优化一下。

枚举到的LED灯使用情况,如果LED灯的使用多与或者等于已知的当前最优方案,就不必考虑。

Solution

1. BFS

// UVaOJ 11205
// The broken pedometer
// by A Code Rabbit#include <cstdio>
#include <cstring>const int LIMITS_P = 17;
const int LIMITS_N = 102;const int DIGIT[] = {1 <<  0, 1 <<  1, 1 <<  2, 1 <<  3,1 <<  4, 1 <<  5, 1 <<  6, 1 <<  7,1 <<  8, 1 <<  9, 1 << 10, 1 << 11,1 << 12, 1 << 13, 1 << 14, 1 << 15,
};int t;
int n;
int p;int symbol[LIMITS_N];struct LED {int codification;int num;
};LED queue[1 << LIMITS_P];
int head, tail;
bool visit[1 << LIMITS_P];
bool is_found;bool CanIdentify(int codification);void BFS();
void Init();
void Search(int codification, int num);int main() {scanf("%d", &t);while (t--) {scanf("%d", &p);scanf("%d", &n);for (int i = 0; i < n; ++i) {symbol[i] = 0;for (int j = 0; j < p; ++j) {int digit;scanf("%d", &digit);symbol[i] += DIGIT[p - j - 1] * digit;}}BFS();}return 0;
}bool CanIdentify(int codification) {for (int i = 0; i < n; ++i) {for (int j = i + 1; j < n; ++j) {if ((symbol[i] & codification) ==(symbol[j] & codification)){return false;    }}}return true;
}void BFS() {Init();Search(0, 0);while (head < tail) {int codification_now = queue[head].codification;int num_now = queue[head].num;for (int i = 0; i < p; ++i) {Search(codification_now | DIGIT[i], num_now + 1);}++head;}
}void Init() {memset(visit, false, sizeof(visit));head = 0;tail = 0;is_found = false;
}void Search(int codification, int num) {// Exit.if (is_found) {return ;}if (visit[codification]) {return;}// Judge.if (CanIdentify(codification)) {printf("%d\n", num);is_found = true;return;}// Continue.queue[tail].codification = codification;queue[tail].num = num;++tail;visit[codification] = true;
}

2. Brute Force

// UVaOJ 11205
// The broken pedometer
// by A Code Rabbit#include <cstdio>
#include <cstring>const int LIMITS_P = 17;
const int LIMITS_N = 102;const int DIGIT[] = {1 <<  0, 1 <<  1, 1 <<  2, 1 <<  3,1 <<  4, 1 <<  5, 1 <<  6, 1 <<  7,1 <<  8, 1 <<  9, 1 << 10, 1 << 11,1 << 12, 1 << 13, 1 << 14, 1 << 15,
};int t;
int n;
int p;int symbol[LIMITS_N];bool visit[LIMITS_P];int CountLedNum(int codification);
bool CanIdentify(int codification);int main() {scanf("%d", &t);while (t--) {scanf("%d", &p);scanf("%d", &n);for (int i = 0; i < n; ++i) {symbol[i] = 0;for (int j = 0; j < p; ++j) {int digit;scanf("%d", &digit);symbol[i] += DIGIT[p - j - 1] * digit;}}memset(visit, false, sizeof(visit));int min = p + 1;for (int i = 0; i < 1 << p; ++i) {int num_led = CountLedNum(i);if (num_led < min && !visit[num_led]) {if (CanIdentify(i)) {min = num_led;visit[num_led] = true;}}}printf("%d\n", min);}return 0;
}int CountLedNum(int codification) {int num_result = 0;for (int i = 0; i < p; ++i) {if (codification & DIGIT[i]) {++num_result;}}return num_result;
}bool CanIdentify(int codification) {for (int i = 0; i < n; ++i) {for (int j = i + 1; j < n; ++j) {if ((symbol[i] & codification) ==(symbol[j] & codification)){return false;    }}}return true;
}

下载PDF

参考资料:沐阳博客

UVaOJ 11205 - The broken pedometer相关推荐

  1. UVA 11205 - The broken pedometer

    题目大意:题中给了一种例子,LED的七根灯管不同的开关方式显示0到9的数字.其中可以去掉第六根灯管(可能去掉其他也可以,没细究)也就是只要六根灯管就可以区分所有的数字.简单理解可以认为是7位的一个二进 ...

  2. uva 11205 The broken pedometer

    刚开始一列一列考虑,最后再减去能去掉几列,后来才发现是错误的.单独考虑某一列,同时去掉几列时,单独考虑的不一定成立.后来一查是算法竞赛入门经典里面的p188页的位向量法.原来一直以为这边书空有理论,现 ...

  3. 11205 - The broken pedometer

    语言:C++ 描述:这道题其实一开始自己就是错误思维,题目就是通过最少的二进制位表示所有的数,也就是说存在自由组合的问题,不过存在这样的问题:如果一列二进制数被删去后,可以使列数减一来表示了所有的数了 ...

  4. UVa11205 The Broken Pedometer

    // 题意:有P个LED灯,以及N个字符,要求选出个数最少的LED灯,使得即使只有这些灯正常工作,也能区分出这N个字符 // 题意抽象:输入两个整数P, N以及N行P列的01矩阵,找少的列,能区分所有 ...

  5. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

  6. 第六周 8.23-8.29

    8.23 POJ 3311 Hie with the Pie TSP问题. 先跑一遍Floyd.再状压dp. dp[i][j]表示经过集合i的点最后到达j的最短距离. 转移:取集合i中任意一点j.如果 ...

  7. TYUT-A专题题解(一)

    TYUT-A专题题解(一) 01A Ad Hoc UVA353 LA5247 Pesky Palindromes[回文] - 海岛Blog - CSDN博客 UVA947 Master Mind He ...

  8. Competitive Programming 3题解

    题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...

  9. AOAPC I: Beginning Algorithm Contests 题解

    AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...

最新文章

  1. 43.6% mAP! 阿里巴巴提出:用于一阶段目标检测的半锚式检测器
  2. 10分钟内把永远跑不完的存储过程变为2秒跑完
  3. 1、MySQL数据类型简介
  4. 搭载鸿蒙处理器的手机,荣耀Magic3被曝光,或采用安卓与鸿蒙双系统,搭载麒麟9000处理器...
  5. mybatis、mybatisplus的常用操作
  6. object-c 中括号[]
  7. Python获取局域网内所有机器IP地址与网卡MAC地址
  8. zabbix监控memcached
  9. runtime 的 assemblyIdentity 元素和bindingRedirect 元素
  10. mysql:Error while performing database loggin with the mysql driver
  11. 本地计算机如何使用代理服务器,自动设置代理ip
  12. 阿里云服务器怎么开发票?
  13. Android封装sdk页面为h5,Android/H5混合 SDK 集成文档
  14. 热乎乎的宇宙头条校招前端面经
  15. oracle报错Error while registering Oracle JDBC Diagno
  16. PHP 版 微信小程序商城 源码和搭建
  17. 三维点云拼接 标记点拼接 SVD分解法
  18. AI人工智能学习之激活函数
  19. Enhancer是啥
  20. 中国境外三个不为人知的汉人政权

热门文章

  1. IITP:截止2017年韩国人工智能专利排名全球第三
  2. Joshua Bloch离开Google了,Dart前景堪忧
  3. 微信小程序——沉浸式导航栏实现(含iphoneX适配和组件封装)
  4. 乔列斯基(Cholesky)法解方程(python,数值积分)
  5. 用word制作电子公章
  6. android刷机恢复出厂设置吗,安卓手机恢复出厂设置和双清有什么区别?
  7. 【原来python还可以这么玩】python逆向爬取网易云评论进行情感分析
  8. cad2018致命错误unhandled_CAD打不开出现致命错误的四种解决办法
  9. jQuery 实现带下拉提示且自动填充的邮箱
  10. Coursera 国内无法登陆问题