📜  剪成最少正方形的纸| 2套

📅  最后修改于: 2021-09-17 07:22:33             🧑  作者: Mango

给定一张大小为 A x B 的纸。任务是将纸剪成任意大小的正方形。找出可以从纸上剪下的最少正方形数。

例子:

Input  : 36 x 30
Output : 5
Explanation : 
3 (squares of size 12x12) + 
2 (squares of size 18x18)

Input  : 4 x 5
Output : 5
Explanation : 
1 (squares of size 4x4) + 
4 (squares of size 1x1)

提问:谷歌

我们在上一篇文章中已经讨论了解决这个问题的贪心方法。但以前的方法并不总是有效。例如,上面的第一个测试用例失败了。因此,在本文中,我们使用动态规划来解决这个问题。

我们知道,如果我们想从纸上切出最少数量的正方形,那么我们必须首先从纸上切出最大的正方形,并且最大的正方形将与纸的较小边具有相同的边。例如,如果纸张的尺寸为 13 x 29,则最大正方形将是边 13。因此我们可以切割 2 个尺寸为 13 x 13 (29/13 = 2) 的正方形。现在剩余的纸将有 3 x 13 的尺寸。类似地,我们可以使用 4 个 3 x 3 的正方形和 3 个 1 x 1 的正方形切割剩余的纸。因此,可以从 13 x 29 尺寸的纸上切割至少 9 个正方形。

挖1

说明
minimumSquare 是一个函数,它试图在某个位置分割矩形。两个部分都递归调用该函数。尝试所有可能的拆分并采用结果最小的拆分。基本情况是当两边相等时,即输入已经是一个正方形,在这种情况下,结果是我们试图找到离中心最近的切口,这将导致我们得到最小结果。

假设我们有一个矩形,宽度为 N,高度为 M。

  • if (N == M),所以它是一个正方形,不需要做任何事情。
  • 否则,我们可以将矩形分成另外两个较小的 (N – x, M) 和 (x, M),因此可以递归求解。
  • 同理,我们也可以将其分为(N, M – x)和(N, x)

我们还需要注意这里的边缘情况,即 N=11 和 M=13,反之亦然。以下将是给定测试用例的最佳答案:

对于这种情况,我们的方法将返回 8,但正如您所看到的,我们可以将纸张切成 6 个正方形,这是最小的。发生这种情况是因为在最佳解决方案中没有穿过整个正方形的垂直或水平线。

下面是使用动态规划实现上述想法。

C++
// C++ program to find minimum number of squares
// to cut a paper using Dynamic Programming
#include
using namespace std;
 
const int MAX = 300;
int dp[MAX][MAX];
 
// Returns min number of squares needed
int minimumSquare(int m, int n)
{
    // Initializing max values to vertical_min
    // and horizontal_min
    int vertical_min = INT_MAX;
    int horizontal_min = INT_MAX;
   
    // N=11 & M=13 is a special case
    if(n==13 && m==11) return 6;
    if(m==13 && n==11) return 6;
     
    // If the given rectangle is already a square
    if (m == n)
        return 1;
     
    // If the answer for the given rectangle is
    // previously calculated return that answer
    if (dp[m][n])
            return dp[m][n];
     
    /* The rectangle is cut horizontally and
       vertically into two parts and the cut
       with minimum value is found for every
       recursive call.
    */
     
    for (int i = 1;i<= m/2;i++)
    {
        // Calculating the minimum answer for the
        // rectangles with width equal to n and length
        // less than m for finding the cut point for
        // the minimum answer
        horizontal_min = min(minimumSquare(i, n) +
                minimumSquare(m-i, n), horizontal_min);
    }
     
    for (int j = 1;j<= n/2;j++)
    {
        // Calculating the minimum answer for the
        // rectangles with width less than n and
        // length equal to m for finding the cut
        // point for the minimum answer
        vertical_min = min(minimumSquare(m, j) +
                minimumSquare(m, n-j), vertical_min);
    }
         
    // Minimum of the vertical cut or horizontal
    // cut to form a square is the answer
    dp[m][n] = min(vertical_min, horizontal_min);
         
    return dp[m][n];
}
 
// Driver code
int main()
{
    int m = 30, n = 35;
   
    // Function call
    cout << minimumSquare(m, n);
    return 0;
}
 
// This code is contributed by Utkarsh Gupta


Java
// Java program to find minimum number of
// squares to cut a paper using Dynamic
// Programming
import java.io.*;
 
class GFG {
 
    static int dp[][] = new int[300][300];
 
