UVaOJ 11205 - The broken pedometer
来源:互联网 发布:淘宝楼道鞋架 编辑:程序博客网 时间:2024/05/16 11:32
——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
- UvaOJ 11205 The broken pedometer
- 11205--The broken pedometer
- 11205 - The broken pedometer
- 11205 - The broken pedometer
- 11205 - The broken pedometer
- UVa 11205 The broken pedometer
- uva 11205 - The broken pedometer
- uva 11205 - The broken pedometer
- UVa 11205 - The broken pedometer
- uva 11205 - The broken pedometer
- UVa 11205 - The broken pedometer
- UVA 11205 The broken pedometer
- uva 11205 The Broken Pedometer
- uva 11205 The broken pedometer
- uva 11205 - The broken pedometer
- UVa 11205 - The broken pedometer
- UVA 11205 The broken pedometer
- java基础之集合类
- Android之SurfaceView学习-----using SurfaceView class创建一个view
- How do I set the DISPLAY variable on Linux
- 图解Git
- xmapp 1.7.4 安装及apahce无法启动的解决办法
- UVaOJ 11205 - The broken pedometer
- 开始认真学习Android编程
- 公有继承、私有继承和保护继承之间的对比
- 第五天 C#字符串函数以及C#面向对象
- OAuth简介
- Qt中实现将float类型转换为QString类型
- Linux之awk详解
- Android组件之Dialog使用
- Convert encoding --- 解决乱码问题