题意:一个n*m的巧克力,你想吃k个单元格的巧克力,每一次切都有切的那条边的长度平方的代价,问最小代价
思路:明显的DP,设dp[i][j][k]为在i*j大的巧克力中吃k的最小代价,那么直接预处理枚举巧克力的长,宽,k,切的方式,切的位置..见代码....
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;
#define INF 1e9+7
int dp[32][32][52];
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
int i, j, k, h, m;
for (i = 0; i <= 30; i++)
{
for (j = 0; j <= 30; j++)
{
for (k = 0; k <= 50; k++)
{
if (k == i*j || k == 0)
{
dp[i][j][k] = 0;
}
else
{
dp[i][j][k] = INF;
}
for (h = 0; h <= k; h++)
{
for (m = 1; m < j; m++)
dp[i][j][k] = min(dp[i][j][k], dp[i][m][h] + dp[i][j - m][k - h] + i*i);
for (m = 1; m < i; m++)
dp[i][j][k] = min(dp[i][j][k], dp[m][j][h] + dp[i - m][j][k - h] + j*j);
}
}
}
}
scanf("%d", &k);
while (k--)
{
scanf("%d%d%d", &i, &j, &h);
printf("%d\n", dp[i][j][h]);
}
//system("pause");
return 0;
}