procedure2012
It's not worth it to know you're not worth it!

[关键字]:动态规划

[题目大意]:在一个n*n的棋盘里放k个主教(象),问使它们不能互相攻击的摆放方案有多少种。

//==================================================================

[分析]:其实不难但我没想到。首先玩过象棋的都知道黑象是吃不到白格里的,反之亦然。所以只要枚举在白格里放i个*黑格里放k-i个累加就行了。但是如何求出来呢?如果把整个棋盘旋转45度主教的行走路线就不再是斜线而是直线!他们变成了车!然后可以动态规划之:f[i][j]=f[i-1][j]+f[i-1][j-1]*(t-(j-1))前i行放j个的方案数,t表示这一行有t各空格。

[代码]:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

int bl,wl,n,m;
int black[110],white[110];
long long ans;
long long fb[11][110],fw[11][110];

void Init()
{
scanf("%d%d",&n,&m);
int i;
for (i=1;i<n;i+=2)
black[++bl]=black[++bl]=i;
if (i==n) black[++bl]=n;
for (i=2;i<n;i+=2)
white[++wl]=white[++wl]=i;
if (i==n) white[++wl]=n;
// for (int i=1;i<=bl;++i) printf("%d\n",black[i]);
// for (int i=1;i<=wl;++i) printf("%d\n",white[i]);
}

void Solve()
{
memset(fb,0,sizeof(fb));
for (int i=0;i<=bl;++i) fb[i][0]=1;
//fb[0][0]=1;
for (int i=1;i<=bl;++i)
for (int j=1;j<=black[i];++j)
{
fb[i][j]=fb[i-1][j]+fb[i-1][j-1]*(black[i]-j+1);
//printf("%d %d %I64d\n",i,j,fb[i][j]);
}
//printf("%I64d ********\n",fb[10][14]);
memset(fw,0,sizeof(fw));
for (int i=0;i<=wl;++i) fw[i][0]=1;
//fw[0][0]=1;
for (int i=1;i<=wl;++i)
for (int j=1;j<=white[i];++j)
fw[i][j]=fw[i-1][j]+fw[i-1][j-1]*(white[i]-j+1);
//printf("%I64d ********\n",fb[10][14]);
ans=0;
for (int i=0;i<=m;++i)
{
ans+=fb[bl][i]*fw[wl][m-i];
//cout<<ans<<endl;
//printf("%d %d %I64d %I64d %I64d\n",bl,i,fb[bl][i],fw[wl][m-i],ans);
}
//printf("%d %d\n",n,m);
printf("%I64d\n",ans);
}

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
Init();
Solve();
return 0;
}



posted on 2012-03-24 22:30  procedure2012  阅读(471)  评论(0编辑  收藏  举报