ZOJ - 3954 ​​​​​​​ Seven-Segment Display (状态压缩)

Seven-Segment Display

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 displays. Seven segment displays are widely used in digital clocks, electronic meters, basic calculators, and other electronic devices that display numerical information.

The segments of a seven segment display are arranged as a rectangle of two vertical segments on each side with one horizontal segment on the top, middle, and bottom. If we refer the segments as the letters from a to g, it's possible to use the status of the segments which is called a seven segment code to represent a number. A standard combination of the seven segment codes is shown below.

 

Xabcdefg
11001111
20010010
30000110
41001100
50100100
60100000
70001111
80000000
90000100
   
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 example, the seven segment codes of permutation "gbedcfa" which is derived from the standard code by exchanging the bits represented by "a" and "g", and by exchanging the bits represented by "c" and "e", is listed as follows.

Xgbedcfa
11011011
20000110
30010010
40011001
50110000
60100000
71011010
80000000
90010000

We indicate the seven segment code of permutation p representing number x as cp, x. For example cabcdefg,7 = 0001111, and cgbedcfa,7 = 1011010.

Given n seven segment codes s1, s2, ... , sn and the numbers x1, x2, ... , xn each of them represents, can you find a permutation p, so that for all 1 ≤ i ≤ n, si = cp, xi holds?

Input

The first line of the input is an integer T (1 ≤ T ≤ 105), indicating the number of test cases. Then T test cases follow.

The first line of each test case contains an integer n (1 ≤ n ≤ 9), indicating the number of seven segment codes.

For the next n lines, the i-th line contains a number xi (1 ≤ xi ≤ 9) and a seven segment code si (|si| = 7), their meanings are described above.

It is guaranteed that ∀ 1 ≤ i < j ≤ n, xi ≠ xj holds for each test case.

Output

For each test case, output "YES" (without the quotes) if the permutation p exists. Otherwise output "NO" (without the quotes).

Sample Input

3
9
1 1001111
2 0010010
3 0000110
4 1001100
5 0100100
6 0100000
7 0001111
8 0000000
9 0000100
2
1 1001111
7 1010011
2
7 0101011
1 1101011

Sample Output

YES
NO
YES

Hint

For the first test case, it is a standard combination of the seven segment codes.

For the second test case, we can easily discover that the permutation p does not exist, as three in seven bits are different between the seven segment codes of 1 and 7.

For the third test case, p = agbfced.

 

题意:给你一个对应的表,0 表示灯亮,1 表示灯不亮。 最后输入是   先是 t ,表示测试数,然后是 n 表示接下来有 n 个表示, 每个表示以一个数字 x 开头,然后是一个长度为7 的字符串,代表这个数字 x 的表示方式,( 注意 , 这个字符串的每一个灯管除了亮的是固定的某几个编号的,其他的并不是固定 ),最后题目问这 n 个表示是否有冲突。

如 样例 中的 7 0101011  ,  其中 3 个 0 中每个 0 都表示 a , b , c 灯管中的一个。其他的 1 的灯管编号都不固定。

思路:因为除了测试数据 t , 其他的数范围都很小, 对于某一个 a , b, c, d, c, f, g 灯管,它表示某一个数的状态都是确定的,所以我们只需要在 给出的表示中   以   列为单位   判断与标准的图 能否完全匹配上(最后再判断,因为标准图的灯管在某个位置是固定的,而输入   的灯管的位置是不固定) ,而这个匹配,就是  状态压缩, 如果完全匹配,就是 YES,即没有冲突,反之就是NO。

AC代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)

using namespace std;

int STD[9][7] = {
	1,0,0,1,1,1,1,
	0,0,1,0,0,1,0,
	0,0,0,0,1,1,0,
	1,0,0,1,1,0,0,
	0,1,0,0,1,0,0,
    0,1,0,0,0,0,0,
	0,0,0,1,1,1,1,
	0,0,0,0,0,0,0,
	0,0,0,0,1,0,0
};

char mp[10][8];
int a[10][8];
map<int,int> now;   /// 存给出的图
map<int,int> ans;   /// 存标准的图
int powt[11] = {1,2,4,8,16,32,64,128,256,512,1024};

int main()
{
    INT(t);
    while(t --){
        clr(a,0); clr(mp,0);
        now.clear(); ans.clear();
        int x; INT(n);
        vector<int> in;
        rep(i ,0 ,n){
            scanf("%d",&x);
            scanf("%s",mp[x - 1]);
            in.push_back(x - 1);
        }

        rep(i,0,in.size()){               /// 输入的是字符串,转化成数字
            rep(j,0,7){
                a[in[i]][j] = mp[in[i]][j] - '0';
            }
        }
        rep(i,0,7){
            int k = 0;
            rep(j,0,in.size()){
                k += powt[j] * a[in[j]][i];         /// 状态压缩
            }
            now[k] ++;                              /// 存状态
        }
        rep(i,0,7){
            int k = 0;
            rep(j,0,in.size()){
                k += powt[j] * STD[in[j]][i];
            }
            ans[k] ++;
        }
        if(now == ans) puts("YES");
        else puts("NO");
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值