UVaOJ 11205 - The broken pedometer

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情


——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

参考资料:沐阳博客


图片处理工具以及录屏工具 1、可以进行图片处理,截小图、整屏截图、滚动截图等; 2、可以进行屏幕录制; 3、屏幕取色、屏幕标尺等。 立即下载

相关推荐

windows系统播放MP4视频HEVC视频扩展工具

windows系统播放MP4视频HEVC视频扩展工具

UVA 11205 The broken pedometer 状态压缩+搜索

The Broken Pedometer  The Problem A marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs):

Clearlemon 735

Visual-Coding-Harness-Evidence-Retention-Window-Planner-v1.0-原创源码与文档.zip

本批资源均为独立编写的可运行 JavaScript 工程工具,包含完整源码、README、MIT License、原创与授权声明、可复现示例、自动化测试、离线 JSON/HTML/SVG 报告和真实运行截图。适合前端开发、AI 工程、自动化测试及技术研究人员用于本地预检、证据整理和二次开发。运行环境为 Node.js 18+,压缩包不含账号、密钥、Cookie、模型权重、品牌素材或第三方受限内容。

UVA11205 The broken pedometer【位运算+暴力】

A marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs): 问题链接:UVA11205 The broken pedometer 问题简述:(略) 问题分析: &amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;...

海岛Blog 659

UVA 11205 The broken pedometer

题目如下: The Broken Pedometer  The Problem A marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs): But the

hexiecs的技术专栏 904

UVa 11205 The broken pedometer (枚举好题&巧用二进制)

11205 - The broken pedometer Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&amp;Itemid=8&amp;category=107&amp;page=show_problem&amp;problem=2146 The Probl...

844604778 165

The broken pedometer-纯暴力枚举

The broken pedometer Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  The Broken Pedometer  T

qq_18661257的专栏 770

UvaOJ 11205 The broken pedometer

暴力枚举法 预处理b[i]数组,按序储存所有“二进制有i个1的数”,每行大小在bn中 b[16][7000]:16表示最多用到b[15](二进制下有15个1),7000表示要大于C(15, 7)=6435 log变量表示最少需要多少位才能表示n个状态,其实是log(n)+1 首先用变量i枚举有几个1,再用变量j枚举具体的值,统计以b[i][j]做掩码后是否所有值均不相同。

baijinze的专栏 582

16A.rar

当 CAD 缺失对应字体时,图纸文字会显示异常,出现乱码、问号。将下载好的字体文件复制到 AutoCAD 的 Fonts 文件夹中,即可恢复正常显示。

政府如何解决科技项目评估效率低、标准不统一、信息不对称的问题?.docx

政府如何解决科技项目评估效率低、标准不统一、信息不对称的问题?

某老年人办公楼电气图纸.dwg.rar

某老年人办公楼电气图纸.dwg.rar

门卫电气设计图.pdf.rar

门卫电气设计图.pdf.rar

某联通通信机房消防图.dwg.rar

某联通通信机房消防图.dwg.rar

高校如何科学评价科研项目质量以优化资源配置?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

简易智能电动车4.rar

简易智能电动车4.rar

科技园区如何精准评估和招引符合产业定位的高质量科创项目?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

科技楼电气施工图.pdf.rar

科技楼电气施工图.pdf.rar

用 Qt Widgets 手绘一个火箭扫描圆形进度控件

用 Qt Widgets 手绘一个火箭扫描圆形进度控件

上一篇: long long 与 int(HDOJ 2294)
下一篇: UVaOJ 131 – The Psychic Poker Player
Ra_WinDing
博客等级 码龄14年 50粉丝 125原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值