zoukankan      html  css  js  c++  java
  • UVa 10970 Big Chocolate

    Big Chocolate

    Mohammad has recently visited Switzerland. As he loves his friends very much, he decided to buy some chocolate for them, but as this fine chocolate is very expensive(You know Mohammad is a little BIT stingy!), he could only afford buying one chocolate, albeit a very big one (part of it can be seen in figure 1) for all of them as a souvenir. Now, he wants to give each of his friends exactly one part of this chocolate and as he believes all human beings are equal (!), he wants to split it into equal parts.

    The chocolate is an  rectangle constructed from  unit-sized squares. You can assume that Mohammad has also  friends waiting to receive their piece of chocolate.

    To split the chocolate, Mohammad can cut it in vertical or horizontal direction (through the lines that separate the squares). Then, he should do the same with each part separately until he reaches  unit size pieces of chocolate. Unfortunately, because he is a little lazy, he wants to use the minimum number of cuts required to accomplish this task.

    Your goal is to tell him the minimum number of cuts needed to split all of the chocolate squares apart.

     

    Figure 1. Mohammad’s chocolate

     

    The Input

    The input consists of several test cases. In each line of input, there are two integers , the number of rows in the chocolate and , the number of columns in the chocolate. The input should be processed until end of file is encountered.

    The Output

    For each line of input, your program should produce one line of output containing an integer indicating the minimum number of cuts needed to split the entire chocolate into unit size pieces.

    Sample Input

    2 2

    1 1

    1 5

     

    Sample Output

    3

    0

    4


    Amirkabir University of Technology - Local Contest - Round #2

    给一大块巧克力,问最少多少刀能把它切成小块

    最初用记忆化的递归写了一个,代码如下,很好理解

     1 #include<iostream>
     2 #include<cstdio>
     3 #define INF 0x7fffffffffffffff
     4 
     5 using namespace std;
     6 
     7 long long dp[500][500];
     8 
     9 long long f(int p,int q)
    10 {
    11     if(dp[p][q]!=-1)
    12         return dp[p][q];
    13 
    14     if(p==1)
    15         return dp[p][q]=q-1;
    16 
    17     if(q==1)
    18         return dp[p][q]=p-1;
    19 
    20     long long Min=INF;
    21     for(int k=1;k<p;k++)
    22     {
    23         long long temp=f(k,q)+f(p-k,q)+1;
    24         if(temp<Min)
    25             Min=temp;
    26     }
    27     for(int k=1;k<q;k++)
    28     {
    29         long long temp=f(p,k)+f(p,q-k)+1;
    30         if(temp<Min)
    31             Min=temp;
    32     }
    33 
    34     return dp[p][q]=Min;
    35 }
    36 
    37 int main()
    38 {
    39     int x,y;
    40 
    41     for(int i=0;i<500;i++)
    42         for(int j=0;j<500;j++)
    43             dp[i][j]=-1;
    44 
    45     while(scanf("%d %d",&x,&y)==2)
    46     {
    47         printf("%lld
    ",f(x,y));
    48     }
    49 
    50     return 0;
    51 }
    [C++]

    后来看别人的代码,发现其实不论怎么切,都要切n*m-1刀

    先(n-1)刀把巧克力切成n条,每条m小块,再对n条每条切(m-1)刀,全部分成小块,这样一共是n-1+n*(m-1)=n*m-1刀

    新代码很短~~~~

     1 #include<iostream>
     2 #include<cstdio>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     int m,n;
     9 
    10     while(scanf("%d %d",&m,&n)==2)
    11         printf("%d
    ",m*n-1);
    12 
    13     return 0;
    14 }
    [C++]
  • 相关阅读:
    [Intellij] 软件设置和常用快捷键
    [Intellij] Project Structure 配置说明
    [日志log] 常用log日志记录方式对比和详解
    [J2EE]web.xml各个版本模板
    [技术选型] CDH-Cloudera Distribution Hadoop
    [技术选型] dubbo
    [技术选型] spring boot
    [hbase] HBase内置过滤器的一些总结
    [zookeeper] Zookeeper伪分布式集群配置
    [maven] settings 文件 本地maven仓库
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3531687.html
Copyright © 2011-2022 走看看