Anniversary Cake

Anniversary Cake
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15704   Accepted: 5123

Description

Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped chocolate cake with known size. She asks each invited person to determine the size of the piece of cake that he/she wants (which should also be square-shaped). She knows that Mr. Kavoosi would not bear any wasting of the cake. She wants to know whether she can make a square cake with that size that serves everybody exactly with the requested size, and without any waste.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case. Each test case consist of a single line containing an integer s, the side of the cake, followed by an integer n (1 ≤ n ≤ 16), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.

Output

There should be one output line per test case containing one of the words KHOOOOB! or HUTUTU! depending on whether the cake can be cut into pieces of specified size without any waste or not.

Sample Input

2
4 8 1 1 1 1 1 3 1 1
5 6 3 3 2 1 1 1

Sample Output

KHOOOOB!
HUTUTU!

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
题解:可理解为将多个正方形小蛋糕放入一个正方形蛋糕盒子。从左往右,从前往后放入。简单的DFS
 
代码:
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int col[45]; //将蛋糕分为1*1的小块,下标表示列,值表示用到第几行
 5 int cakesize;   //蛋糕大小
 6 int part[11];    //数组值为该大小的小块的个数
 7 int num;    //蛋糕个数
 8 
 9 bool DFS(int fillnum)     //从前往后,从左往右放入
10 {
11     int min=50;
12     int flag;
13     int wide;
14     if(fillnum==num)
15         return true;
16     for(int i=1; i<=cakesize; i++)   //记录所有列里所用最少的
17         if(min>col[i])
18         {
19             min=col[i];
20             flag=i;
21         }
22     for(int size=10; size>0; size--)    //从大到小遍历,从大的开始放,越小灵活性越大
23     {
24         if(!part[size])
25             continue;
26         if(cakesize-min>=size&&cakesize-flag+1>=size)   //判断蛋糕放入‘是否有可能’溢出,是否有‘可能’放入
27         {
28             wide=0;                      //之前错在这里
29             for(int j=flag; j<=flag+size-1; j++)  //与上面的if判断一起,其作用为判断是否能放下该块蛋糕
30             {
31                 if(col[j]<=min)
32                     wide++;
33                 else
34                     break;
35             }
36             if(wide>=size)
37             {
38                 part[size]--;
39                 for(int k=flag; k<=flag+size-1; k++)
40                     col[k]+=size;
41                 if(DFS(fillnum+1))
42                     return true;
43                 part[size]++;                              //回溯
44                 for(int k=flag; k<=flag+size-1; k++)
45                     col[k]-=size;
46             }
47         }
48     }
49     return false;
50 }
51 
52 int main()
53 {
54     int t,side;
55     cin>>t;
56     while(t--)
57     {
58         memset(part,0,sizeof(part));
59         memset(col,0,sizeof(col));
60         cin>>cakesize;
61         cin>>num;
62         for(int i=1; i<=num; i++)
63         {
64             cin>>side;
65             part[side]++;
66         }
67         if(DFS(0))
68             cout<<"KHOOOOB!"<<endl;
69         else
70             cout<<"HUTUTU!"<<endl;
71     }
72     return 0;
73 }

 

posted on 2015-04-27 18:53  JASONlee3  阅读(384)  评论(0编辑  收藏  举报

导航