链接:https://vjudge.net/problem/POJ-1020

题意:

给一个宽为s的正方形,再给n个变长为an的小正方形,

判断是否能将这n个小正方形完全填充到这个大正方形里面。

思路:

首先判断总面积是否相等。

用一维数组记录每一列用了多少的高度。

每次选择剩余高度最长的 ,再找出对应的长度。

从大往小的选择正方形往里填充。

如果刚好填充完,则满足。

代码:

#include <iostream>
#include <memory.h>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <queue>
#include <string> using namespace std; typedef long long LL; const int MAXN = 60; int square[MAXN];
int col[MAXN];
int n, m; bool DFS(int num)
{
if (num == n)
return true;
int minVal = m;
int minOrd = 0;
for (int i = 1;i <= m;i++)
{
//填充最小的列
if (col[i] < minVal)
{
minVal = col[i];
minOrd = i;
}
}
int minNum = 0;
for (int i = minOrd;i <= m;i++)
{
//得到填充最小的列的行数
if (col[i] == minVal)
minNum++;
else
break;
}
//从10往1填
for (int i = 10;i >= 1;i--)
{
if (square[i] <= 0)
continue;
if (i <= m - col[minOrd] && i <= minNum)
{
square[i]--;
for (int j = minOrd;j < minOrd + i;j++)
col[j] += i;
if (DFS(num + 1))
return true;
for (int j = minOrd;j < minOrd + i;j++)
col[j] -= i;
square[i]++;
}
}
return false;
} int main()
{
int t;
int wid;
scanf("%d", &t);
while (t--)
{
int sum = 0;
memset(square, 0, sizeof(square));
memset(col, 0, sizeof(col));
scanf("%d%d", &m, &n);
for (int i = 1;i <= n;i++)
{
cin >> wid;
square[wid]++;
sum += wid * wid;
}
if (sum != m * m)
{
cout << "HUTUTU!" << endl;
continue;
}
if (DFS(0))
cout << "KHOOOOB!" << endl;
else
cout << "HUTUTU!" << endl;
} return 0;
}

  

POJ-1020-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. POJ1020 Anniversary Cake

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

  4. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  5. DP Intro - poj 2342 Anniversary party

    今天开始做老师给的专辑,打开DP专辑 A题 Rebuilding Roads 直接不会了,发现是树形DP,百度了下了该题,看了老半天看不懂,想死的冲动都有了~~~~ 最后百度了下,树形DP入门,找到了 ...

  6. 【DFS】Anniversary Cake

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

  7. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  8. Anniversary Cake

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

  9. 【poj1020】 Anniversary Cake

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

  10. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

随机推荐

  1. Gym - 100676E —— 基础题

    题目链接:https://odzkskevi.qnssl.com/1110bec98ca57b5ce6aec79b210d2849?v=1490453767 题解: 这种方法大概跟离散化扯上点关系:首 ...

  2. BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心

    BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜 ...

  3. [SPOJ1557] Can you answer these queries II

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2482 [算法] 线段树维护历史最值 时间复杂度 : O(NlogN) [代码] #i ...

  4. 面试题:sql数据查询

    前几天参加一个面试,面试公司让做一套题,sql题不是很难,但是我第一次还是写错了,回来后,重新写了下.简单记录下吧, 1.题目: 2.测试数据 select * from student ; inse ...

  5. PLSQL导入导出oracle表 表空间

    PLSQL导入导出表的正确步骤 原来总是直接 tools->import talbes->Oracle Import结果发现有的时候会出错:有的表不能正确导入, baidu+googel解 ...

  6. 模拟定位工具gps mock

    1. 到应用宝下载http://sj.qq.com/myapp/detail.htm?apkName=com.lexa.fakegps 2.  在  setting  里面  开发者选项 3. 把 模 ...

  7. 给source insight添加.cc的C++文件后缀识别(转载)

    转自:http://blog.chinaunix.net/uid-9950859-id-99172.html 今天在读mysql代码的时候,发现.cc结尾的文件都没有添加进来,google下了,发现原 ...

  8. 执行多个Sql脚本,Sqlplus

    1.先制作需要执行的Sql文件list CMD 中输入[dir E:\FolderName >E:\ExcuteSqlList.txt ] 2.根据ExcuteSqlList.txt 中的文件名 ...

  9. HTTP客户端代码片段

    代码片段: public HttpURLConnection connection = null; 设置connection属性 URL url = new URL(urlPath); connect ...

  10. IOS按需返回刷新数据

    问题描述 相信大家都会遇到过这种情况: 进入下一页面,并且在下一页面执行某一动作,返回要刷新,没有执行某一动作,返回不刷新.也就是当前页面要实现按照需求刷新页面 实现思路 在当前页面定义个Bool类型 ...