    // Returns min number of squares needed
    static int minimumSquare(int m, int n)
    {
        // Initializing max values to
        // vertical_min and horizontal_min
        int vertical_min = Integer.MAX_VALUE;
        int horizontal_min = Integer.MAX_VALUE;
       
        // N=11 & M=13 is a special case
        if(n==13 && m==11) return 6;
        if(m==13 && n==11) return 6;
 
        // If the given rectangle is
        // already a square
        if (m == n)
            return 1;
 
        // If the answer for the given
        // rectangle is previously
        // calculated return that answer
        if (dp[m][n] != 0)
            return dp[m][n];
 
        /* The rectangle is cut horizontally
        and vertically into two parts and
        the cut with minimum value is found
        for every recursive call.
        */
 
        for (int i = 1; i <= m / 2; i++)
        {
            // Calculating the minimum answer
            // for the rectangles with width
            // equal to n and length less than
            // m for finding the cut point for
            // the minimum answer
            horizontal_min
                = Math.min(minimumSquare(i, n)
                           + minimumSquare(m - i, n),
                           horizontal_min);
        }
 
        for (int j = 1; j <= n / 2; j++) {
            // Calculating the minimum answer
            // for the rectangles with width
            // less than n and length equal to
            // m for finding the cut point for
            // the minimum answer
            vertical_min
                = Math.min(minimumSquare(m, j)
                           + minimumSquare(m, n - j),
                           vertical_min);
        }
 
        // Minimum of the vertical cut or
        // horizontal cut to form a square
        // is the answer
        dp[m][n] = Math.min(vertical_min, horizontal_min);
 
        return dp[m][n];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int m = 30, n = 35;
       
        // Function call
        System.out.println(minimumSquare(m, n));
    }
}
// This code is contributed by Prerna Saini


Python3
# Python3 program to find minimum
# number of squares
# to cut a paper using Dynamic Programming
 
MAX = 300
dp = [[0 for i in range(MAX)] for i in range(MAX)]
 
# Returns min number of squares needed
 
 
def minimumSquare(m, n):
 
    # Initializing max values to
    # vertical_min
    # and horizontal_min
    vertical_min = 10000000000
    horizontal_min = 10000000000
 
    # N=11 & M=13 is a special case
    if n == 13 and m == 11:
        return 6
    if m == 13 and n == 11:
        return 6
 
    # If the given rectangle is
    # already a square
    if m == n:
        return 1
 
    # If the answer for the given rectangle is
    # previously calculated return that answer
    if dp[m][n] != 0:
        return dp[m][n]
 
    # The rectangle is cut horizontally and
    # vertically into two parts and the cut
    # with minimum value is found for every
    # recursive call.
    for i in range(1, m//2+1):
 
        # Calculating the minimum answer for the
        # rectangles with width equal to n and length
        # less than m for finding the cut point for
        # the minimum answer
        horizontal_min = min(minimumSquare(i, n) +
                             minimumSquare(m-i, n), horizontal_min)
    for j in range(1, n//2+1):
 
        # Calculating the minimum answer for the
        # rectangles with width equal to n and length
        # less than m for finding the cut point for
        # the minimum answer
        vertical_min = min(minimumSquare(m, j) +
                           minimumSquare(m, n-j), vertical_min)
 
    # Minimum of the vertical cut or horizontal
    # cut to form a square is the answer
    dp[m][n] = min(vertical_min, horizontal_min)
    return dp[m][n]
 
 
# Driver code
if __name__ == '__main__':
    m = 30
    n = 35
 
    # Function call
    print(minimumSquare(m, n))
 
# This code is contributed by sahilshelangia


C#
// C# program to find minimum number of
// squares to cut a paper using Dynamic
// Programming
using System;
 
class GFG {
 
    static int[, ] dp = new int[300, 300];
 
    // Returns min number of squares needed
    static int minimumSquare(int m, int n)
    {
        // Initializing max values to
        // vertical_min and horizontal_min
        int vertical_min = int.MaxValue;
        int horizontal_min = int.MaxValue;
       
        // N=11 & M=13 is a special case
        if(n==13 && m==11) return 6;
        if(m==13 && n==11) return 6;
 
        // If the given rectangle is
        // already a square
        if (m == n)
            return 1;
 
        // If the answer for the given
        // rectangle is previously
        // calculated return that answer
        if (dp[m, n] != 0)
            return dp[m, n];
 
        /* The rectangle is cut horizontally
        and vertically into two parts and
        the cut with minimum value is found
        for every recursive call.
        */
 
        for (int i = 1; i <= m / 2; i++)
        {
            // Calculating the minimum answer
            // for the rectangles with width
            // equal to n and length less than
            // m for finding the cut point for
            // the minimum answer
            horizontal_min
                = Math.Min(minimumSquare(i, n)
                               + minimumSquare(m - i, n),
                           horizontal_min);
        }
 
        for (int j = 1; j <= n / 2; j++)
        {
            // Calculating the minimum answer
            // for the rectangles with width
            // less than n and length equal to
            // m for finding the cut point for
            // the minimum answer
            vertical_min
                = Math.Min(minimumSquare(m, j)
                               + minimumSquare(m, n - j),
                           vertical_min);
        }
 
        // Minimum of the vertical cut or
        // horizontal cut to form a square
        // is the answer
        dp[m, n] = Math.Min(vertical_min, horizontal_min);
 
        return dp[m, n];
    }
 
    // Driver code
    public static void Main()
    {
        int m = 30, n = 35;
 
        // Function call
        Console.WriteLine(minimumSquare(m, n));
    }
}
 
// This code is contributed by anuj_67.


Javascript


输出
5

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程