-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcode_BU(Rectangle Cutting).cpp
More file actions
39 lines (29 loc) · 996 Bytes
/
code_BU(Rectangle Cutting).cpp
File metadata and controls
39 lines (29 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
fast_io;
int a,b; //taking input
cin>>a>>b;
vector <vector<int>> dp(a+1,vector<int> (b+1)); //
for(int i=0;i<=a;i++)
{
for(int j=0;j<=b;j++)
{
//Base Case
if(i == j) //when i==j, it is already a square,so 0 cuts needed.
dp[i][j] = 0;
else
{
dp[i][j] = INT_MAX; //initially max
for(int k=1;k<i;k++)
dp[i][j] = min(dp[i][j], dp[k][j]+dp[i-k][j]+1); //check for min possible among all possible cuts in i (horizontally)
for(int k=1;k<j;k++)
dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][j-k]+1); //check for min possible among all possible cuts in j (vertically)
}
}
}
cout<<dp[a][b]<<endl;
return 0;
}