AtCoder Beginner Contest 159 E - Dividing Chocolate [enumeration]

Topic Link
Data range:
\ (1≤H≤10 \)
\ (1≤W≤1000 \)
\ (1≤K≤H × W is \)

analysis:

First observation data, found that the number of rows are very small, then we can hold the gold points system for each of the points system to determine the number of divisions column, taking a minimum.
And first two-dimensional prefix, the entire pretreatment FIG.
Complexity: \ (O (2 ^ * H * W is H) \)

Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1010;
char s[15][N];
int num[15][N];
int main()
{
    int h,w,k,ans=0;
    scanf("%d%d%d",&h,&w,&k);
    for(int i=1;i<=h;i++)
        scanf("%s",s[i]+1);
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
            num[i][j]=num[i-1][j]+num[i][j-1]-num[i-1][j-1]+s[i][j]-'0';
    }
    if(h==1)
    {
        int st=0;
        for(int i=1;i<=w;i++)
        {
            if(num[1][i]-num[1][st]==k&&i<w)
            {
                ans++;
                st=i;
            }
            printf("%d\n",ans);
            return 0;
        }
    }
    ans=h*w;
    for(int i=0;i<(1<<(h-1));i++)//行的划分种类数
    {
        int c=0,r;
        int res=__builtin_popcount(i);
        bool g=0;
        for(int j=1;j<=w;j++)//枚举列
        {
            bool f=0;
            r=0;
            for(int u=1;u<=h;u++)
            {
                if((1&(i>>(u-1)))||u==h)
                {
                    int t=num[u][j]+num[r][c]-num[r][j]-num[u][c];
                    r=u;
                    if(t>k)
                        f=1;
                }
            }
            if(f&&j==c+1)
            {
                g=1;
                break;
            }
            if(f)
            {
                res++;
                c=j-1;
            }
        }
        if(!g)
            ans=min(res,ans);
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/1024-xzx/p/12554604.html