1087: [SCOI2005]互不侵犯King
Time Limit: 10 Sec Memory Limit: 162 MB
Description
在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上
左下右上右下八个方向上附近的各一个格子,共8个格子。
Input
只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N)
Output
方案数。
Sample Input
3 2
Sample Output
16
这道题就是HDU【2167】的加强版。。。仍然是用状态压缩来做。
在基础上增加一维来移动就OK啦!
不懂的就去看一下我前一篇博客吧YWY
传送门:http://blog.csdn.net/ljh_koqi/article/details/52301304
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<sstream>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int bit[110],num[1<<9+10];
LL f[110][(1<<9)+10][110];
int v[(1<<9)+10],vn;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int maxx=(1<<n)-1;
bit[1]=1;for(int i=2;i<=15;i++) bit[i]=bit[i-1]*2;
memset(f,0,sizeof(f));
vn=0;
for(int x=0;x<=maxx;x++)
{
if((x&(x<<1))==0)
{
v[++vn]=x;
int tmp=0;
for(int j=1;j<=n;j++)
{
if(bit[j]&x)
{
tmp++;
}
}
f[1][x][tmp]=1;
num[vn]=tmp;
}
}
for(int i=2;i<=n;i++)
{
for(int p=1;p<=vn;p++)
{
for(int q=1;q<=vn;q++)
{
if((v[p]&v[q])==0 && ((v[q]<<1)&v[p])==0 && ((v[q]>>1)&v[p])==0)
{
for(int k=num[q];k+num[p]<=m;k++)
{
f[i][v[p]][num[p]+k]+=f[i-1][v[q]][k];
}
}
}
}
}
LL ans=0;
for(int i=1;i<=vn;i++) ans+=f[n][v[i]][m];
printf("%lld\n",ans);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。