POJ 1020 Anniversary Cake

九点五十九分A了我的第二题,之前提交了好多次,各种改,最后还是很兴奋。

我很开心,真的很开心,这是我自己独立做出来的两道ACM难度的试题,而且还不是水题,虽然自己参考了别人的算法。

本来今天计划是要A三题的,但是下午海龙跑过来找我了,晚上去广外吃了饭、粥、粉、螺。感冒了,抽烟不爽。

贴下代码,继续加油!

View Code
 1 #include<iostream>
2 //#include<memory.h>
3 using namespace std;
4
5
6 int cakesLine,cakesPieces;
7 const int MAXLONG = 40;
8
9 int len[41];
10 int cakes[11];
11
12
13 bool dfs(int num)
14 {
15 int max=40;
16 int maxcol=0;
17
18 int trypieces=0;
19
20 bool flag=true;
21
22 if(num == cakesPieces)
23 return true;
24
25 for(int i=1;i<=cakesLine;i++)
26 {
27 if(len[i]<max)
28 {
29 max=len[i];
30 maxcol=i;
31 }
32 }
33
34 for(trypieces=1;trypieces<=cakesPieces;trypieces++)
35 {
36 if(cakes[trypieces]&&(trypieces+maxcol<=cakesLine+1)&&(max+trypieces<=cakesLine))
37 {
38 for(int j=maxcol;j<maxcol+trypieces;j++)
39 if(len[j]!=max)
40 {
41 flag=false;
42 break;
43 }
44
45 if(flag)
46 {
47 cakes[trypieces]--;
48 for(int j=maxcol;j<maxcol+trypieces;j++)
49 len[j]+=trypieces;
50 if(dfs(num+1))
51 return true;
52 cakes[trypieces]++;
53 for(int j=maxcol;j<maxcol+trypieces;j++)
54 len[j]-=trypieces;
55 }
56
57 }
58 }
59 return false;
60
61
62 }
63
64 int main()
65 {
66 int cases;
67 cin>>cases;
68
69 // memset(len,0,sizeof(len[0]));
70 // memset(cakes,0,sizeof(cakes[0]));
71
72
73 while(cases--)
74 {
75 for(int i=1;i<41;i++) len[i]=0;
76 for(int i=1;i<11;i++) cakes[i]=0;
77 int sum=0;
78 cin>>cakesLine>>cakesPieces;
79 for(int i=1;i<=cakesPieces;i++)
80 {
81 int edg;
82 cin>>edg;
83 cakes[edg]++;
84 sum+=edg*edg;
85 }
86 if(cakesLine*cakesLine == sum && dfs(0) )
87 cout<<"KHOOOOB!"<<endl;
88 else
89 cout<<"HUTUTU!"<<endl;
90
91 }
92 }

原文地址:https://www.cnblogs.com/YipWingTim/p/2200876.html