标签:dp
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#define N 30
using namespace std;
typedef long long ll;
char s[N];
int vis[N];
ll dp[N][N];
ll ans;
int main() {
while (scanf("%s", s + 1) != EOF) {
ans = 0;
memset(vis, 0, sizeof(vis));
memset(dp, 0, sizeof(dp));
int len = strlen(s + 1);
for (int i = 1; i <= len; i++) {
if (s[i] >= ‘1‘ && s[i] <= ‘9‘) {
vis[i] = s[i] - ‘0‘;
} else if (s[i] >= ‘A‘ && s[i] <= ‘F‘) {
vis[i] = s[i] - ‘A‘ + 10;
}
}
if (vis[1]) {
dp[vis[1]][1] = 1;
} else {
for (int i = 1; i <= len; i++) {
dp[i][1] = 1;
}
}
for (int i = 2; i <= len; i++) {
if (vis[i]) {
for (int k = 1; k <= len; k++) {
if (abs(vis[i] - k) > 1) {
dp[vis[i]][i] += dp[k][i - 1];
}
}
} else {
for (int j = 1; j <= len; j++) {
for (int k = 1; k <= len; k++) {
if (abs(j - k) > 1) {
dp[j][i] += dp[k][i - 1];
}
}
}
}
}
for (int i = 1; i <= len; i++) {
ans += dp[i][len];
}
printf("%lld\n", ans);
}
return 0;
}
uva 10401 Injured Queen Problem(DP)
标签:dp
原文地址:http://blog.csdn.net/llx523113241/article/details/45875319