码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1087 [SCOI2005]互不侵犯King (状压DP)

时间:2017-11-11 17:42:58      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:submit   hint   www.   mon   algo   sample   stdout   for   左右   

1087: [SCOI2005]互不侵犯King

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 4622  Solved: 2678
[Submit][Status][Discuss]

Description

  在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上
左下右上右下八个方向上附近的各一个格子,共8个格子。

Input

  只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N)

Output

  方案数。

Sample Input

3 2

Sample Output

16

HINT

 

Source

析:dp[i][j][s] 表示前 i 行放 j 个king,状态为 s 时有多少情况,然后很简单的转移。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 10;
const int maxm = 3e5 + 10;
const ULL mod = 3;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

LL dp[13][100][600];
vector<P> state[600];

bool judge(int s1, int s2){
  for(int k = 0; k < n; ++k) if(s2&1<<k){
    if(s1&1<<k)  return false;
    if(k && s2&1<<k-1)  return false;
    if(k && s1&1<<k-1)  return false;
    if(k + 1 < n && s1&1<<k+1)  return false;
  }
  return true;
}

int main(){
  scanf("%d %d", &n, &m);
  int all = 1<<n;
  FOR(i, 0, all) FOR(j, 0, all)if(judge(i, j)){
    int cnt = 0;
    for(int k = 0; k < n; ++k)  if(j&1<<k)  ++cnt;
    state[i].pb(P(j, cnt));
  }
  dp[0][0][0] = 1;
  for(int i = 1; i <= n; ++i)  FOR(j, 0, all){
    for(int k = 0; k < state[j].sz; ++k){
      P &p = state[j][k];
      for(int l = 0; l + p.se <= m; ++l)
        dp[i][l+p.se][p.fi] += dp[i-1][l][j];
    }
  }

  LL ans = 0;
  for(int i = 0; i < all; ++i)  ans += dp[n][m][i];
  printf("%lld\n", ans);
  return 0;
}

  

BZOJ 1087 [SCOI2005]互不侵犯King (状压DP)

标签:submit   hint   www.   mon   algo   sample   stdout   for   左右   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7819350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!