因为有状态转移,可以说是一道DP,递归代码
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#define MAXN 100
using std::cin;
using std::cout;
using std::endl;
int start[MAXN], end[MAXN];
typedef long long LL;
LL dp(int* p, int i, int final) {
if (i == 0) return 0;
if (p[i] == final) return dp(p, i - 1, final);
return dp(p, i - 1, 6 - final - p[i]) + (1ll << (i - 1));
}
int main() {
int n, now = 0;
while (scanf("%d", &n) != EOF) {
if (n == 0) break;
for (int i = 1; i <= n; ++ i) scanf("%d", &start[i]);
for (int i = 1; i <= n; ++ i) scanf("%d", &end[i]);
int k;
for (k = n; k >= 1; -- k) {
if (start[k] != end[k]) break;
}
//cout << k << endl;
LL res = 0;
if (k > 0) {
res = dp(start, k - 1, 6 - start[k] - end[k])
+ dp(end, k - 1, 6 - start[k] - end[k]) + 1;
}
printf("Case %d: ", ++ now);
cout << res << endl;
}
}