标签:cas include itemid other lang clu scanf task turn
在初始局面和目标局面中,找出所在柱子不同的盘子种编号最大的一个,作为参考局面,
然后分别计算从初始局面和目标局面到参考局面的步数
递归计算,\(f(P, i, final)\) 表示当前盘子 \(i\) 在柱子 \(P[i]\) 上,将前 \(i\) 个盘子都移动到柱子 \(final\) 上的方案数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 65;
int n;
int st[maxn], ed[maxn];
ll f(int *P, int i, int to){
if(i == 0) return 0;
if(P[i] == to) return f(P, i - 1, to);
return f(P, i - 1, 6 - P[i] - to) + (1ll << (i - 1));
}
ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < ‘0‘ || ch > ‘9‘){ if(ch == ‘-‘) f = -1; ch = getchar(); } while(ch >= ‘0‘ && ch <= ‘9‘){ s = s * 10 + ch - ‘0‘; ch = getchar(); } return s * f; }
int main(){
int T = 0;
while(scanf("%d", &n) == 1 && n){
for(int i = 1 ; i <= n ; ++i) scanf("%d", &st[i]);
for(int i = 1 ; i <= n ; ++i) scanf("%d", &ed[i]);
int k = n;
while(k >= 1 && st[k] == ed[k]) --k;
ll ans = 0;
if(k >= 1){
int other = 6 - st[k] - ed[k];
ans = f(st, k - 1, other) + f(ed, k - 1, other) + 1;
}
printf("Case %d: %lld\n", ++T, ans);
}
return 0;
}
UVA 10795 A Different Task (递归)
标签:cas include itemid other lang clu scanf task turn
原文地址:https://www.cnblogs.com/tuchen/p/14839055.html