求助状压 dp

P2704 [NOI2001] 炮兵阵地

一只书虫仔 @ 2021-09-12 14:18:51

样例输出 13

#include <bits/stdc++.h>

using namespace std;

long long n, m;
bool a[102][12];
char S[12];
long long f[102][(1 << 10) + 2][(1 << 10) + 2];

long long dkw (long long n, long long k) {
    n >>= k;
    return n & 1;
}

bool inlaw (long long s1, long long s2, long long p, long long s) {
    p = m - p;
    if (dkw(s1, p) == 1) return false;
    if (dkw(s2, p) == 1) return false;
    if (dkw(s, p + 1) == 1) return false;
    if (dkw(s, p + 2) == 1) return false;
    return true;
} // s1 p, s2 p, s p - 1, s p - 2

void dfs (long long i, long long s1, long long s2, long long p, long long s) {
    if (p > m) {
        f[i + 1][s][s1] += f[i][s1][s2];
        return;
    }
    dfs(i, s1, s2, p + 1, s);
    if (a[i][p] && inlaw(s1, s2, p, s)) 
        dfs(i, s1, s2, p + 1, s | (1 << (m - p)));
}

int main () {
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%s", S);
        for (long long j = 0; j < (long long)(strlen(S)); j++)
            if (S[j] == 'P') a[i][j + 1] = 1;
            else a[i][j + 1] = 0;
    }
    f[0][0][0] = 1ll;
    for (long long i = 0; i < n; i++)
        for (long long s1 = 0; s1 < (1 << m); s1++)
            for (long long s2 = 0; s2 < (1 << m); s2++)
                dfs(i, s1, s2, 1, 0);
    long long Max = 0;
    for (long long s1 = 0; s1 < (1 << m); s1++)
        for (long long s2 = 0; s2 < (1 << m); s2++)
            Max = max(Max, f[n][s1][s2]);
//  for (long long s1 = 0; s1 < (1 << m); s1++) {
//      for (long long s2 = 0; s2 < (1 << m); s2++)
//          printf("%lld ", f[n][s1][s2]);
//      puts("");
//  }
    printf("%lld", Max);
    return 0;
}

by ftiasch @ 2021-09-12 17:53:57

红名爷为什么连自己调试都不会了?


by 一只书虫仔 @ 2021-09-12 18:17:16

@ftiasch 叉姐别阴阳我啊 /kel

正经话:自己试着调过了,输出了也看了若干遍程序,然后还是看不出来哪里有问题


by ftiasch @ 2021-09-12 18:42:16

@一只书虫仔 如果我没有看错,你 s1s2 的下标范围是 [0, m),但是 a[i][j]j 的范围是 [1, m]. 感觉肯定有谁不太对


by 一只书虫仔 @ 2021-09-12 22:04:25

@ftiasch 那个,a_{i,j} 对应的好像是 ps_1,s_2 那里我用的下标都是 m-pm-p 的范围是 [0,m) 的,p 的范围是 [1,m]


by ftiasch @ 2021-09-12 22:33:01

@一只书虫仔 你的 p 传进去是 1 开始的欸,inlaw 里面也是用的 p,这难道不是值域没对上?没人去访问 s 的第 0 位耶(?)


by 一只书虫仔 @ 2021-09-13 18:21:40

@ftiasch inlaw 里面不是有 p = m - p 吗 /yiw 我这么写了两道题都没问题啊 /kel


by ftiasch @ 2021-09-13 18:36:06

@一只书虫仔 那不好意思,我度数可能深了


by _l_l_l_l_l_ @ 2021-09-19 21:49:06

请问dfs是干什么的?


by 一只书虫仔 @ 2021-09-19 22:04:38

@WenZKbb 用于判断是否能够主动转移的


|