用二位的dp做为什么不对,求助

P2704 [NOI2001] 炮兵阵地

Man_CCNU @ 2022-10-12 21:04:42

c++
#include<iostream>
#include<vector>

using namespace std;

const int N = 1e3 + 10;

int f[N][N], g[N],cnt[1<<10], n, m, res;
vector<int>state;
vector<int>h[1 << 10];

bool check(int x)
{
    for(int i=0;i<m;i++){
        if((x>>i&1)&&((x>>i+1&1)||(x>>i+2&1))){
            return false;
        }
    }

    return true;
}
int getS(int x)
{
    int res = 0;
    while (x) {
        if (x & 1) {
            res = res + 1;
        }
        x = x >> 1;
    }

    return res;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char a;
            cin >> a;
            if (a == 'H') {
                g[i] += 1 << (m - j);
            }
        }
    }
    for (int i = 0; i < 1 << m; i++) {
        if (check(i)) {
            state.push_back(i);
            cnt[i] = getS(i);
        }
    }
    for (int i = 0; i < state.size(); i++) {
        for (int j = 0; j < state.size(); j++) {
            int a = state[i];
            int b = state[j];
            if ((a & b) == 0) {
                h[a].push_back(b);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < state.size(); j++) {
            int a = state[j];
            if (a & g[i]) continue;
            f[i][a]=cnt[a];
            for (int k = 0; k < h[a].size(); k++) {
                int b = h[a][k];
                if (b & g[i - 1]) {
                    continue;
                }
                if (i <= 2) {
                    f[i][a] = max(f[i][a], f[i - 1][b] + cnt[a]);
                }
                for (int p = 0; p < h[b].size()&&i>2; p++) {
                    int c = h[b][p];
                    if (c & g[i - 2]) {
                        continue;
                    }
                    if (!(a & c)) {
                        f[i][a] = max(f[i][a], f[i-2][c] + cnt[b] + cnt[a]);
                    }
                }
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<state.size();j++){
            res=max(res,f[i][state[j]]);
        }
    }
    cout << res << endl;

    return 0;
}

|