Hdu 4804 Campus Design

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1s4z5/article/details/53036300

有一个n×m(n100,m10)的棋盘,除了一些不能被覆盖的位置外其他的地方用1×21×1的骨牌填满,1×1的骨牌使用次数在[C,D]之间。问满足条件的方案数(mod109+7)


一个简单的插头dp入门题。

不能覆盖的地方可以视作必须放下1×1的骨牌并且不占使用的个数来简化代码。

因为直接列下状态数内存吃不消,用滚动数组优化即可


有的时候一些适当的注释能降低写代码的时候的脑力负担

#include<bits/stdc++.h>
using namespace std;

const int mod = 1e9+7;
char Map[112][12];
bool arr[112][12];
#define LL long long
int dp[2][1<<12][25];

inline int ge(int k,int j){return (k >> j) & 1;}
inline int sub(int k,int j){return k & (~(1<<j));}
inline int add(int k,int j){return k | (1<<j);}

int main(){
    int n,m,c,d;
    while(~scanf("%d %d %d %d",&n,&m,&c,&d)){
        memset(dp,0,sizeof(dp));
        for(int i = 1; i <= n;i++)
            scanf("%s",Map[i]);
        for(int i = 0;i <= n;i++){
            for(int j = 0;j < m;j++){
                arr[i][j] = (i>0) && (Map[i][j] == '1');
            }
        }
        int bnd = (1<<m) - 1;
        dp[0][bnd][0] = 1;
        for(int i = 1; i <= n;i++){
            for(int j = 0; j < m;j++){

                for(int k = 0 ; k <= bnd; k ++){
                    for(int lef = 0 ; lef <= d;lef++){
                        int &ndp = dp[1][k][lef];
                        if(arr[i][j]){ // can add
                            if(ge(k,j)){
                                (ndp += dp[0][sub(k,j)][lef] ) %= mod; // |
                                if(lef-1>=0) (ndp += dp[0][k][lef-1]) %= mod;  //. 
                                if(j && ge(k,j-1)) (ndp += dp[0][sub(k,j-1)][lef]) %= mod; //- 
                            }
                            else (ndp += dp[0][add(k,j)][lef])%=mod;    //empty
                        }
                        else if(ge(k,j)) (ndp += dp[0][k][lef])%=mod; //only have .
                    }
                }

                for(int k = 0 ; k <= bnd; k ++){
                    for(int lef = 0 ; lef <= d;lef++){
                        dp[0][k][lef] = dp[1][k][lef];
                        dp[1][k][lef] = 0;
                    }
                }
            }
        }
        int ans = 0;
        for(int lef = c ; lef <= d;lef++){
            (ans += dp[0][bnd][lef] ) %= mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

Campus Design

07-22

Problem DescriptionnNanjing University of Science and Technology is celebrating its 60th anniversary. In order to make room for student activities, to make the university a more pleasant place for learning, and to beautify the campus, the college administrator decided to start construction on an open space.nThe designers measured the open space and come to a conclusion that the open space is a rectangle with a length of n meters and a width of m meters. Then they split the open space into n x m squares. To make it more beautiful, the designer decides to cover the open space with 1 x 1 bricks and 1 x 2 bricks, according to the following rules:nn1. All the bricks can be placed horizontally or verticallyn2. The vertexes of the bricks should be placed on integer lattice pointsn3. The number of 1 x 1 bricks shouldn’t be less than C or more than D. The number of 1 x 2 bricks is unlimited.n4. Some squares have a flowerbed on it, so it should not be covered by any brick. (We use 0 to represent a square with flowerbet and 1 to represent other squares)nnNow the designers want to know how many ways are there to cover the open space, meeting the above requirements.n nnInputnThere are several test cases, please process till EOF.nEach test case starts with a line containing four integers N(1 <= N <= 100), M(1 <= M <= 10), C, D(1 <= C <= D <= 20). Then following N lines, each being a string with the length of M. The string consists of ‘0’ and ‘1’ only, where ‘0’ means the square should not be covered by any brick, and ‘1’ otherwise.n nnOutputnPlease print one line per test case. Each line should contain an integers representing the answer to the problem (mod 109 + 7).n nnSample Inputn1 1 0 0n1n1 1 1 2n0n1 1 1 2n1n1 2 1 2n11n1 2 0 2n01n1 2 0 2n11n2 2 0 0n10n10n2 2 0 0n01n10n2 2 0 0n11n11n4 5 3 5n11111n11011n10101n11111n nnSample Outputn0n0n1n1n1n2n1n0n2n954n n

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试