zoukankan      html  css  js  c++  java
  • SGU 220.Little Bishops(DP)

    题意:

           给一个n*n(n<=10)的棋盘,放上k个主教(斜走),求能放置的种类总数。


    Solution:

                 一眼看上去感觉是状压DP,发现状态太多,没办法存下来。。。

                 下面是一个十分巧妙的处理:

                 将棋盘按照国际象棋的样子分成黑白两部分,再旋转45°,以黑色为例,一行有1,3,5,7。。。5,3,2,1个格子,

                 可以处理为1,1,3,3,5,5,7。。。

                  f[i][j]代表第i层,放了j个棋子的方案数,只要预处理出每一行可以放的个数tem[i]

         f[i][j]=f[i-1][j]+f[i-1][j-1]*(tem[i]-j+1),tem[i]>=j;

                 同样对白色部分如此处理,最后将对应的黑白方案乘起来累加就好了。

                 注意答案会超过INT

    code

    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    long long  f[2][100][100], ans;
    int tem[100];
    int n, k, tol;
    
    void make (int x) {
        tol = 0;
        for (int t = x; t <= n; t += 2) {
            tem[++tol] = t;
            if (t != n) tem[++tol] = t;
        }
        f[x - 1][0][0] = 1;
        for (int i = 1; i <= tol; i++)
            for (int j = 0; j <= k; j++)
                if (tem[i] >= j) f[x - 1][i][j] = f[x - 1][i - 1][j]+f[x - 1][i - 1][j - 1] * (tem[i] - j + 1);
    }
    int main() {
        scanf ("%d %d", &n, &k);
        make (1);
        make (2);
        for (int i = 0; i <= k; i++)
            ans += f[1][tol][i] * f[0][2 * n - 1 - tol][k - i];
        printf ("%I64d", ans);
    }
    View Code
  • 相关阅读:
    python和matlab
    进程和线程的主要区别
    如何理解卷积
    Leetcode 961. N-Repeated Element in Size 2N Array
    Leetcode 387. First Unique Character in a String
    Python ord()与chr()函数
    Leetcode 709. To Lower Case
    python 字符串大小写相关函数
    Leetcode 367. Valid Perfect Square
    Leetcode 1014. Capacity To Ship Packages Within D Days
  • 原文地址:https://www.cnblogs.com/keam37/p/4031788.html
Copyright © 2011-2022 走看看