AtCoder Beginner Contest 113 D Number of Amidakuji

Number of Amidakuji

思路:dp

dp[i][j]表示经过(i, j) 这个点的方案数

然后一层一层地转移, 对于某一层, 用二进制枚举这一层的连接情况,

判断连接是否符合题意, 然后再进行转移

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 105, M = 8;
const int MOD = 1e9 + 7;
int dp[N][M];
int main() {
    int h, w, k;
    scanf("%d %d %d", &h, &w, &k);
    if(w == 1) return 0*puts("1");
    dp[0][0] = 1;
    for (int i = 1; i <= h; i++) {
        for (int k = 0; k < 1<<(w-1); k++) {
            bool f = true;
            for (int j = 0; j < (w-2); j++) {
                if((k&(1<<j)) && (k&(1<<j+1))) {
                    f = false;
                    break;
                }
            }
            if(f) {
                for (int j = 0; j < w; j++) {
                    if(j >= 1 && (k&(1<<j-1))) {
                        dp[i][j] = (dp[i][j] + dp[i-1][j-1]) % MOD;
                    }
                    else if(j < w-1 && (k&(1<<j))) {
                        dp[i][j] = (dp[i][j] + dp[i-1][j+1]) % MOD;
                    }
                    else {
                        dp[i][j] = (dp[i][j] + dp[i-1][j]) % MOD;
                    }
                }
            }
        }
    }
    printf("%d\n", dp[h][k-1]);
    return 0;
}

 

posted @ 2018-11-12 11:27  Wisdom+.+  阅读(368)  评论(0编辑  收藏  举报