POJ1020 Anniversary Cake

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16683   Accepted: 5442

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

大致意思是,有一块儿边长为s的正方形大蛋糕,问是否能不浪费地分割成n个边长不同的小蛋糕。可行则输出KHOOOOB!否则输出HUTUTU!

逆向思维,尝试用n个边长不同的小蛋糕拼成一个边长为s的正方形大蛋糕

搜索解决。为了便于处理状态,严格从左往右,从下往上地加入小蛋糕,填满下面的行才能填上面的行。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 int T,n,m;
 7 int a[11];//边长为[i]的蛋糕的数量
 8 int ck[50];//第[i]列已填的高度
 9 bool dfs(int y){
10     if(y==m)return 1;
11     int i,j;
12     //
13     int mi=200,pos=0;
14     for(j=1;j<=n;j++)
15         if(ck[j]<mi){
16             mi=ck[j];
17             pos=j;
18         }
19     //
20     for(i=10;i;i--){
21         if(!a[i])continue;//没有该尺寸蛋糕,跳过
22         if(pos+i-1>n)continue;//该尺寸宽度比剩余宽度大,跳过
23         for(j=0;j<i;j++){
24             if(ck[pos+j]<=ck[pos] && ck[pos+j]+i<=n)continue;
25             else break;
26         }
27         if(j<i)continue;//高度不足,跳过
28         for(j=0;j<i;j++)ck[pos+j]+=i;
29         a[i]--;
30         if(dfs(y+1))return 1;
31         a[i]++;
32         for(j=0;j<i;j++)ck[pos+j]-=i;
33     }
34     return 0;
35 }
36 int main(){
37     scanf("%d",&T);
38     int i,j;
39     while(T--){
40         memset(ck,0,sizeof ck);
41         memset(a,0,sizeof a);
42         scanf("%d%d",&n,&m);
43         int x;
44         int ssum=0;
45         int cnt=0;
46         for(i=1;i<=m;i++){
47             scanf("%d",&x);
48             a[x]++;
49             ssum+=x*x;
50             if(x>n/2)cnt++;//尺寸大于n/2的蛋糕若有多块,肯定不可行
51         }
52         if(ssum!=n*n || cnt>1){
53             printf("HUTUTU!\n");
54             continue;
55         }
56         if(!dfs(0)){
57             printf("HUTUTU!\n");
58         }
59         else printf("KHOOOOB!\n");
60
61     }
62     return 0;
63 }    
时间: 2024-11-13 17:14:06

POJ1020 Anniversary Cake的相关文章

poj1020 Anniversary Cake 搜索

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 cak

【DFS】Anniversary Cake

[poj1020]Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17203   Accepted: 5619 Description Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square

poj 1020 Anniversary Cake(切正方形蛋糕+搜索)

Anniversary Cake 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 o

【poj1020】 Anniversary Cake

http://poj.org/problem?id=1020 (题目链接) 题意 有一个S*S的大蛋糕,还有许多正方形的小蛋糕,问能否将大蛋糕完整的分成所有的小蛋糕,不能有剩余. Solution 像这种看起来很麻烦的基本上都是水题,然而poj上的所谓水题,我也是呵呵了. 在根据题意做完若干判断剪枝后,我们开始搜索.因为蛋糕不能有剩余,所以搜索就很好搜了,刚开始没注意,不知道不允许剩余,直接参考了别人的程序= =.详情请见:题解 代码 // poj1020 #include<algorithm>

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 wit

poj 1020 Anniversary Cake dfs的灵活结构

题意: 给一个目标正方形边长和n个小正方形的边长,问是否可以用这n个小正方形填满目标正方形. 分析: dfs不一开始就定好放入顺序,而是用已放入的个数代表深搜维度. 代码: #include <iostream> using namespace std; int box_size; int n; int size_num[16]; int col[64]; bool dfs(int cnt) { if(cnt==n) return true; int minx=INT_MAX,col_inde

poj 1020 Anniversary Cake (DFS)

出处: http://blog.csdn.net/lyy289065406/article/details/6683250 大致题意: 有一块边长为BoxSize的正方形的大蛋糕,现在给出n块不同尺寸的正方形的小蛋糕的边长,问是否能把大蛋糕按恰好切割为这n块小蛋糕,要求每块小蛋糕必须为整块. 解题思路: 有技巧的DFS 可以把大蛋糕想象为一个蛋糕盒子,然后往里面装小蛋糕. 装蛋糕时遵循以下原则: 自下而上,自左至右: 即先装好盒子底部,再继续往上层装,且装每一层时都靠左边放蛋糕: 大蛋糕优先装,

wewe

BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位大虾啦. pku 1175 Starry Night 题目地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1175 解法:BFS,要注意的是如何判断图形是一样的,我的做法就是计算每两个点的距离之和. 看:http://hi.baidu.com/doxi

HDU1020(小正方形铺大正方形)

Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16579   Accepted: 5403 Description Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped c