uva10401 Injured Queen Problem(DP)

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

 

#include <iostream>
#include <cstring>
#include <cstdio>
#define ll long long
using namespace std;
char s[50];
ll dp[50][50];
int f(int x){return x>=0 ? x:(-x);}
int to(char s){
   if(s>='0'&&s<='9') return s-'0';
   else return s-'A'+10;
}
int main(){
    while(~scanf("%s",s+1)){
        int l=strlen(s+1);
        memset(dp,0,sizeof dp);
        if(s[1]=='?'){
           for(int i=1;i<=l;i++) dp[1][i]=1;
        }else{
           dp[1][to(s[1])]=1;
        }
        for(int i=2;i<=l;i++){
            if(s[i-1]=='?'&&s[i]=='?'){
               for(int j=1;j<=l;j++){
                   for(int k=1;k<=l;k++){
                      if(f(j-k)>=2) dp[i][k]+=dp[i-1][j];
                   }
               }
            }else if(s[i-1]!='?'&&s[i]=='?'){
               for(int k=1;k<=l;k++){
                    if(f(to(s[i-1])-k)>=2) dp[i][k]+=dp[i-1][to(s[i-1])];
               }
            }else if(s[i-1]=='?'&&s[i]!='?'){
               for(int j=1;j<=l;j++){
                    if(f(to(s[i])-j)>=2) dp[i][to(s[i])]+=dp[i-1][j];
               }
            }else{
               if(f(to(s[i])-to(s[i-1]))>=2) dp[i][to(s[i])]+=dp[i-1][to(s[i-1])];
            }
        }
        ll ans=0;
        for(int i=1;i<=l;i++){
            ans+=dp[l][i];
        }
        cout<<ans<<endl;
    }
    return 0;
}
uva10401

 

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