ZOJ 3141 Arnie's Dog Biscuits【dp递推】
2017-08-18 09:41
357 查看
Arnie's Dog Biscuits
Time Limit: 1 Second Memory Limit: 32768 KB
Our discerning gourmet puppy Arnie is turning to you for a program to help him split his dog biscuits. Each biscuit is shaped like a rectangle and perforated into equal sized squares:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/02/eedd5dba9a248dd2603416bbf0e42609.jpg)
Unfortunately, Arnie will only eat square-shaped biscuits; therefore, he must break the biscuit into squares. Each break, termed a split, is applied to one rectangle, runs along one straight
perforated line, and separates the rectangle into two pieces:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/02/8d962d2c6128d5f68cc31956aec8cb00.jpg)
Input
The first line of the input contains one positive integer n, the number of biscuits to split. Each of the next n lines contains two positive integers r and c, the number of rows and columns
of one biscuit, separated by white space.
Output
The output contains one line for each biscuit specifying the minimal number of splits required to break the biscuit into squares.
Sample Input
This defines two biscuits: the one shown above which requires four splits, and a square biscuit which requires no splits.
Sample Output
dp[i][j]=min(dp[r][j]+dp[i-r][j]+1, dp[i][k]+dp[i][j-k]+1)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
int dp[1100][1100];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n, m;
ms(dp, INF);
for (int i = 0; i <= 250; i++)
{
dp[i][i]=0;
dp[i][0]=0;
dp[0][i]=0;
}
for (int i = 1; i <= 250; i++)
{
for (int j = 1; j <= 250; j++)
{
for (int k = 0; k <= i; k++)
{
dp[i][j] = min(dp[i][j], dp[k][j] + dp[i - k][j] + 1);
}
for (int k = 0; k <= j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[i][j - k] + 1);
}
}
}
int t;
scanf("%d", &t);
while (t--)
{
int n, m;
scanf("%d%d", &n, &m);
printf("%d\n", dp
[m]);
}
return 0;
}
Time Limit: 1 Second Memory Limit: 32768 KB
Our discerning gourmet puppy Arnie is turning to you for a program to help him split his dog biscuits. Each biscuit is shaped like a rectangle and perforated into equal sized squares:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/02/eedd5dba9a248dd2603416bbf0e42609.jpg)
Unfortunately, Arnie will only eat square-shaped biscuits; therefore, he must break the biscuit into squares. Each break, termed a split, is applied to one rectangle, runs along one straight
perforated line, and separates the rectangle into two pieces:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/02/8d962d2c6128d5f68cc31956aec8cb00.jpg)
Input
The first line of the input contains one positive integer n, the number of biscuits to split. Each of the next n lines contains two positive integers r and c, the number of rows and columns
of one biscuit, separated by white space.
Output
The output contains one line for each biscuit specifying the minimal number of splits required to break the biscuit into squares.
Sample Input
2 6 7 5 5
This defines two biscuits: the one shown above which requires four splits, and a square biscuit which requires no splits.
Sample Output
4 0
dp[i][j]=min(dp[r][j]+dp[i-r][j]+1, dp[i][k]+dp[i][j-k]+1)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
int dp[1100][1100];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n, m;
ms(dp, INF);
for (int i = 0; i <= 250; i++)
{
dp[i][i]=0;
dp[i][0]=0;
dp[0][i]=0;
}
for (int i = 1; i <= 250; i++)
{
for (int j = 1; j <= 250; j++)
{
for (int k = 0; k <= i; k++)
{
dp[i][j] = min(dp[i][j], dp[k][j] + dp[i - k][j] + 1);
}
for (int k = 0; k <= j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[i][j - k] + 1);
}
}
}
int t;
scanf("%d", &t);
while (t--)
{
int n, m;
scanf("%d%d", &n, &m);
printf("%d\n", dp
[m]);
}
return 0;
}
相关文章推荐
- ZOJ 3872-Beauty of Array(递推||DP)
- LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)
- zoj 3747 Attack on Titans 递推 计数dp
- UVA 10328 , ZOJ 3747 (DP 递推)
- ZOJ 3256 Tour in the Castle(插头DP-按行递推—矩阵)
- zoj3141 nie's Dog Biscuits(DP)
- 【dp】递推 zoj3747
- 简单dp之递推(2)--ZOJ 3747
- 【dp】递推 zoj3747
- zoj 3747 Attack on Titans 带限制条件的计数递推dp
- dp递推 zoj 3747 Attack on Titans
- HDU 6143 Killer Names【dp递推】【好题】【思维题】【阅读题】
- zoj3605 Find the Marble --- 概率dp
- ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)
- ZOJ 3469 Food Delivery (区间DP,经典)
- ZOJ 3929 Deque and Balls (放球问题_DP思维好题)
- zoj 3408 dp
- ZOJ 3725 Painting Storages DP
- ZOJ-3399 Classes Division 单调队列优化DP
- ZOJ 3822 期望DP