这个题目相对比较简单
状态: dp[i][j] 表示第i列第j行放下棋子
状态转移: dp[i][j] -> sum{dp[i+1][k]}, 0 <= k <= j-2, j-2 <= k <= strlen(str)
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 20
char str[MAXN];
unsigned long long dp[MAXN][MAXN];
unsigned long long dfs(const int &col, const int &pos)
{
unsigned long long rst(0L);
if( col == strlen(str)-0x1 ) {
return 1L;
}
if( -1 != dp[col][pos] ) {
return dp[col][pos];
}
if( '?' != str[col+1] ) {
int idx( (isdigit(str[col+1])? str[col+1]-'0'-1 : str[col+1]-'A'+9) );
if( idx >= 0 && idx < pos-1 ) {
return dp[col][pos] = dfs(col+1, idx);
}
if( idx >= pos+2 && idx < strlen(str) ) {
return dp[col][pos] = dfs(col+1, idx);
}
return dp[col][pos] = 0L;
}
for(int i = 0; i < pos-1; i ++) {
rst += dfs(col+1, i);
}
for(int i = pos+2; i < strlen(str); i ++) {
rst += dfs(col+1, i);
}
return dp[col][pos] = rst;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
unsigned long long rst;
while( ~scanf("%s", str) ) {
memset(dp, -1, sizeof(dp)); rst = 0L;
if( '?' != str[0] ) {
rst = dfs(0, (isdigit(str[0])? str[0]-'0'-1 : str[0]-'A'+9));
}
else {
for(int i = 0; i < strlen(str); i ++) {
rst += dfs(0, i);
}
}
printf("%llu\n", rst);
}
return 0;
}