zoukankan      html  css  js  c++  java
  • The broken pedometer-纯暴力枚举

    Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

     Status

    Description

    Download as PDF
     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 pedometer does not work properly (possibly the sweat affected the batteries) and only some of the LEDs are active. The runner wants to know if all the possible symbols:

    can be correctly identified. For example, when the active LEDs are:

    numbers 2 and 3 are seen as:

    so they cannot be distinguished. But when the active LEDs are:

    the numbers are seen as:

    and all of them have a different representation.

    Because the runner teaches algorithms at University, and he has some hours to think while he is running, he has thought up a programming problem which generalizes the problem of his sweat pedometer. The problem consists of obtaining the minimum number of active LEDs necessary to identify each one of the symbols, given a number P of LEDs, and N symbols to be represented with these LEDs (along with the codification of each symbol).

    For example, in the previous sample P = 7 and N = 10. Supposing the LEDs are numbered as:

    The codification of the symbols is: "0" = 1 1 1 0 1 1 1; "1" = 0 0 1 0 0 1 0; "2" = 1 0 1 1 1 0 1; "3" = 1 0 1 1 0 1 1; "4" = 0 1 1 1 0 1 0; "5" = 1 1 0 1 0 1 1; "6" = 1 1 0 1 1 1 1; "7" = 1 0 1 0 0 1 1; "8" = 1 1 1 1 1 1 1; "9" = 1 1 1 1 0 1 1. In this case, LEDs 5 and 6 can be suppressed without losing information, so the solution is 5.

    The Input

    The input file consists of a first line with the number of problems to solve. Each problem consists of a first line with the number of LEDs (P), a second line with the number of symbols (N), and N lines each one with the codification of a symbol. For each symbol, the codification is a succession of 0s and 1s, with a space between them. A 1 means the corresponding LED is part of the codification of the symbol. The maximum value of P is 15 and the maximum value of N is 100. All the symbols have different codifications.

    The Output

    The output will consist of a line for each problem, with the minimum number of active LEDs necessary to identify all the given symbols.

    Sample Input

    2
    7
    10
    1 1 1 0 1 1 1
    0 0 1 0 0 1 0
    1 0 1 1 1 0 1
    1 0 1 1 0 1 1
    0 1 1 1 0 1 0
    1 1 0 1 0 1 1
    1 1 0 1 1 1 1
    1 0 1 0 0 1 0
    1 1 1 1 1 1 1
    1 1 1 1 0 1 1
    6
    10
    0 1 1 1 0 0
    1 0 0 0 0 0
    1 0 1 0 0 0
    1 1 0 0 0 0
    1 1 0 1 0 0
    1 0 0 1 0 0
    1 1 1 0 0 0
    1 1 1 1 0 0
    1 0 1 1 0 0
    0 1 1 0 0 0
    

    Sample Output

    5
    4
    
    此题目最重要的是看懂,他的大概意思是给你个P代表着有多少位,给你个N代表着有多少个数
    终于要你求。每个数改变同样的位(题目是直接删掉同样的位。即关掉LED,让他不能执行)。最多改变多少,剩下的位依然能够表示独特性。即每个数都不同样,都能够识别出来
    所以直接用DFS枚举全部的情况(中间剪枝一小部分)就可以。
    /*
    Author: 2486
    Memory: 0 KB		Time: 33 MS
    Language: C++ 4.8.2		Result: Accepted
    VJ RunId: 4153206		Real RunId: 15826224
    Public:		No Yes
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int n,p,t,maps[105][105],Max,ind[20],ds[100+5];
    bool vis[20];
    bool C(int m) {
        int s;
        memset(vis,false,sizeof(vis));
        for(int i=0; i<m; i++) {
            vis[ind[i]]=true;
        }
        for(int i=0; i<n; i++) {
            s=0;
            for(int j=0; j<p; j++) {
                if(!vis[j]) {
                    s=s*2+maps[i][j];
                }
            }
            ds[i]=s;
        }
        sort(ds,ds+n);
        for(int i=1; i<n; i++) {
            if(ds[i]==ds[i-1])return false;
        }
        return true;
    }
    void dfs(int col,int num) {
        if(!C(num))return;
        if(col>=p) {
            Max=max(Max,num);
            return ;
        }
        ind[num]=col;
        dfs(col+1,num+1);
        dfs(col+1,num);
    }
    int main() {
        scanf("%d",&t);
        while(t--) {
            scanf("%d%d",&p,&n);
            for(int i=0; i<n; i++) {
                for(int j=0; j<p; j++) {
                    scanf("%d",&maps[i][j]);
                }
            }
            Max=0;
            dfs(0,0);
            printf("%d
    ",p-Max);
        }
        return 0;
    }
    


     Status

  • 相关阅读:
    ExcelUtil工具类-1
    图论-floyd算法-python实现
    流式编程一些简单的例子
    利用Stream实现简单的等差数列求和
    根据主机IP列表自动部署指定规模的redis cluster
    使用cgroup限制磁盘io读写速率
    consul kv导出和导入(备份)
    mysql8.0 运维相关新特性(未完待续)
    Rider写ASP.NET MVC调试报错
    网络协议知识串讲:搭建一个网络试验环境:授人以鱼不如授人以渔
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7039254.html
Copyright © 2011-2022 走看看