80tps,求助

P2704 [NOI2001] 炮兵阵地

yan044 @ 2022-06-09 20:59:45

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

int n,m,dp[105][2200][2200];//第i行  这行的状态  i-1行状态
int a[105],b[2200],cnt;
char p[105][13];

int work(int x)
{
    int qwq = 0;
    while(x)
    {
        if (x & 1)
            qwq ++;
        x = x >> 1;
    }
    return qwq;
}

bool judge1(int x)
{
    if ((x & (x << 1)) || (x & (x << 2)))
        return 0;
    else 
        return 1;
}

bool judge2(int i,int x)
{
    if (a[i] & b[x])
    {
        return 0;
    }
    else 
        return 1;
}

int main()
{
    cin >> n >> m;
    //存图,转2进制
    for (int i = 1 ; i <= n ; i++)
    {
        for (int j = 1 ; j <= m ; j++)
        {
            cin >> p[i][j];
            if (p[i][j] == 'P')
                a[i] = (a[i] << 1) + 0;
            else 
                a[i] = (a[i] << 1) + 1;
        }
    }
    //初始化1001001001001这样的状态
    for (int sta = 0 ; sta < (1 << m) ; sta++)
    {
        if (judge1(sta))
        {
            b[cnt] = sta;
            cnt++;
        }
    }
    for (int i = 0 ; i < cnt ; i++)
    {
        if (judge2(1,i));
            dp[1][i][0] = work(b[i]);
    }
    for (int i = 0 ; i < cnt ; i++)
    {
        if (judge2(2,i))
        {
            for (int j = 0 ; j < cnt ; j++)
            {
                if (judge2(1,i) && !(b[i] & b[j]))
                    dp[2][i][j] = work(b[i]) + work(b[j]);
            }
        }
    }
    for (int i = 3 ; i <= n ; i++)
    {
        for (int j = 0 ; j < cnt ; j++)//第i行
        {
            if (judge2(i,j))
            {
                for (int l = 0 ; l < cnt ; l++)//i - 1
                {
                    if (judge2(i - 1,l) && !(b[j] & b[l]))
                    {
                        for (int r = 0 ; r < cnt ; r++)//i - 2
                        {
                            if (judge2(i - 2,r) && !(b[j] & b[r]) && !(b[r] & b[l]))
                            {
                                dp[i][j][l] = max(dp[i][j][l],dp[i - 1][l][r] + work(b[j]));
                            }
                        }
                    }
                }
            }
        }
    }
    int ans = 0;
    for (int i = 0 ; i < cnt ; i++)
    {
        for (int j = 0 ; j < cnt ; j++)
        {
            ans = max(ans,dp[n][i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}

|