——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. 14. Popen类
  2. 【PP操作手册】工艺路线相关操作
  3. linux idea 启动报错StartupAbortedException: Fatal error initializing plugin idea.plugin.protoeditor
  4. visual studio中导入opencv的项目
  5. Android实现仿美图秀秀给图片加框
  6. autoupdater 更新后重新运行_Windows 10 总是自动更新?这几个方法教你关闭它
  7. nodeJS中的异步编程
  8. Ubuntu中Python3找不到_sqlite3模块
  9. (19)Verilog HDL顺序块:begin-end
  10. coreboot学习10:coreboot第一阶段学习小结
  11. Python 字符串(二)
  12. shell export path_Shell的变量声明
  13. c语言如何在坐标输出,tc 如何在指定坐标处 输出bmp图片??
  14. 阿里的easyexcel
  15. 二维数组和稀疏数组转化
  16. Webmin远程命令执行漏洞(CVE-2019-15107 )复现
  17. Deep Homography Estimation - Pytorch实现
  18. MSSQL·手动安装机器学习相关CAB文件
  19. RK3399 hi3559A 平台离线语音识别、合成、翻译、声纹
  20. 32位颜色 转换成16位颜色

热门文章

  1. 二、三级等保建议安全设备及其主要依据(毫无保留版)
  2. mysql repair 所有表_MySQL磁盘满repair多个表问题的解决
  3. WaitForSingleObject与事件、信号量、互斥、临界区的用法
  4. docker里mysql精简版_新手把mysql装进docker中碰到的各种问题
  5. 不要和负能量的人交朋友
  6. 尚学堂Java培训:如何读书?
  7. OCI env setting
  8. RT-Thread——STM32——FAL库
  9. 人工智能案例集 | 连续值预测基础
  10. 西门子S7-1200小练手之电机星三角启动