pku1190 生日蛋糕
这道题是一道经典的搜索剪枝题,每次听搜索的课总有这道题,剪枝思想主要就是最优化和可行性。
最优化:当前面积+下面的最小面积>当前解
可行性:1:当前剩余体积<做完剩余蛋糕需要的最小体积
2:当前剩余体积+做剩余蛋糕能用的最大体积<N
View Code
1 program pku1190(input,output);
2 const
3 maxr = 30;
4 maxh = 30;
5 var
6 n,m,answer : longint;
7 minv : array[0..21] of int64;
8 mins : array[0..21] of int64;
9 procedure init;
10 begin
11 readln(n);
12 readln(m);
13 end; { init }
14 procedure dfs(now,v,ri,hi,s :longint );
15 var
16 i,r,h,ss,maxv : longint;
17 begin
18 if v<minv[now] then
19 exit;
20 if s+mins[now]>=answer then
21 exit;
22 maxv:=0;
23 for i:=1 to now do
24 inc(maxv,(ri-i)*(ri-i)*(hi-i));
25 if maxv<v then
26 exit;
27 if now=0 then
28 begin
29 ss:=s+ri*ri;
30 if ss<answer then
31 answer:=ss;
32 exit;
33 end;
34 for r:=ri-1 downto now do
35 for h:=hi-1 downto now do
36 dfs(now-1,v-r*r*h,r,h,s+ri*ri-r*r+2*r*h);
37 end; { dfs }
38 procedure main;
39 var
40 i,r,h : longint;
41 begin
42 answer:=19950714;
43 mins[0]:=1;
44 minv[0]:=0;
45 for i:=1 to m do
46 begin
47 minv[i]:=minv[i-1]+i*i*i;
48 mins[i]:=mins[i-1]+2*i*i+2*i-1;
49 end;
50 for r:=maxh downto m do
51 for h:=maxh downto m do
52 dfs(m-1,n-r*r*h,r,h,2*r*h);
53 end; { main }
54 procedure print;
55 begin
56 if answer=19950714 then
57 writeln(0)
58 else
59 writeln(answer);
60 end; { print }
61 begin
62 init;
63 main;
64 print;
65 end.