hdu 1693 插头dp

       在n*m的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法。 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t,a[13][13],n,m;
ll dp[13][13][1<<13];
int main()
{
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a[i][j]);
            }
        memset(dp,0,sizeof dp);
        dp[0][m][0]=1ll;
        for(int i=1;i<=n;i++)
        {
            for(int s=0;s<(1<<m);s++)
                dp[i][0][s<<1]=dp[i-1][m][s];
            for(int j=1;j<=m;j++)
            {
                for(int s=0;s<(1<<(m+1));s++)
                {
                    int x=(1<<j);
                    int y=x>>1;
                    if(a[i][j])
                    {
                        if((s&x)&&(s&y))
                            dp[i][j][s]=dp[i][j-1][s^x^y];
                        else if((s&x)||(s&y))
                            dp[i][j][s]=dp[i][j-1][s]+dp[i][j-1][s^x^y];
                        else dp[i][j][s]=dp[i][j-1][s^x^y];
                    }
                    else
                    {
                        if((s&x)==0&&(s&y)==0)
                            dp[i][j][s]=dp[i][j-1][s];
                        else dp[i][j][s]=0;
                    }
                }
            }
        }
        printf("Case %d: There are %lld ways to eat the trees.\n",++cas,dp[n][m][0]);
    }
    return 0;
} 

太菜了,不知道怎样把此代码改成单回路的,期待大佬指点。



猜你喜欢

转载自blog.csdn.net/dllpxfire/article/details/80300697