sgu233 little kings

题目大意: 有n*n的棋盘上放k个国王。国王可以攻击与它相邻的八个格子。现在要使国王不相互攻击,有多少种放置的方案数。一个格子不能放两个国王。

n<=10,k<=n*n。

分析:简单的状态压缩DP。f[i][state][j]表示第i行放置国王的状态为state,前i行一共放了j个国王的方案数,state为位压缩表示的状态,某位为1,表示该处放了国王,为0表示没有放。合法的state状态数是有限的,所以,可以预处理出一行当中所有合法的state状态,保存在数组中。

f[i][p1][j]=∑(f[i-1][p2][j-cnts[state]]) {(state[p2]&state[p1])==0&&((state[p2]>>1)&state[p1]==0)&&((state[p2]<<1)&state[p1]==0)}

最后的答案即为∑f[n][j][k] {枚举j}

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int n,k,num=0,s[1200],c[1200];
 6 long long f[2][1100][120];
 7 void dfs(int i,int state,int cnt)
 8 {
 9     if(i>=n)
10     {s[++num]=state;
11      c[num]=cnt;
12         return;
13     }
14     dfs(i+1,state<<1,cnt);
15     if(state&1)return;
16     dfs(i+1,(state<<1)+1,cnt+1);
17 }
18 int main()
19 {
20     while(scanf("%d%d",&n,&k)!=-1)
21     {
22     memset(f,0,sizeof f);
23     num=0;
24     dfs(0,0,0);
25     for(int i=1;i<=n;i++)
26     {
27         for(int j=1;j<=num;j++)
28         {if(i==1)f[i&1][j][c[j]]=1;
29             else
30             for(int p=c[j];p<=k;p++)
31             {
32                 f[i&1][j][p]=0;
33                 for(int x=1;x<=num;x++)
34                 {
35                     if(c[x]+c[j]<=p&&(s[x]&s[j])==0&&((s[x]&(s[j]<<1))==0)&&((s[x]&(s[j]>>1))==0))
36                         f[i&1][j][p]+=f[!(i&1)][x][p-c[j]];
37                 }
38             }
39         }
40     }
41     long long ans=0;
42     for(int j=1;j<=num;j++)
43         ans+=f[n&1][j][k];
44     printf("%I64d\n",ans);
45 }
46 }
时间: 2024-10-05 04:13:12

sgu233 little kings的相关文章

POJ2699 The Maximum Number of Strong Kings

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats

【POJ2699】The Maximum Number of Strong Kings

Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a tournament T, the score of x is the number of playe

POJ2699_The Maximum Number of Strong Kings

这题目,,,真是...诶.坑了好久. 给一个有向图.U->V表示U可以打败V并得一分. 如果一个人的得分最高,或者他打败所有比自己得分高的人,那么此人就是king. 现在给出每个人的得分,求最多可能有多少个king同时存在. 可以证明,如果有k个人是king,那么至少有一种分配方案使得这k个king都是分数最高的那k个人.(证明略,想想就知道了) 于是我们可以开始枚举从i个人开始,后面的都是king. 除了源点和汇点以外,还有两种点,一种表示人(n),一种表示比赛(n*(n/2)/2). 如果一

SGU 223.Little Kings

时间限制:0.25s 空间限制:4M 题意: 在 n*n(n≤10)的棋盘上放 k (k<=n*n)个国王(可攻击相邻的 8 个格子),求使它们无法互相攻击的方案数. Solution: 采用状态压缩,用k位(k<=n)的二进制数1的位置代表棋盘放置的国王的位置. 先用预处理,求出statu[i]仅考虑一行时满足要求的方案,c[i]是statu[i]状态放置的棋子数. f[i][j][p] 代表第i行放置状态为 p时且已经放置了j个棋子的方案数 状态转移方程:f[i][j][p]=Σf[i-1

POJ 2699 The Maximum Number of Strong Kings Description

The Maximum Number of Strong Kings Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a tournament T, th

Dwarves, Elves, Wizards, and Kings

Dwarves, Elves, Wizards, and Kings Evan Cofsky in nEAl STEpHEnSon'S novEl CryptonomiCon (EoS), Randy Water- house explains his classification system for the different types of people he meets. Dwarves are hard workers, steadily producing beautiful ar

[2016-04-17][Gym][100947][D][The Three Kings of Asgard]

时间:2016-04-17 16:45:30 星期日 题目编号:[2016-04-17][Gym][100947][D][The Three Kings of Asgard] 题目大意:A给B和当前一样多的金币,B给C,然后C又给A,结束之后,A,B,C三人的金币刚好一样多,问原来A,B,C各有多少个金币,不可能就输出 Impossible 分析: 直接解方程组 ?????2×(a?b)=n2×b?c=n2c?(a?b)=n{2×(a?b)=n2×b?c=n2c?(a?b)=n 得到 ?????

解题报告 之 POJ2699 The Maximum Number of Strong Kings

解题报告 之 POJ2699 The Maximum Number of Strong Kings Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a t

【poj2699】 The Maximum Number of Strong Kings

http://poj.org/problem?id=2699 (题目链接) 题意 给出1张有向完全图.U->V表示U可以打败V并得一分.如果一个人的得分最高,或者他打败所有比自己得分高的人,那么此人就是king.现在按顺序给出每个人的得分,求最多可能有多少个king同时存在. Solution 想了半天贪心,然而得分相等的情况真的很不好处理..真的没想到是最大流..左转题解:http://blog.csdn.net/sdj222555/article/details/7797257 考虑这样建图