Anniversary Cake

Problem 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!

大致题意:
有一块边长为BoxSize的正方形的大蛋糕,现在给出n块不同尺寸的正方形的小蛋糕的边长,
问是否能把大蛋糕按恰好切割为这n块小蛋糕,要求每块小蛋糕必须为整块。

可以把大蛋糕想象为一个蛋糕盒子,然后往里面装小蛋糕。
装蛋糕时遵循以下原则:
自下而上,自左至右;
即先装好盒子底部,再继续往上层装,且装每一层时都靠左边放蛋糕;
大蛋糕优先装,因为小蛋糕灵活度比较高。

#include<stdio.h>
#include<string.h>//用排序来确定从大到小的蛋糕,再遍历,超时,用数组确定每个蛋糕大小有多少个
int t,s,n,arr[11];// arr 为各种大小蛋糕的个数!s为”大蛋糕“尺寸,n为“小蛋糕”数量
int col[41];//统计每列已被占用的格子int dfs(int Num)//Num已放入蛋糕盒子的数量
{if(Num == n)return 1;int min = 50,p = 0;//从左寻找格子数被填充最少的列for(int i = 0; i<s; i++){if(min>col[i]){min = col[i];p = i;//记录哪一列}}for(int size = 10; size>=1; size--)//从大到小的尺寸开始自下而上的放入盒子{if(!arr[size])//Size 大小的蛋糕个数为 0 的情况continue;//判断横向和纵向是否越界if(s-min >=size && s-p >=size){int wide = 0;//检查盒子从第prow列到第prow+size-1列,共size列的宽度wide中//是否每列剩余的空间都足够放入高度为size的蛋糕for(int j = p; j<=p+size-1; j++)if(col[j] <= col[p])//判断后面的各列填充数是否满足条件{wide++;}if(wide>=size){int k;//放入尺寸为Size的蛋糕arr[size]--;for(k = p; k<=p+size-1; k++){col[k]+=size;}if(dfs(Num+1))return 1;//回溯arr[size]++;for(k = p; k<=p+size-1; k++){col[k]-=size;}}}}return 0;
}int main()
{scanf("%d",&t);while(t--){memset(col,0,sizeof(col));memset(arr,0,sizeof(arr));scanf("%d%d",&s,&n);int size,k = 0,l = 0;//l 记录 size>(s/2) 的个数for(int i = 0; i<n; i++){scanf("%d",&size);k += size*size;arr[size]++;if(size>(s/2))l++;}if(l>1 || k != s*s)//如果面积不相等直接输出HUTUTU!{printf("HUTUTU!\n");continue;}if(dfs(0)){printf("KHOOOOB!\n");}elseprintf("HUTUTU!\n");}return 0;
}

参考:https://blog.csdn.net/lyy289065406/article/details/6683250

Anniversary Cake (深搜)相关推荐

  1. Go 分布式学习利器(15) -- Go 实现 深搜和广搜

    强化语法,回顾算法. 通过Go语言实现 深度优先搜索 和 广度优先搜索,来查找社交网络中的三度好友关系(三度指的是一个节点到 其相邻节点 到 其相邻节点的节点 ,图递增三层好友关系). 涉及到的Go语 ...

  2. 水管工游戏 (深搜)

    水管工游戏 本题依然是采用搜索,深搜,广搜都可以,本代码采用深搜,此题在搜索时需要增加一些判断条件以及下一步要搜索的位置即可. 代码如下: #include<stdio.h> int a[ ...

  3. Poj(2488),按照字典序深搜

    题目链接:http://poj.org/problem?id=2488 思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序. 后来,凡哥说可以在搜索路径的时候就按照字 ...

  4. [数据结构] 迷宫问题(栈和队列,深搜和广搜)

    代码: #include <iostream> #include <string.h> #include <stack> #include <queue> ...

  5. 迷宫问题最短捷径c语言深搜,迷宫问题 C语言实现(深搜)

    问题描述: 2015年05月21日 10:24:05 这是我自己出的一道题   其原型基于迷宫问题,用深搜来解决的!我就简单的说一说吧! 给定一个N * M 的迷宫!,1代表有障碍,0代表无障碍可通行 ...

  6. POJ-1724 深搜剪枝

    这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) {if (s==N){minLen=min(minLen,totalLen);return ;}for ( ...

  7. POJ2044 深搜+剪枝(云彩下雨)

    题意:        有一个城镇,是4*4的大小的,然后你控制一块云彩,2*2的,你每天可以有9种走的方法,上下左右,或者不动,走的时候可以走1或者2步,云彩所在的地方肯定会下雨,然后给你做多365天 ...

  8. hdu4876 深搜+(随机枚举剪枝)

    题意:       给你n个数,让你从选择k个数,然后排成一个环(k个数的顺序随意,但是排成一个环后就不能变了),然后可以在这个环上任意的找连续w个数(w<=k),可以找多次,得到一个值等于当前 ...

  9. 单词拼接 ----- 深搜

    先判断这些单词能不能构成 接龙 , 能的话在排序 , 然后深搜确定接龙 . 题解 : 如果先确定所有单词的首尾字母的个数 , 如果首字母个数等于尾字母个数就不用管了 , 如果发现首字母比尾字母大1那个 ...

最新文章

  1. shell快速将同一名称的不同类型文件自动划分到一个文件中
  2. 用过美德乐吸奶器的宝妈们感觉比国产吸奶器怎么样啊?
  3. CentOS 7 搭建docker仓库
  4. 有没有一种方法可以缓存GitHub凭证来推送提交?
  5. 【Flutter】开发 Flutter 包和插件 ( 开发 Dart 插件包 | 发布 Dart 插件包 )
  6. HDU - 2586 How far away ?(LCA)
  7. 关于SAP Commerce Cloud OCC API url里不包含user信息的问题
  8. [2-sat专练]poj 3683,hdu 1814,hdu 1824,hdu 3622,hdu 4115,hdu 4421
  9. 通讯可以并联吗_工业控制知识:吃透RS485通讯的连接方式,接485总线就简单多了...
  10. Python中的实用小技巧,可以省下不是事情,喜欢记得收下
  11. 神经网络入门--连载1
  12. iOS照片缩略图thumbnail模糊问题
  13. Windows IO 性能简单测试
  14. Dapper.NET—轻量ORM
  15. 移动网速测试软件,网速测试大师APP
  16. storm风暴英雄 tempo_《风暴英雄》Tempo Storm天梯环境速报
  17. 导入百度导航SDK遇到的相关问题
  18. 电脑突然重启解决方案
  19. 提升WordPress 打开速度全面解决方案
  20. 如何设置虚拟机访问外网

热门文章

  1. Flink实操 : 状态管理
  2. 怎么删除github项目/仓库中已经上传的代码
  3. 从经济学角度解读人工智能
  4. MAC virtual box“不能为虚拟机启动一个新任务”问题解决方法
  5. 详解 P沟道mos管与N沟道mos管
  6. c语言对于随机数的产生于应用,c语言之产生随机数详解
  7. 寄存器寻址和寄存器间接寻址的区别
  8. C语言实现SM4加解密算法
  9. Codeforces 1089D Eels (看题解)
  10. Ubuntu 更新错误修复大全