uva702 The Vindictive Coach(DP)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=643

 

#include <iostream>
#include <cstring>
#define ll long long
using namespace std;
int n,m;
ll dp[30][30][2],ans;
bool vis[30];
ll DP(int l,int v,int c){
    if(dp[l][v][c]!=-1) return dp[l][v][c];
    if(l==n) return 1;
    ll ret=0;
    if(c==1){
        for(int i=v+1;i<=n;i++){
           if(!vis[i]){
               vis[i]=1;
               ret+=DP(l+1,i,0);
               vis[i]=0;
           }
        }
    }else{
        for(int i=v-1;i>=1;i--){
           if(!vis[i]){
               vis[i]=1;
               ret+=DP(l+1,i,1);
               vis[i]=0;
           }
        }
    }
    return dp[l][v][c]=ret;
}
int main(){
    while(cin>>n>>m){
        if(n==1&&m==1) {cout<<1<<endl;continue;}
        bool check=0;
        ans=0;
        for(int i=1;i<=m-1;i++){
            memset(vis,0,sizeof vis);
            vis[m]=1;
            memset(dp,-1,sizeof dp);
            vis[i]=1;
            ans+=DP(2,i,1);
        }
        if(ans){
            cout<<ans<<endl;
            continue;
        }
        for(int i=m+1;i<=n;i++){
            memset(vis,0,sizeof vis);
            vis[m]=1;
            memset(dp,-1,sizeof dp);
            vis[i]=1;
            ans=DP(2,i,0);
            if(ans){
                cout<<ans<<endl;
                check=1;break;
            }
        }
        if(!check) cout<<0<<endl;
    }
    return 0;
}
uva702

 

posted @ 2014-02-09 20:47  wonderzy  阅读(240)  评论(0编辑  收藏  举报