ZOJ Seven-Segment Display 暴力dfs + 剪枝
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954
|
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 ≤ 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.
Author: WANG, Yucheng
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
一点思路都没有,那只能暴力了,
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[] = {, , , , , , , };
char str[][];
int arr[];
int biao[][] = {
{},
{-, , , , , , , , },
{-, , , , , , , , },
{-, , , , , , , , },
{-, , , , , , , , },
{-, , , , , , , , },
{-, , , , , , , , },
{-, , , , , , , , },
{-, , , , , , , , },
{-, , , , , , , , },
};
bool flag;
bool vis[];
int n;
int now[];
void dfs(int cur) {
if (flag) return;
if (cur == + ) {
printf("YES\n");
flag = true;
return;
}
for (int i = ; i <= ; ++i) {
if (vis[i]) continue;
now[cur] = i;
bool flag = true;
for (int k = ; k <= n; ++k) {
if (biao[arr[k]][cur] != str[arr[k]][i] - '') {
flag = false;
break;
}
}
if (flag) {
vis[i] = true;
dfs(cur + );
vis[i] = false;
}
}
}
void work() {
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
int id;
scanf("%d", &id);
scanf("%s", str[id] + );
arr[i] = id;
}
for (int i = ; i <= n; ++i) {
int cnt = ;
for (int j = ; j <= ; ++j) {
cnt += str[arr[i]][j] == '';
}
if (cnt != biao[arr[i]][]) {
printf("NO\n");
return;
}
}
memset(vis, , sizeof vis);
flag = false;
dfs();
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 ;
}
ZOJ Seven-Segment Display 暴力dfs + 剪枝的更多相关文章
- zoj 2734 Exchange Cards【dfs+剪枝】
Exchange Cards Time Limit: 2 Seconds Memory Limit: 65536 KB As a basketball fan, Mike is also f ...
- ZOJ 3962 Seven Segment Display 16进制的八位数加n。求加的过程中所有的花费。显示[0,F]有相应花费。
Seven Segment Display Time Limit: Seconds Memory Limit: KB A seven segment display, or seven segment ...
- ZOJ 3962 E.Seven Segment Display / The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple E.数位dp
Seven Segment Display Time Limit: 1 Second Memory Limit: 65536 KB A seven segment display, or s ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- ZOJ 3962 Seven Segment Display
Seven Segment Display 思路: 经典数位dp 代码: #include<bits/stdc++.h> using namespace std; #define LL l ...
- 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)
//never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...
- 2017浙江省赛 E - Seven Segment Display ZOJ - 3962
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目: A seven segment display, or ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- (2017浙江省赛E)Seven Segment Display
Seven Segment Display Time Limit: 2 Seconds Memory Limit: 65536 KB A seven segment display, or ...
随机推荐
- gitblit安装使用
1.下载地址 http://www.gitblit.com/ 2.安装jdk(自行安装) 3.解压gitblit # tar -zxvf gitblit-1.8.0.tar.gz 4.配置# cd g ...
- [SHOI 2017] 组合数问题
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4870 [算法] 回顾组合数的定义 : C(N , M)表示将N个小球放入M个盒子里的 ...
- c# aop讲解
先说下场景,C#中为什么要使用Aop,而我又是在哪里使用Aop? 本人只是想拦截实体类的Set的方法,然后在Set之前,调用一下其它方法,把值赋给另一个对象. 而我做的都是在实体类的基类里处理: 比如 ...
- Walk of Length 6
简要题意: 给一n(n<=2000)个点的有标号无向图,在图上从1出发走六步回到1,问有多少种不是六元简单环的情况. 解法: 用暴力找到31种走法,环有9种形状: 分为9种,统计出每一种情况的方 ...
- 《Java多线程编程核心技术》读后感(一)
1.继承Thread package First; public class MyThread extends Thread { public void run() { super.run(); Sy ...
- 3-C++程序的结构1.1
数据的共享和保护机制是C++的重要特性之一. 1.标识符的作用域与可见性 作用域讨论的是标识符的有效范围,可见性是讨论标识符是否可以被引用. a.作用域 作用域是一个标识符在程序正文中有效的区域.C+ ...
- 模板 - 数据结构 - ST表 + 二维ST表
区间最大值,$O(nlogn)$ 预处理,$O(1)$ 查询,不能动态修改.在查询次数M显著大于元素数量N的时候看得出差距. 令 $f[i][j]$ 表示 $[i,i+2^j-1]$ 的最大值. 显然 ...
- art-template在项目中的应用
art-template 是一个简约.超快的模板引擎.它采用作用域预声明的技术来优化模板渲染速度,从而获得接近 JavaScript 极限的运行性能,并且同时支持 NodeJS 和浏览器. 下面介绍在 ...
- DLL中加载其它DLL使用LoadLibrary加载动态库失败的解决办法
方式一 采用LoadLibraryEx 若DLL不在调用方的同一目录下,可以用LoadLibrary(L"DLL绝对路径")加载.但若调用的DLL内部又调用另外一个DLL,此时调用 ...
- lightoj1026【tarjan】
题意: 据说就是找桥: 思路: 无敌RE......是cmp写挫了...现在数组开太大了 模板题: #include <bits/stdc++.h> using namespace std ...








