Paper Cut Into Ten Pieces Again and Gain

Given a paper of size A x B. Task is to cut the paper into squares of whatever size. Find the minimum number of squares that can be cut from the paper.

Examples:

Input  : 36 10 30 Output : five Explanation :  3 (squares of size 12x12) +  2 (squares of size 18x18)  Input  : 4 x 5 Output : v Explanation :  1 (squares of size 4x4) +  4 (squares of size 1x1)

Asked in : Google

We have already discussed the Greedy arroyo to solve this problem in previous article. But the previous approach doesn't always work. For example, it fails for the higher up showtime test case. And so, in this commodity nosotros solve this trouble using Dynamic Programming.

Nosotros know that if nosotros desire to cutting minimum number of squares from the paper then we would have to cutting largest square possible from the paper offset and largest square will have same side as smaller side of the paper. For example if paper accept the size 13 x 29, then maximum square will exist of side 13. then we tin can cut 2 square of size thirteen ten 13 (29/xiii = ii). Now remaining paper will have size 3 x 13. Similarly we tin cut remaining paper by using 4 squares of size 3 x 3 and 3 squares of 1 ten one. And so minimum 9 squares can be cut from the Paper of size xiii x 29.

dig1

Caption:
minimumSquare is a function which tries to split the rectangle at some position. The function is called recursively for both parts. Try all possible splits and take the i with minimum result. The base instance is when both sides are equal i.e the input is already a foursquare, in which instance the result is We are trying to notice the cut which is nearest to the middle which will pb united states to our minimum result.

Assuming we have a rectangle with width is N and elevation is Thou.

  • if (Northward == M), so it is a square and zip need to exist washed.
  • Otherwise, nosotros can carve up the rectangle into two other smaller one (N – 10, M) and (x, G), and so it tin be solved recursively.
  • Similarly, nosotros can too divide information technology into (North, Yard – ten) and (Northward, x)

Also we need to be aware of an edge case here which is N=xi and M=xiii or vice versa. The following volition be the all-time possible answer for the given test case:

Our Approach volition return viii for this case but as y'all tin can run into we can cut the paper in half-dozen squares which is minimum. This happens because there is no vertical or horizontal line that cuts through the whole square in the optimum solution.

Beneath is the implementation of the above thought using Dynamic Programming.

C++

#include<bits/stdc++.h>

using namespace std;

const int MAX = 300;

int dp[MAX][MAX];

int minimumSquare( int one thousand, int n)

{

int vertical_min = INT_MAX;

int horizontal_min = INT_MAX;

if (n==13 && grand==11) render half-dozen;

if (chiliad==xiii && north==xi) render vi;

if (m == n)

return ane;

if (dp[m][n])

return dp[1000][due north];

for ( int i = 1;i<= m/ii;i++)

{

horizontal_min = min(minimumSquare(i, northward) +

minimumSquare(1000-i, due north), horizontal_min);

}

for ( int j = 1;j<= north/ii;j++)

{

vertical_min = min(minimumSquare(1000, j) +

minimumSquare(chiliad, n-j), vertical_min);

}

dp[m][n] = min(vertical_min, horizontal_min);

return dp[m][n];

}

int principal()

{

int m = thirty, north = 35;

cout << minimumSquare(grand, due north);

return 0;

}

Java

import java.io.*;

class GFG {

static int dp[][] = new int [ 300 ][ 300 ];

static int minimumSquare( int m, int north)

{

int vertical_min = Integer.MAX_VALUE;

int horizontal_min = Integer.MAX_VALUE;

if (due north== thirteen && m== 11 ) return 6 ;

if (k== thirteen && n== 11 ) return 6 ;

if (k == n)

return 1 ;

if (dp[m][n] != 0 )

render dp[chiliad][n];

for ( int i = 1 ; i <= thou / 2 ; i++)

{

horizontal_min

= Math.min(minimumSquare(i, north)

+ minimumSquare(m - i, northward),

horizontal_min);

}

for ( int j = 1 ; j <= n / two ; j++) {

vertical_min

= Math.min(minimumSquare(m, j)

+ minimumSquare(m, n - j),

vertical_min);

}

dp[m][north] = Math.min(vertical_min, horizontal_min);

return dp[m][due north];

}

public static void main(String[] args)

{

int one thousand = 30 , north = 35 ;

Organization.out.println(minimumSquare(m, due north));

}

}

