链接: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. "makefile:5: *** missing separator. Stop."【转】

    本文转载自:http://blog.csdn.net/fireroll/article/details/8607903 写makefile时出现这个错误提示 是表示makefile中的命令前没有使用T ...

  2. jquery中attr和prop的区别—判断复选框选中状态

    最近项目中需要用jquery判断input里checkbox是否被选中,发现用attr()获取不到复选框改变后的状态,最后查资料发现jQuery 1.6以后新增加了prop()方法,借用官方的一段描述 ...

  3. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  4. iPhone HTTP获得XML并使用GDataXML解析

    1. [代码][C/C++]代码     NSURL *url = [NSURL URLWithString:  @"http://www.raywenderlich.com/downloa ...

  5. 细谈HTML解析模块

     细谈HTML解析模块 Html在网页中所占的位置,用一个简单直观的图给展示一下:    

  6. python 基础之第十三天(xineted服务器,forking,多线程)

  7. codevs 3269 混合背包(复习混合背包)

    传送门 [题目大意]给出物品的数量.-1为无限个. [思路]混合背包.... [code] #include<iostream> #include<cstdio> #inclu ...

  8. JVM介绍(一)

    JVM是运行在操作系统之上的,它与硬件没有直接的交互 类装载器ClassLoader 负责加载class文件,class文件在文件开头有特定的文件标示,并且ClassLoader只负责class文件的 ...

  9. 各个国家 不同字符集的unicode 编码范围

    原文地址:http://blog.csdn.NET/xzl04/article/details/6307416 0000-007F:C0控制符及基本拉丁文 (C0 Control and Basic  ...

  10. unittest执行测试用例的N种姿势总结

    1.我们写几个方法,用来做测试用例 2.我们在另一文件中引用这个模块下面的所有类方法,先看第一种方法: 运行结果: 缺点:每个用例都需要加载到测试套件中,如果有1000个用例,要写1000次重复的代码 ...