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
 
代码:
 #include<iostream>
#include<cstring>
using namespace std;
int col[]; //将蛋糕分为1*1的小块,下标表示列,值表示用到第几行
int cakesize; //蛋糕大小
int part[]; //数组值为该大小的小块的个数
int num; //蛋糕个数 bool DFS(int fillnum) //从前往后,从左往右放入
{
int min=;
int flag;
int wide;
if(fillnum==num)
return true;
for(int i=; i<=cakesize; i++) //记录所有列里所用最少的
if(min>col[i])
{
min=col[i];
flag=i;
}
for(int size=; size>; size--) //从大到小遍历,从大的开始放,越小灵活性越大
{
if(!part[size])
continue;
if(cakesize-min>=size&&cakesize-flag+>=size) //判断蛋糕放入‘是否有可能’溢出,是否有‘可能’放入
{
wide=; //之前错在这里
for(int j=flag; j<=flag+size-; j++) //与上面的if判断一起,其作用为判断是否能放下该块蛋糕
{
if(col[j]<=min)
wide++;
else
break;
}
if(wide>=size)
{
part[size]--;
for(int k=flag; k<=flag+size-; k++)
col[k]+=size;
if(DFS(fillnum+))
return true;
part[size]++; //回溯
for(int k=flag; k<=flag+size-; k++)
col[k]-=size;
}
}
}
return false;
} int main()
{
int t,side;
cin>>t;
while(t--)
{
memset(part,,sizeof(part));
memset(col,,sizeof(col));
cin>>cakesize;
cin>>num;
for(int i=; i<=num; i++)
{
cin>>side;
part[side]++;
}
if(DFS())
cout<<"KHOOOOB!"<<endl;
else
cout<<"HUTUTU!"<<endl;
}
return ;
}

Anniversary Cake的更多相关文章

  1. POJ 1020 Anniversary Cake(DFS)

    Anniversary Cake Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit St ...

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

                                                                                                         ...

  3. 【DFS】Anniversary Cake

    [poj1020]Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17203   Accep ...

  4. POJ1020 Anniversary Cake

    题目来源:http://poj.org/problem?id=1020 题目大意:有一块边长为s的正方形大蛋糕,有n个客人,每个客人想分一块边长为si的正方形蛋糕.求这块大蛋糕能否恰好满足所有客人的需 ...

  5. 【poj1020】 Anniversary Cake

    http://poj.org/problem?id=1020 (题目链接) 题意 有一个S*S的大蛋糕,还有许多正方形的小蛋糕,问能否将大蛋糕完整的分成所有的小蛋糕,不能有剩余. Solution 像 ...

  6. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest

    A. Anniversary Cake 随便挑两个点切掉就好了. #include<bits/stdc++.h> using namespace std; const int Maxn=2 ...

  7. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  8. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  9. POJ1020(小正方形铺大正方形)

    Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16579   Accepted: 5403 ...

随机推荐

  1. Boss OpenCart 商城自适应主题模板 ABC-0012-01

    Boss OpenCart 商城自适应主题模板 ABC-0012-01 模板特性兼容浏览器IE7, IE8, IE9, IE10, Firefox, Safari, Chrome OpenCart版本 ...

  2. prototype与几个循环的心得

    <一>prototypeprototype其实是函数的一个属性,并且只有函数有这个属性,这个属性就是给函数增加函数或者属性的,比如写一个function one(){},那么one.pro ...

  3. ubuntu上跑python连接pg,报错 ImportError: No module named psycopg2

    ubuntu上跑python连接pg,报错  ImportError: No module named psycopg2 root@pgproxy1:~# python /home/zxw/PGWri ...

  4. 微信JSSDK使用指南

        这篇文章主要来说说微信JSSDK的一些配置和微信分享的使用,包括从前端sdk文件引入到server端和微信server的交互. 另外Tangide已经把一些工作做好了.我会简要说一说怎么把它用 ...

  5. c# Winform实现中国省份地图

    1.利用raphael.js IE下的VML和SVG实现矢量地图 2.JS调用c# Winform代码,代码 [System.Runtime.InteropServices.ComVisibleAtt ...

  6. 2016/1/21 练习 arraylist 1,添加 add() 2,遍历集合

    package shuzu; public class Customer { //从源码中 使用字段生成构造函数 public Customer(String good, int price) { s ...

  7. linux之安装软件出现Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)解决总结

    sudo rm -rf /var/lib/dpkg/lock sudo rm -rf /var/cache/apt/archives/lock

  8. SpringMVC_RESTRUL_CRUD

    编写POJO Departmet: package org.springmvc.curd.entity; public class Department { private int id; priva ...

  9. A Go library implementing an FST (finite state transducer)——mark下

    https://github.com/couchbaselabs/vellum Building an FST To build an FST, create a new builder using ...

  10. IDEA 单元测试

    下载所需的两个 jar 包,下载地址:Download and Install · junit-team/junit4 Wiki · GitHub junit-4.12.jar hamcrest-co ...