您的位置:首页 > 其它

HDU POJ 1190 生日蛋糕 dfs + 剪枝

2016-08-30 23:11 190 查看
题目链接:poj 1190

题解:这题刚开始想不到搜索的初始状态,没办法下手,迷迷糊糊的,后来就想着
按照给定n的初始入手,吧未放置的第0层看成一个圆,不是立体,高度也设为n + 1, 然后 从下往上找。。。。 想这个思路 花了太长时间   。。。。虽然是一发a但是感觉 自己对搜索的理解 太浅  能力还是要提高啊

AC code:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>

using namespace std;

#define debug 0
#define INF 0x3f3f3f3f

const int maxn = 20 + 5;
int n, m;
int minv[maxn], ans;

void dfs(int v, int deep, int r, int h, int temp)//v:当前剩余体积,deep层数,r,h:半径,高的范围
{
if (deep == 0)
{
if (v == 0 && temp < ans)
ans = temp;//更新最小值
return;
}

if (v - minv[deep - 1] < 0 || temp >= ans || 2 * v / r + temp >= ans)//2*v/r+temp>=ans 当前解+剩余资源的最优解>=最优解
return;
for (int R = r - 1; R >= deep; R--)//枚举半径
{
int MH = min(h - 1, (v - minv[deep - 1]) / R / R);//取两者较小者
for (int H = MH; H >= deep; H--)//枚举高
{
if (v - R * R * H >= 0)
{
if (deep == m)
temp = R * R;
temp += 2 * R * H;
dfs(v - R * R * H, deep - 1, R, H, temp);
temp -= 2 * R * H;//返回递归前状态
if (deep == m)
temp = 0;
}
}
}
}
int main()
{
#if debug
freopen("in.txt", "r", stdin);
#endif //debug
minv[0] = 0;
for (int i = 1; i <= 20; i++)
minv[i] = minv[i - 1] + i * i * i;  //从上到下前i层蛋糕需要的最小体积

while (~scanf("%d%d", &n, &m))
{
ans = INF;
dfs(n, m, (int)sqrt(n) + 1, n + 1, 0);

if (ans == INF)
ans = 0;

printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs poj