UVA 10401 Injured Queen Problem

 

UVA_10401

 

    这个题目本身并不难,但是如果用gets读的话要注意输入里面有空行。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAXD 20
char b[MAXD];
int N;
long long int f[MAXD][MAXD];
void solve()
{
int i, j, k, r;
long long int res;
N = strlen(b);
memset(f, 0, sizeof(f));
if(b[0] == '?')
{
for(i = 0; i < N; i ++)
f[0][i] = 1;
}
else
{
if(isdigit(b[0]))
j = b[0] - '1';
else
j = b[0] - 'F' + 14;
f[0][j] = 1;
}
for(i = 1; i < N; i ++)
{
if(b[i] == '?')
{
for(j = 0; j < N; j ++)
{
for(k = 0; k < j - 1; k ++)
f[i][j] += f[i - 1][k];
for(k = j + 2; k < N; k ++)
f[i][j] += f[i - 1][k];
}
}
else
{
if(isdigit(b[i]))
j = b[i] - '1';
else
j = b[i] - 'F' + 14;
for(k = 0; k < j - 1; k ++)
f[i][j] += f[i - 1][k];
for(k = j + 2; k < N; k ++)
f[i][j] += f[i - 1][k];
}
}
res = 0;
for(i = 0; i < N; i ++)
res += f[N - 1][i];
printf("%lld\n", res);
}
int main()
{
while(scanf("%s", b) == 1)
{
solve();
}
return 0;
}



你可能感兴趣的:(uva)