Sgu 132 状压dp(gtmd128)

辣鸡题卡常,改了以后又zz地把数组开成了100,debug到死都没有发现。一题做了一天多,沃日。

代码:

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int m, n;
char str[10];
int g[80];
int dp[2][200][200];
int tofix1, tofix2;
void dfs(int line, int flor, int tar1, int tar2, int cnt)
{
    if(flor>1&&!(tofix2&(1<<(flor-2)))&&!(tar2&(1<<(flor-2)))){/*printf("sb1 %d %d %d\n",line, tar2, tofix2);*/return;}
    if(flor>2&&!(tar2&(1<<(flor-2)))&&!(tar2&(1<<(flor-3)))){/*printf("sb2 %d %d\n", line, tar2);*/return;}
    if(flor==n+1)
    {
        //printf("%d %d %d\n", tar1, tar2, line);
        dp[line%2][tar1][tar2]=min(dp[line%2][tar1][tar2], dp[(line+1)%2][tofix1][tofix2]+cnt);
        //printf("%d %d %d\n", cnt, dp[line%2][tar1][tar2], dp[(line+1)%2][tofix1][tofix2]);
        return;
    }
    dfs(line, flor+1, tar1, tar2, cnt);
    if(flor<n&&!(tar1&(1<<(flor-1)))&&!(tar1&(1<<(flor))))
        dfs(line, flor+1, tar1|(1<<(flor-1))|(1<<flor), tar2, cnt+1);
    if(!(tar1&(1<<(flor-1)))&&!(tar2&(1<<(flor-1))))
        dfs(line, flor+1, tar1|(1<<(flor-1)), tar2|(1<<(flor-1)), cnt+1);
}
int main()
{
    memset(dp, 0x3f3f3f3f, sizeof(dp));
    scanf("%d%d", &m, &n);
    for(int i=1;i<=m;i++)
    {
        g[i]=0;
        scanf("%s", str);
        for(int j=0;j<n;j++)
        {
            g[i]<<=1;
            g[i]|=(str[j]=='*');
        }
        //printf("%d\n", g[i]);
    }
    g[m+1]=(1<<n)-1;
    dp[0][(1<<n)-1][(1<<n)-1]=0;
    for(int i=1;i<=m+1;i++)
    {
        for(int j=0;j<(1<<n);j++)
            for(int k=0;k<(1<<n);k++)
            {
                if(dp[(i+1)%2][j][k]!=INF)
                {
                    tofix1=j;
                    tofix2=k;
                    //printf("%d %d %d\n", i, j, k);
                    dfs(i, 1, g[i], j, 0);
                }
            }
        memset(dp[(i+1)%2], 0x3f3f3f3f, sizeof(dp[(i+1)%2]));
    }
    int M=INF;
    for(int j=0;j<(1<<n);j++)M=min(M, dp[(m+1)%2][(1<<n)-1][j]);
    printf("%d\n", M);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值