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

X g b e d c f a
1 1 0 1 1 0 1 1
2 0 0 0 0 1 1 0
3 0 0 1 0 0 1 0
4 0 0 1 1 0 0 1
5 0 1 1 0 0 0 0
6 0 1 0 0 0 0 0
7 1 0 1 1 0 1 0
8 0 0 0 0 0 0 0
9 0 0 1 0 0 0 0

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 ≤ in, 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 < jn, xixj 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.



Author: WANG, Yucheng
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple

Submit    Status

一点思路都没有,那只能暴力了,

7! * 1e5 = 5e8会T

其实可以一直剪枝,每次dfs的时候,设排列数为now[i]表示放在第i位的字母是now[i],那么,比如1的是"1001111",如果你把第2位放的字母不是b或c,则不处理下去。

biao[i][j]表示数字i的第j位本来应该的状态,0/1

str[i][j]表示数字i的第j位的状态。

那么如果第一位我放的是字母e,本来第一位的状态应该是biao[i][1],现在放了字母e,状态是str[i][e],判断一下是否相等即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
int f[] = {0, 1, 2, 3, 4, 5, 6, 7};
char str[20][44];
int arr[22];
int biao[10][10] = {
{0},
{-1, 1, 0, 0, 1, 1, 1, 1, 5},
{-1, 0, 0, 1, 0, 0, 1, 0, 2},
{-1, 0, 0, 0, 0, 1, 1, 0, 2},
{-1, 1, 0, 0, 1, 1, 0, 0, 3},
{-1, 0, 1, 0, 0, 1, 0, 0, 2},
{-1, 0, 1, 0, 0, 0, 0, 0, 1},
{-1, 0, 0, 0, 1, 1, 1, 1, 4},
{-1, 0, 0, 0, 0, 0, 0, 0, 0},
{-1, 0, 0, 0, 0, 1, 0, 0, 1},
};
bool flag;
bool vis[33];
int n;
int now[33];
void dfs(int cur) {
    if (flag) return;
    if (cur == 7 + 1) {
        printf("YES\n");
        flag = true;
        return;
    }
    for (int i = 1; i <= 7; ++i) {
        if (vis[i]) continue;
        now[cur] = i;
        bool flag = true;
        for (int k = 1; k <= n; ++k) {
            if (biao[arr[k]][cur] != str[arr[k]][i] - ‘0‘) {
                flag = false;
                break;
            }
        }
        if (flag) {
            vis[i] = true;
            dfs(cur + 1);
            vis[i] = false;
        }
    }
}
void work() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        int id;
        scanf("%d", &id);
        scanf("%s", str[id] + 1);
        arr[i] = id;
    }
    for (int i = 1; i <= n; ++i) {
        int cnt = 0;
        for (int j = 1; j <= 7; ++j) {
            cnt += str[arr[i]][j] == ‘1‘;
        }
        if (cnt != biao[arr[i]][8]) {
            printf("NO\n");
            return;
        }
    }
    memset(vis, 0, sizeof vis);
    flag = false;
    dfs(1);
    if (flag == false) {
        printf("NO\n");
    }
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    int t;
    scanf("%d", &t);
    while (t--) {
        work();
    }
    return 0;
}

时间: 2024-12-25 02:55:25

ZOJ Seven-Segment Display 暴力dfs + 剪枝的相关文章

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

zoj 2734 Exchange Cards【dfs+剪枝】

Exchange Cards Time Limit: 2 Seconds      Memory Limit: 65536 KB As a basketball fan, Mike is also fond of collecting basketball player cards. But as a student, he can not always get the money to buy new cards, so sometimes he will exchange with his

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

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

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

ZOJ 3631 Watashi&#39;s BG DFS

J - Watashi's BG Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3631 Appoint description: Description Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer trainin

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4

DFS + 剪枝策略

一:简介 (1)相信做过ACM的人,都很熟悉图和树的深度优先搜索:算法里面有蛮力法 -- 就是暴力搜索(不加任何剪枝的搜索): (2)蛮力搜搜需要优化时,就是需要不停的剪枝,提前减少不必要的搜索路径,提前发现判断的过滤条件: (3)剪枝的核心问题就是设计剪枝判断方法,哪些搜索路径应当舍弃,哪些搜索路径不能舍弃(保留): (4)高效的剪枝过滤条件需要从局部和全局来考虑问题,发现内在的规律. (5)详细的剪枝算法,请见剪枝算法(算法优化) 二:示例验证 (1)题目来源于 poj 1011 Stick