POJ 1020
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <iostream>
2 using namespace std;
3 int cake[11], pos[41];
4 int side, number;
5
6 bool DFS(int a)
7 {
8 int min_pos = 41;
9 int put;
10
11 if(a == number)
12 {
13 return true;
14 }
15
16 for(int i=1; i<=side; i++)
17 {
18 if(pos[i] < min_pos)
19 {
20 min_pos = pos[i];
21 put = i;
22 }
23 }
24 int j;
25 for(int i=10; i>=1; i--)
26 {
27 if(cake[i] && min_pos+i <= side && put+i-1 <= side)
28 {
29 for(j=put; j<=put+i-1; j++)
30 {
31 if(pos[j] > min_pos)
32 {
33 break;
34 }
35 }
36
37 if(j>put+i-1)
38 {
39 cake[i]--;
40 for(int t=put; t<=put+i-1; t++)
41 {
42 pos[t] += i;
43 }
44 if(DFS(a+1))
45 {
46 return true;
47 }
48
49 cake[i]++;
50 for(int t=put; t<=put+i-1; t++)
51 {
52 pos[t] -= i;
53 }
54 }
55 }
56 }
57
58 return false;
59 }
60
61 int main()
62 {
63 int count;
64 cin >> count;
65 while(count > 0)
66 {
67 count--;
68 memset(cake, 0, sizeof(cake));
69 memset(pos, 0, sizeof(pos));
70 cin >> side >> number;
71 int sum = 0;
72 int c;
73 for(int i=0; i<number; i++)
74 {
75 cin >> c;
76 cake[c]++;
77 sum += c * c;
78 }
79
80 if(sum != side * side)
81 {
82 cout << "HUTUTU!" << endl;
83 }
84 else
85 {
86 if(DFS(0))
87 {
88 cout << "KHOOOOB!" << endl;
89 }
90 else
91 {
92 cout << "HUTUTU!" << endl;
93 }
94 }
95 }
96
97
98 return 0;
99 }
2 using namespace std;
3 int cake[11], pos[41];
4 int side, number;
5
6 bool DFS(int a)
7 {
8 int min_pos = 41;
9 int put;
10
11 if(a == number)
12 {
13 return true;
14 }
15
16 for(int i=1; i<=side; i++)
17 {
18 if(pos[i] < min_pos)
19 {
20 min_pos = pos[i];
21 put = i;
22 }
23 }
24 int j;
25 for(int i=10; i>=1; i--)
26 {
27 if(cake[i] && min_pos+i <= side && put+i-1 <= side)
28 {
29 for(j=put; j<=put+i-1; j++)
30 {
31 if(pos[j] > min_pos)
32 {
33 break;
34 }
35 }
36
37 if(j>put+i-1)
38 {
39 cake[i]--;
40 for(int t=put; t<=put+i-1; t++)
41 {
42 pos[t] += i;
43 }
44 if(DFS(a+1))
45 {
46 return true;
47 }
48
49 cake[i]++;
50 for(int t=put; t<=put+i-1; t++)
51 {
52 pos[t] -= i;
53 }
54 }
55 }
56 }
57
58 return false;
59 }
60
61 int main()
62 {
63 int count;
64 cin >> count;
65 while(count > 0)
66 {
67 count--;
68 memset(cake, 0, sizeof(cake));
69 memset(pos, 0, sizeof(pos));
70 cin >> side >> number;
71 int sum = 0;
72 int c;
73 for(int i=0; i<number; i++)
74 {
75 cin >> c;
76 cake[c]++;
77 sum += c * c;
78 }
79
80 if(sum != side * side)
81 {
82 cout << "HUTUTU!" << endl;
83 }
84 else
85 {
86 if(DFS(0))
87 {
88 cout << "KHOOOOB!" << endl;
89 }
90 else
91 {
92 cout << "HUTUTU!" << endl;
93 }
94 }
95 }
96
97
98 return 0;
99 }