1. 程式人生 > 其它 >藍橋杯2020初賽:七段碼

藍橋杯2020初賽:七段碼

時隔一年再回來寫這道題,發現的確好簡單。

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int z(char C) {
    return C - 'a';
}

set<string> all;
vector<int> G[20];

bool check(string s) { //形成環
    set<char> S(s.begin(), s.end());
    if ((int)S.size() != (int)s.size()) return false;
    return true;
}

void dfs(int u, string res) {
    if (!check(res)) return;
    sort(res.begin(), res.end());
    if (res.size()) all.insert(res);

    for (auto& v : G[u]) {
        char vv = v + 'a';
        dfs(v, res + vv);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a = z('a');
    int b = z('b');
    int c = z('c');
    int d = z('d');
    int e = z('e');
    int f = z('f');
    int g = z('g');
    G[a].push_back(b); G[b].push_back(a);
    G[a].push_back(f); G[f].push_back(a);

    G[b].push_back(c); G[c].push_back(b);
    G[b].push_back(g); G[g].push_back(b);

    G[f].push_back(g); G[g].push_back(f);
    G[f].push_back(e); G[e].push_back(f);

    G[g].push_back(c); G[c].push_back(g);
    G[g].push_back(e); G[e].push_back(g);

    G[d].push_back(c); G[c].push_back(d);
    G[d].push_back(e); G[e].push_back(d);

    for (char c = 'a'; c <= 'g'; c++) {
        dfs(z(c), "");
    }

    // for (auto z : all) {
    //     cout << z << "\n";
    // }

    cout << all.size() << "\n";

    return 0;
}