ZOJ 3954 Seven-Segment Display

二分图匹配。

先检查每个数字$1$的个数是否满足条件,不满足直接就是无解。剩下的情况可以建立二分图,如果现在的某一列可以对应于原图的某一列,那么建边。如果二分图的最大匹配是$7$,则有解,否则误解。

#include<bits/stdc++.h>
using namespace std;

int n;
int num[15],g[15][15];
char s[15][15];
int c[20];
int cx[15], cy[15];
int mk[15],nx,ny;

int path(int u)
{
    for (int v = 0; v<ny; v++)
    {
        if (g[u][v] && !mk[v])
        {
            mk[v] = 1;
            if (cy[v] == -1 || path(cy[v]))
            {
                cx[u] = v;
                cy[v] = u;
                return 1;
            }
        }
    }
    return 0;
}

int MaxMatch()
{
    int res = 0;
    memset(cx, -1, sizeof(cx));
    memset(cy, -1, sizeof(cy));
    for (int i = 0; i<nx; i++)
    {
        if (cx[i] == -1)
        {
            memset(mk, 0, sizeof(mk));
            res = res + path(i);
        }
    }
    return res;
}

char m[15][15]={
    "",
    "1001111",
    "0010010",
    "0000110",
    "1001100",
    "0100100",
    "0100000",
    "0001111",
    "0000000",
    "0000100"
};

void init()
{
    c[1]=2;
    c[2]=5;
    c[3]=5;
    c[4]=4;
    c[5]=5;
    c[6]=6;
    c[7]=3;
    c[8]=7;
    c[9]=6;
}

int main()
{
    init();  int T;
    scanf("%d",&T);
    int fail;
    while(T--)
    {
        fail=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d%s",&num[i],s[i]);
        for(int i=1;i<=n;i++)
        {
            int sum=0;
            for(int j=0;s[i][j];j++)
            {
                if(s[i][j]==‘0‘) sum++;
            }
            if(sum!=c[num[i]]) fail=1;
        }

        if(fail)
        {
            printf("NO\n");
            continue;
        }

        for(int i=0;i<=6;i++)
        {
            for(int j=0;j<=6;j++)
            {
                g[i][j]=1;
            }
        }

        for(int i=1;i<=n;i++)
        {
            for(int j=0;s[i][j];j++)
            {
                for(int k=0;k<m[num[i]][k];k++)
                {
                    if(s[i][j]!=m[num[i]][k]) g[j][k]=0;
                }
            }
        }

        nx=7,ny=7;

        int ans = MaxMatch();

        if(ans!=7) printf("NO\n");
        else printf("YES\n");

    }
    return 0;
}
时间: 2024-12-24 00:02:38

ZOJ 3954 Seven-Segment Display的相关文章

zoj 3962 Seven Segment Display(数位dp)

Seven Segment Display Time Limit: 2 Seconds      Memory Limit: 65536 KB A seven segment display, or seven segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix

2017浙江省赛 E - Seven Segment Display ZOJ - 3962

地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目: A seven segment display, or seven segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix

ZOJ 3962:Seven Segment Display(思维)

https://vjudge.net/problem/ZOJ-3962 题意:有16种灯,每种灯的花费是灯管数目,代表0~F(十六进制),现在从x开始跳n-1秒,每一秒需要的花费是表示当前的数的花费之和,问n-1秒后这段时间的花费总共是多少.跳到FFFFFFFF之后会跳回00000000. 思路:怀疑人生的题目.如果从平时计算[L,R]的花费,就计算[0,R] - [0,L-1]这样的角度来看,就会好做很多.同样如果跳到1LL<<32之后回到0,也分段考虑.这样写一个函数就可以计算了. 考虑三

ZJ省赛 2017 E.Seven Segment Display

数码管从某个状态顺序转移N个状态 计算总共有多少个数码管被点亮 N<=10^9 观察数码管的变化规律,有明显的周期和重复,利用这个性质,计算相对于初始状态,某一位上的某个状态重复了多少次,就可以在常数时间内求得. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<vector> #include<queue> #inc

MARS3.6 Programming

An Assembly Language I.D.E. To Engage Students Of All Levels * A Tutorial *2007 CCSC: Central Plains Conference Pete Sanderson, Otterbein College, [email protected] Ken Vollmar, Missouri State University, [email protected] ? ? MARS is a software simu

第十四届浙江省赛

题目真的是从易到难的顺序,而且跨度非常合理,只是看到榜单上后5题只有一支队做出来了一个,其他的没人做出来啊.. A - Cooking Competition 水题. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n, a; 5 6 int main() { 7 // freopen("in", "r", stdin); 8 int T; 9 scanf("%d",

2019省赛训练组队赛4.9周二 2017浙江省赛

A - Cooking Competition "Miss Kobayashi's Dragon Maid" is a Japanese manga series written and illustrated by Coolkyoushinja. An anime television series produced by Kyoto Animation aired in Japan between January and April 2017. In episode 8, two

数位DP Ⅱ

本节题目:数位之间和的关系.一般需要多加一维状态f[i][j][k]:表示长度为i,第一位是i,数位和等于k的数列个数 Seven Segment Display 题意:每个数字都有一个对应的权值,求l~r(十六进制数)之间所有数的所有数字的权值和 预处理数组有点麻烦,f[i][j]表示长度为i,第一个数是j的所有数列的数位之和的总和,f[i][j]=f[i-1][0~15]+j*(长度为i-1的数列个数即pow(16,i-1)). dp枚举第i位的数字时统计时还要算上last*(长度为i-1的

ZOJ Seven-Segment Display 暴力dfs + 剪枝

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on     1 = off A seven segment code of permutation p is a set of seven segment code derived from the standard code by rearranging the bits into the order indicated by p. For exampl