Python3

MAX = 300

dp = [[ 0 for i in range ( MAX )] for i in range ( MAX )]

def minimumSquare(m, due north):

vertical_min = 10000000000

horizontal_min = 10000000000

if north = = 13 and grand = = eleven :

return 6

if m = = 13 and n = = 11 :

return 6

if m = = n:

return 1

if dp[m][n] ! = 0 :

render dp[m][n]

for i in range ( 1 , m / / ii + 1 ):

horizontal_min = min (minimumSquare(i, north) +

minimumSquare(m - i, due north), horizontal_min)

for j in range ( 1 , northward / / ii + 1 ):

vertical_min = min (minimumSquare(thou, j) +

minimumSquare(m, north - j), vertical_min)

dp[m][northward] = min (vertical_min, horizontal_min)

return dp[m][north]

if __name__ = = '__main__' :

thousand = 30

n = 35

print (minimumSquare(one thousand, n))

C#

using System;

grade GFG {

static int [, ] dp = new int [300, 300];

static int minimumSquare( int 1000, int n)

{

int vertical_min = int .MaxValue;

int horizontal_min = int .MaxValue;

if (n==13 && one thousand==11) render 6;

if (m==13 && due north==11) return 6;

if (m == n)

return ane;

if (dp[chiliad, n] != 0)

return dp[thou, n];

for ( int i = ane; i <= m / 2; i++)

{

horizontal_min

= Math.Min(minimumSquare(i, n)

+ minimumSquare(one thousand - i, n),

horizontal_min);

}

for ( int j = one; j <= n / 2; j++)

{

vertical_min

= Math.Min(minimumSquare(g, j)

+ minimumSquare(m, n - j),

vertical_min);

}

dp[1000, n] = Math.Min(vertical_min, horizontal_min);

return dp[m, northward];

}

public static void Master()

{

int m = 30, due north = 35;

Console.WriteLine(minimumSquare(m, n));

}

}

Javascript

<script>

let dp = new Array(300);

for (let i = 0; i < 300; i++)

{

dp[i] = new Assortment(300);

for (let j = 0; j < 300; j++)

{

dp[i][j] = 0;

}

}

function minimumSquare(m, n)

{

permit vertical_min = Number.MAX_VALUE;

let horizontal_min = Number.MAX_VALUE;

if (n==13 && k==11) return six;

if (g==thirteen && northward==11) return 6;

if (grand == n)

render 1;

if (dp[thou][due north] != 0)

return dp[one thousand][n];

for (let i = 1; i <= parseInt(m / ii, 10); i++)

{

horizontal_min

= Math.min(minimumSquare(i, n)

+ minimumSquare(one thousand - i, n),

horizontal_min);

}

for (let j = 1; j <= parseInt(n / ii, 10); j++) {

vertical_min

= Math.min(minimumSquare(m, j)

+ minimumSquare(m, n - j),

vertical_min);

}

dp[thousand][north] = Math.min(vertical_min, horizontal_min);

return dp[m][n];

}

let thousand = 30, n = 35;

document.write(minimumSquare(one thousand, due north));

</script>

This commodity is contributed by Ayush Govil , Aditya Nihal Kumar Singh and Deepanshu Aggarwal. If yous like GeeksforGeeks and would like to contribute, you can also write an commodity using write.geeksforgeeks.org or postal service your commodity to review-team@geeksforgeeks.org. Meet your article appearing on the GeeksforGeeks main page and aid other Geeks.
Please write comments if yous find anything incorrect, or y'all want to share more information about the topic discussed higher up.


wentztherip63.blogspot.com

Source: https://www.geeksforgeeks.org/paper-cut-minimum-number-squares-set-2/

0 Response to "Paper Cut Into Ten Pieces Again and Gain"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel