显示原始代码
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define il inline
#define ll long long
using namespace std;
const int N = 35;
int n;
ll d[N];
int stk[5][5], cnt[4];
struct node {
int fr, to;
} a[N];
bool vis[4];
char s[4];
il void dfs(int p, int c, int lst) {
if (cnt[1] == c || cnt[2] == c) {
d[c] = p;
return;
}
For(i, 1, 6) {
int j = a[i].fr, k = a[i].to;
if (cnt[j] && j != lst) {
if (stk[j][cnt[j]] < stk[k][cnt[k]] || !stk[k][cnt[k]]) {
stk[k][++cnt[k]] = stk[j][cnt[j]];
cnt[j]--;
dfs(p + 1, c, k);
break;
}
}
}
}
int main() {
scanf("%d", &n);
For(i, 1, 6) {
scanf("%s", s);
a[i].fr = s[0] - 'A', a[i].to = s[1] - 'A';
}
stk[0][++cnt[0]] = 1;
dfs(0, 1, -1);
cnt[1] = cnt[2] = cnt[0] = 0;
For(i, 1, 2) stk[0][++cnt[0]] = 3 - i;
dfs(0, 2, -1);
cnt[1] = cnt[2] = cnt[0] = 0;
For(i, 1, 3) stk[0][++cnt[0]] = 4 - i;
dfs(0, 3, -1);
if (n <= 3)
cout << d[n];
else {
ll k = (d[3] - d[2]) / (d[2] - d[1]), q = d[3] - k * d[2];
For(i, 4, n) d[i] = 1ll * k * (d[i - 1]) + q;
cout << d[n];
}
return 0;
}