sgu220Little Bishops(dp)

版权声明:转我原创记得说你是我的脑残粉哟 https://blog.csdn.net/zjy2015302395/article/details/77564617

题意:

在n*n的棋盘上放K个象,使得两两之间不互相攻击。有多少种放法?

tip:

棋盘按照(i+j)的奇偶黑白染色,则在黑色格子中放的象不可能攻击到白色格子,分开考虑并将棋盘翻转45度。(旋转后就是一行一列只能放一个)
这里写图片描述

f[i][j]=f[i-1][j]+f[i-1][j-1]*(a[i]-(j-1))
f[i][j]表示前i行放j个车的方案数,a[i]表示第i行的可放位置数。最后枚举在黑色格子中放置的象的个数,剩下的象放在白色格子中。
因为格子先增后减:
一个是:cnt = (i+1)/2 * 2-1;
另一个是:cnt = i/2 * 2;且少一行(2~n)

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 12;
const int maxm = maxn*maxn;
LL n,m,dp1[maxn][maxm],dp2[maxn][maxm];
void init(){
    memset(dp1,0,sizeof(dp1));
    memset(dp2,0,sizeof(dp2));
    dp1[0][0] = dp2[1][0] = 1;
}
void sov(){
    for(LL i = 1; i <= n ; i++){
        dp1[i][0] = dp1[i-1][0];
        LL cnt = (i+1)/2 * 2-1;
        for(LL j = 1; j <= (min(cnt,m)) ; j++ )
            dp1[i][j] = dp1[i-1][j]+dp1[i-1][j-1] * (cnt-j+1);
    }

    for(LL i = 2; i <= n ; i++){
        dp2[i][0] = dp2[i-1][0];
        LL cnt = i/2 * 2;
        for(LL j = 1; j <= min(cnt,m) ; j++)
            dp2[i][j] = dp2[i-1][j] + dp2[i-1][j-1] * (cnt-j+1);
    }
}
void print(){
    LL ans = 0;
    for(int i = 0 ; i <= m ; i++){
        ans += dp1[n][i] * dp2[n][m-i];
    }
    printf("%lld\n",ans);
}
int main(){
    while(~scanf("%lld%lld",&n,&m)){
        if(n == 0 && m == 0)    break;
        init();
        sov();
        print();
    }
}
阅读更多

Bishops

05-18

Problem DescriptionnYesterday was Sam's birthday. The most interesting gift was definitely the chessboard. Sam quickly learned the rules of chess and defeated his father, all his friends, his little sister, and now no one wants to play with him any more. nnSo he decided to play with another birthday gift – a Book of Math Problems for Young Mathematicians. He opened the book somewhere in the middle and read the following problem: "How many knights can be placed on a chessboard without threatening each other?" After a while he realized that this was trivial and moved on to the next problem: "How many bishops can be placed on a chessboard without threatening each other?". Sam is in trouble here. He is not able to solve this problem and needs your help. nnTask SpecificationnnSam's chessboard has size N×N. A bishop can move to any distance in any of the four diagonal directions. A bishop threatens another bishop if it can move to the other bishop's position. Your task is to compute the maximum number of bishops that can be placed on a chessboard in such a way that no two bishops threaten each other. nn nnInputnThe input file consists of several lines. The line number i contains a single number representing the size( <10^100) of the i-th chessboard.n nnOutputnThe output file should contain the same number of lines as the input file. The i-th line should contain one number – the maximum number of bishops that can be placed on i-th chessboard without threatening each other. n nnSample Inputn2n3n nnSample Outputn2n4

没有更多推荐了,返回首页