UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)

题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法。

析:感觉像个DP,但是不会啊。。。就想暴力试试,反正数据量看起来不大才7,但是。。。TLE了,又换了一个暴力方法,2秒多过了,差点啊。

其实这是一个状压DP,dp[i][s]表示在第 i 列,在集合 s 中有方法数,那么怎么转移呢,这个还是挺简单的,就是判断第i+1列是不是比第 i 列都大于等于就ok了,

输入时先把行,转化成列,再计算,初始化就是第一列喽,假设什么组合都行。

代码如下:

暴力代码:

#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>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 450 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
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 int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int w[10], h[10];
int ans = 0, cnt[10][10];

void dfs(int r, int c, int mx){
    if(c > m){
        ++ans;  return ;
    }
    int mm = Min(n, n-h[c]+r);

    for(int i = Max(mx+1, cnt[r][c-1]); i <= mm; ++i){
            cnt[r][c] = i;
            if(r == h[c])  dfs(1, c+1, 0);
            else  dfs(r+1, c, i);
    }
}

int main(){
    int k;
    while(scanf("%d", &k) == 1){
        memset(h, 0, sizeof h);
        m = 0;
        for(int i = 1; i <= k; ++i){
            int x;
            scanf("%d", &x);
            m = Max(m, x);
            for(int j = 1; j <= x; ++j)
                ++h[j];
        }

        scanf("%d", &n);
        ans = 0;
        memset(cnt, 0, sizeof cnt);
        dfs(1, 1, 0);
        printf("%d
", ans);
    }
    return 0;
}

  

状压DP代码:

#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>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 8;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
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 int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}

int a[10];
int dp[maxn][1<<maxn];

inline int lowbit(int x){
    return x & (-x);
}

inline int bitcount(int i){//返回 i 这个数二进制数中有几个1
    int ans = 0;
    while(i) ++ans, i -= lowbit(i);
    return ans;
}

inline bool judge(int j, int k){//判断第 j 列是不是小于等于第 k 列
    int tj[10], tk[10];
    int cntj = 0, cntk = 0;
    for(int i = 0; i < m; ++i){
        if(j>>i&1) tj[cntj++] = i;
        if(k>>i&1) tk[cntk++] = i;
    }
    for(int i = 0; i < cntk; ++i)
        if(tj[i] > tk[i])   return false;
    return true;
}

int main(){
    while(scanf("%d", &n) == 1){
        memset(a, 0, sizeof a);
        int t = 0, x;
        for(int i = 0; i < n; ++i){
            scanf("%d", &x);
            t = Max(t, x);
            for(int j = 1; j <= x; ++j)
                ++a[j];
        }

        memset(dp, 0, sizeof dp);
        scanf("%d", &m);
        int len = 1<<m;
        for(int i = 0; i < len; ++i)//初始化第 1 列
            if(bitcount(i) == a[1])  dp[1][i] = 1;

        for(int i = 1; i < t; ++i){
            for(int j = 0; j < len; ++j){
                if(bitcount(j) != a[i] || !dp[i][j])  continue;

                for(int k = 0; k < len; ++k)
                    if(bitcount(k) == a[i+1] && judge(j, k))  dp[i+1][k] += dp[i][j];
            }
        }

        int ans = 0;
        for(int i = 0; i < len; ++i)  ans += dp[t][i];
        printf("%d
", ans);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/5793160.html