UVA 10795 A Different Task(递归 状态转移)

来源:互联网 发布:阿里云架构部署方案 编辑:程序博客网 时间:2024/04/30 21:28

解析见刘汝佳的《算法竞赛入门经典训练指南》P27

#include <cstdio>#include <cstring>typedef long long ll;const int N = 65;int n, start[N], finish[N];ll f(int p[],int i,int fin) {    if(i == 0)        return 0;    if(p[i] == fin)        return f(p,i-1,fin);    return f(p,i-1,6-p[i]-fin) + (1LL << (i-1));} int main() {    int cas = 1;    while(scanf("%d",&n) != EOF && n) {        for(int i = 1; i <= n; i++) {            scanf("%d",&start[i]);        }        for(int i = 1; i <= n; i++) {            scanf("%d",&finish[i]);        }        int k = n;        while(k >= 0 && finish[k] == start[k]) {            k--;        }        ll ans = 0;        if(k >= 1) {            int other = 6 - start[k] - finish[k];            ans = f(start,k-1,other) + f(finish,k-1,other) + 1;        }        printf("Case %d: %lld\n",cas++,ans);    }    return 0;}
0 0
原创粉丝点击