试题E 七段码
【问题描述】
小蓝要用七段码数码管来表示一种特殊的文字。
七段码上图给出了七段码数码管的一个图示,数码管中一共有7 段可以发光的二极管,分别标记为a, b, c, d, e, f, g。小蓝要选择一部分二极管(至少要有一个)发光来表达字符。在设计字符的表达时,要求所有发光的二极管是连成一片的。
例如:b 发光,其他二极管不发光可以用来表达一种字符。
例如:c 发光,其他二极管不发光可以用来表达一种字符。这种方案与上一行的方案可以用来表示不同的字符,尽管看上去比较相似。
例如:a, b, c, d, e 发光,f, g 不发光可以用来表达一种字符。
例如:b, f 发光,其他二极管不发光则不能用来表达一种字符,因为发光的二极管没有连成一片。
请问,小蓝可以用七段码数码管表达多少种不同的字符?
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一
个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
思路
利用图片的性质,将有关系的点构成无向图。然后暴力枚举每一种发光的情况,判断是否连通、计数即可。
答案:80
代码
#include<bits/stdc++.h>
using namespace std;
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define For(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(a) cout << #a << " = " << a << endl;
#define mod(x) (x) % MOD
#define ENDL "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
const int N = 10 + 10;
int vis[N], h[N], ne[N], e[N], idx, n, cnt, ans;
bool st[N];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool bfs(int u) {
queue<int> q;
st[u] = true;
q.push(u);
int sz = 1;
while (!q.empty()) {
u = q.front();
q.pop();
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (!st[j] && vis[j]) {
st[j] = true;
sz++;
q.push(j);
}
}
}
return sz == cnt;
}
int main()
{
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cout.tie(0), cin.tie(0);
memset(h, -1, sizeof h);
cin >> n;
_for(i, 0, n) {
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
_rep(len, 1, 7) {
memset(vis, 0, sizeof vis);
For(i, 7, 8 - len) vis[i] = 1;
cnt = len;
do {
int u = 1;
while (!vis[u]) ++u;
memset(st, 0, sizeof st);
if (bfs(u)) ans++;
} while (next_permutation(vis + 1, vis + 8));
}
cout << ans << endl;
return 0;
}
10
1 2
1 6
2 7
2 3
3 7
3 4
4 5
5 7
5 6
6 7