【BZOJ5248】一双木棋(多省联考2018)-状压DP/轮廓线DP

测试地址:一双木棋
做法:本题需要用到状压DP/轮廓线DP。
注意到决策仅仅和当前的局面(即轮廓线)有关,而和之前的具体决策无关,因此我们令 f ( s t a t e ) 为轮廓线状态为 s t a t e 时,当前要下的棋手能获得的和对手的最大分数差是多少。
观察到无论何时,轮廓线的形态从下到上看,都是要么向上走,要么向右走,也就是一种单调的形态。因此我们获得了一种表示轮廓线状态的方式:从下往上看,向上走就在该位填 0 ,向右走就在该位填 1 。因为只会走 n + m 次,所以状态最多有 2 n + m 个,可以接受。
那么状态转移方程就很显然了,对于所有可能转移到的状态 n e x t ,设要转移到这一个状态需要下棋的位置为 ( x , y ) ,有:
f ( s t a t e ) = max ( v a l ( x , y ) f ( n e x t ) )
其中 v a l ( x , y ) 为当前棋手在 ( x , y ) 下棋能获得的分数,显然 s t a t e = ( 0...01...1 ) 2 f ( s t a t e ) 为答案。这样我们就可以通过记忆化搜索来完成这一题,时间复杂度为 O ( 2 n + m )
以下是本人代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf=(ll)1000000000*(ll)1000000000;
int n,m;
ll a[11][11],b[11][11],f[2000010]={0};
bool vis[2000010]={0};

ll dp(int st,bool type)
{
    if (vis[st]) return f[st];
    vis[st]=1;
    int x=n+1,y=1,last=-1,i=0,now=st;
    f[st]=-inf;
    while(now)
    {
        if (now&1)
        {
            if (last==0)
                f[st]=max(f[st],(type?a[x][y]:b[x][y])-dp(st-(1<<i)+(1<<(i-1)),!type));
            y++;
            last=1;
        }
        else x--,last=0;
        i++;now>>=1;
    }
    if (f[st]==-inf) f[st]=0;
    return f[st];
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%lld",&a[i][j]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%lld",&b[i][j]);

    int start=0;
    for(int i=1;i<=m;i++)
        start+=(1<<(n+i-1));
    dp(start,1);
    printf("%lld",f[start]);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Maxwei_wzj/article/details/80065279
今日推荐