[SGU223]Little Kings(状压DP)

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=223

题意:n*n个格子里放k个王(走相邻8个格子),问不互相碰到的放法一共多少个。

状压dp,设dp(st,i,r)为到第i行,st状态,并且目前为止已经放了r个的所有情况。

转移为dp(cur,i,r)=∑dp(pre,i-1,r-x),x为cur的1的数量,r枚举的范围是[x,k]。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <cassert>
 24 #include <cstdio>
 25 #include <bitset>
 26 #include <vector>
 27 #include <deque>
 28 #include <queue>
 29 #include <stack>
 30 #include <ctime>
 31 #include <set>
 32 #include <map>
 33 #include <cmath>
 34 using namespace std;
 35 #define fr first
 36 #define sc second
 37 #define cl clear
 38 #define BUG puts("here!!!")
 39 #define W(a) while(a--)
 40 #define pb(a) push_back(a)
 41 #define Rint(a) scanf("%d", &a)
 42 #define Rs(a) scanf("%s", a)
 43 #define FRead() freopen("in", "r", stdin)
 44 #define FWrite() freopen("out", "w", stdout)
 45 #define Rep(i, len) for(int i = 0; i < (len); i++)
 46 #define For(i, a, len) for(int i = (a); i < (len); i++)
 47 #define Cls(a) memset((a), 0, sizeof(a))
 48 #define Clr(a, x) memset((a), (x), sizeof(a))
 49 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
 50 #define lrt rt << 1
 51 #define rrt rt << 1 | 1
 52 #define pi 3.14159265359
 53 #define RT return
 54 #define lowbit(x) x & (-x)
 55 #define onenum(x) __builtin_popcount(x)
 56 typedef long long LL;
 57 typedef long double LD;
 58 typedef unsigned long long ULL;
 59 typedef pair<int, int> pii;
 60 typedef pair<string, int> psi;
 61 typedef pair<LL, LL> pll;
 62 typedef map<string, int> msi;
 63 typedef vector<int> vi;
 64 typedef vector<LL> vl;
 65 typedef vector<vl> vvl;
 66 typedef vector<bool> vb;
 67 
 68 const int maxn = 12;
 69 LL dp[1<<maxn][maxn][maxn*maxn];
 70 int n, k;
 71 
 72 bool ok(LL x, LL y) {
 73     if(x & y) return 0;
 74     if((x << 1) & y) return 0;
 75     if((x >> 1) & y) return 0;
 76     return 1;
 77 }
 78 
 79 bool line(LL x) {
 80     if((x >> 1) & x) return 0;
 81     return 1;
 82 }
 83 
 84 signed main() {
 85     // FRead();
 86     while(~scanf("%d%d",&n,&k)) {
 87         LL nn = 1 << n;
 88         Cls(dp);
 89         Rep(st, nn) {
 90             if(line(st)) {
 91                 dp[st][1][__builtin_popcount(st)] = 1;
 92             }
 93         }
 94         For(i, 2, n+1) {
 95             Rep(cur, nn) {
 96                 if(!line(cur)) continue;
 97                 LL x = __builtin_popcount(cur);
 98                 Rep(pre, nn) {
 99                     if(!line(pre)) continue;
100                     For(r, x, k+1) {
101                         if(ok(cur, pre)) {
102                             dp[cur][i][r] += dp[pre][i-1][r-x];
103                         }
104                     }
105                 }
106             }
107         }
108         LL ret = 0;
109         Rep(i, nn) ret += dp[i][n][k];
110         cout << ret << endl;
111     }
112     RT 0;
113 }

 

posted @ 2016-09-16 16:57  Kirai  阅读(251)  评论(0编辑  收藏  举报