Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/598/problem/E
Description
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar.
In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.
For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).
For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - ksquares are not necessarily form a single rectangular piece.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process.
Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.
Output
For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.
Sample Input
4
2 2 1
2 2 3
2 2 2
2 2 4
Sample Output
5
5
4
0
HINT
题意
给你n*m的巧克力,然后你想吃面积恰好为k的巧克力,问你怎么样才能花费最少吃到他
每次花费是你切的这刀的长度的平方
题解:
记忆化搜索,然后中间直接暴力dfs就好了
代码
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int dp[][][]; int dfs(int n,int m,int k)
{
if(!k||n*m==k)return ;
if(dp[n][m][k]!=-)return dp[n][m][k];
int& minn = dp[n][m][k];
minn = 9999999LL;
for(int i=;i<n;i++)
for(int j=;j<=k;j++)
minn = min(minn,dfs(i,m,j)+dfs(n-i,m,k-j)+m*m);
for(int i=;i<m;i++)
for(int j=;j<=k;j++)
minn = min(minn,dfs(n,i,j)+dfs(n,m-i,k-j)+n*n);
return minn;
}
int main()
{
int t;scanf("%d",&t);
memset(dp,-,sizeof(dp));
while(t--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
printf("%d\n",dfs(n,m,k));
} }
最新文章
- jquery_DOM笔记4
- 中文编程语言Z语言开源正式开源!!!
- mysql replace into用法与坑
- PyCharm 134 单元测试输出大量空行解决方案
- C#winform项目添加引用编译文件
- xUtils框架介绍(三)
- oracle表空间建立与用户创建删除
- backbone初次使用及hello world
- Git 和 SVN之间的五个基本区别
- android取得所在位置的经纬度
- ruby正则表带式对象使用备忘
- RMQ区间最大值与最小值查询
- Sql语句基础练习(一)
- #7 找出数组中第k小的数
- CSS文本(Text)属性-----letter-spacing和text-align
- L327 找灵魂伴侣
- C#窗体模拟键盘按键(组合键)产生事件 ---- 通过keybd_event()函数
- day21-类的组合
- 【MYSQL】语法复习
- Kafka下的生产消费者模式与订阅发布模式