zoukankan      html  css  js  c++  java
  • LA4794 分享巧克力

    Sharing Chocolate

    Chocolate in its many forms is enjoyed by millions of people around the world every day. It is a truly universal candy available in virtually every country around the world.

    You find that the only thing better than eating chocolate is to share it with friends. Unfortunately your friends are very picky and have different appetites: some would like more and others less of the chocolate that you offer them. You have found it increasingly difficult to determine whether their demands can be met. It is time to writte a program that solves the problem once and for all!


    Your chocolate comes as a rectangular bar. The bar consists of same-sized rectangular pieces. To share the chocolate you may break one bar into two pieces along a division between rows or columns of the bar. You or the may then repeatedly break the resulting pieces in the same manner. Each of your friends insists on a getting a single rectangular portion of the chocolate that has a specified number of pieces. You are a little bit insistent as well: you will break up your bar only if all of it can be distributed to your friends, with none left over.


    For exampla, Figure 9 shows one way that a chocolate bar consisting of 3 x 4 pieces can be split into 4 parts that contain 6, 3, 2, and 1 pieces respectively, by breanking it 3 times (This corresponds to the first sample input.)

    Input 

    The input consists of multiple test cases each describing a chocolate bar to share. Each description starts with a line containing a single integer n (1<=n<=15), the number of parts in which the bar is supposed to be split. This is followed by a line containing two integers x and y(1<=xy<=100), the dimensions of the chocolate bar. The next line contains n positive integers, giving the number of pieces that are supposed to be in each of the n parts.

    The input is terminated by a line containing the integer zero.

    Output 

    For each test case, first display its case number. Then display whether it is possible to break the chocolate in the desired way: display ``Yes" if it is possible, and ``No" otherwise. Follow the format of the sample output.

    Sample Input 


    3 4 
    6 3 2 1 

    2 3 
    1 5 0

    Sample Output 

    Case 1: Yes
    Case 2: No

    题意: 将一个x*y大小的巧克力分成n分, 固定大小a[i], 判断是否可以成功.

    解题思路:
    1. 设状态dp[x][y][S]: x*y的巧克力是否可以分割成集合S.
    2. dp[x][y][S]=true 当且仅当:
      dp[x0][y][S0] = dp[x-x0][y][S-S0] = true //1<=x0<x,S0是S的子集
    OR dp[x][y0][S0] = dp[x][y-y0][S-S0] = true //1<=y0<y,S0是S的子集
    3.把集合S中面积的总和记为sum[S]
    4.只计算sum(S)=x*y的
    5.dp[x][y][S] = dp[y][x][S],不必重复计算
    我们设x<y,用dp[x][S]代替dp[x][y][S]和dp[y][x][S]
    此时x为较小的边长,y为sum(S)/x.
    方程变化为:
    dp[x][S]=true 当且仅当:   
        sum[S0]%x==0 AND dp[min(x,sum[S]/x)][S0] = dp[min(x,sum[S]/x)][S-S0] = true
    或者是 sum[S0]%y==0 AND
    dp[min(y,sum[S]/y)][S0] = dp[min(y,sum[S]/y)][S-S0] = true
    6.输入之后要比较所有所有a[i]的和是否等于x*y

    代码如下:
    #include<iostream>
    #include<cstring>
    #define Max_n 15
    #define Size 105
    using namespace std;
    
    bool d[Size][(1<<Max_n)]; 
    bool vis[Size][(1<<Max_n)];
    int sum[(1<<Max_n)];
    int a[Max_n];
    int n,x,y;
    
    
    int bitcount(int S){
        int num=0;
        for(int i=0;i<n;i++)
            if(S&(1<<i))num++;
        return num;
    }
    
    bool dp(int x,int S){
        if(vis[x][S])return d[x][S];
        
        vis[x][S]=true;
        bool& ans=d[x][S];
        
        if(bitcount(S)==1)return ans=true;
         
        int y=sum[S]/x;
        for(int S0=(S-1)&S;S0;S0=(S0-1)&S){
            int S1=S-S0;
            if(sum[S0]%x==0 && dp(min(x,sum[S0]/x),S0) && dp(min(x,sum[S1]/x),S1)) return ans=true;
            if(sum[S0]%y==0 && dp(min(y,sum[S0]/y),S0) && dp(min(y,sum[S1]/y),S1)) return ans=true;
        }
        
        return ans=false;
    }
    
    int main(){
        freopen("32.in","r",stdin); 
        
        while(scanf("%d",&n)==1 && n!=0){
            memset(vis,false,sizeof(vis));
            
            cin>>x>>y;
            for(int i=0;i<n;i++){
                cin>>a[i];
            }
        
            for(int S=0;S<(1<<n);S++){
                for(int j=0;j<n;j++){
                    if(S&(1<<j)){
                        sum[S]+=a[j];
                    }
                }
            }
        
            int ALL=(1<<n)-1;
            int ans;
            if(sum[ALL]!=x*y)ans=0;
            else ans=dp(min(x,y),ALL);
        
            cout<<(ans?"Yes":"No")<<endl;
        }
        
        fclose(stdin);
        return 0;
    }
  • 相关阅读:
    Winform学习笔记
    ASP.NET后台注册javascript脚本方法
    使用MultipleActiveResultSets复用Sql Server 2008数据库连接
    angular 2 新建组件命令
    一个关于日志操作方法
    vs2017 打开包管理器 (程序包管理控制台)
    Asp.Net Core Identity 怎么获取当前登录的用户信息?
    abp 实现所有审计的接口
    IIS8.5 布署 Asp.Net Core 老是报500的错误怎么办?
    .NET Core 1.1布署后出现“HTTP Error 502.5 Process Failure”的解决办法
  • 原文地址:https://www.cnblogs.com/FuTaimeng/p/5422557.html
Copyright © 2011-2022 走看看