C++-POJ1020-Anniversary Cake[搜索][dfs]
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int t,s,n,a,sum,row[],cake[];
void init(){memset(row,,sizeof(row)),memset(cake,,sizeof(cake)),sum=;}
int find(){int p;for(int i=,min=;i<=s;i++)if(min>row[i])min=row[i],p=i;return p;}
int count(int p){int cnt=;for(int i=p;i<=s;i++)if(row[i]==row[p])cnt++;else break;return cnt;}
void put(int p,int x){cake[x]--;for(int i=p;i<=p+x-;i++)row[i]+=x;}
void del(int p,int x){cake[x]++;for(int i=p;i<=p+x-;i++)row[i]-=x;}
bool dfs(int dep) {
if(dep==n)return true;int p=find();
for(int x=;x;x--)if(cake[x]&&row[p]+x<=s&&count(p)>=x){put(p,x);if(dfs(dep+))return true;del(p,x);}
return false;
}
int main() {
for (scanf("%d",&t);t--;) {
init(),scanf("%d%d",&s,&n);
for(int i=;i<=n;i++)scanf("%d",&a),cake[a]++,sum+=a*a;
puts((sum==s*s?dfs():)?"KHOOOOB!":"HUTUTU!");
}
return ;
}
C++-POJ1020-Anniversary Cake[搜索][dfs]的更多相关文章
- POJ1020 Anniversary Cake
题目来源:http://poj.org/problem?id=1020 题目大意:有一块边长为s的正方形大蛋糕,有n个客人,每个客人想分一块边长为si的正方形蛋糕.求这块大蛋糕能否恰好满足所有客人的需 ...
- POJ 1020 Anniversary Cake(DFS)
Anniversary Cake Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit St ...
- 【DFS】Anniversary Cake
[poj1020]Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17203 Accep ...
- poj 1020 Anniversary Cake(切正方形蛋糕+搜索)
...
- Anniversary Cake
Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15704 Accepted: 5123 ...
- 简单搜索dfs, 简单的修剪搜索
选择最合适的语言做一个项目是非常重要的.但,熟练的掌握自己的武器,这也是非常重要的. ========================================================= ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)
深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...
- 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)
需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...
- POJ 2243 简单搜索 (DFS BFS A*)
题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...
随机推荐
- Miller-Rabin素数检测算法 acm模板
Miller-Rabin素数检测算法 其基于以下两个定理. Fermat小定理 若n是素数,则∀a(a̸≡0(modn))\forall a(a \not\equiv 0 \pmod{n})∀a(a̸ ...
- oracle基础知识点
一.count(*).count(1).count(字段名)的区别select count(*) from t_md_inst --153797 --包含字段为null 的记录select count ...
- hextorgb
function hexToRgb(hex) { // By Tim Down - http://stackoverflow.com/a/5624139/3493650 // Expand short ...
- NOI Online能力测试游记:退役选手的自娱自乐
2020年2月17日早上8点,CCF发布了关于举办NOI Online能力测试的通知. 为给选手提供一个锻炼的机会,CCF拟举办一场NOI Online能力测试.测试分为入门组和提高组,每组限额报名3 ...
- 指数函数在c语言实现
指数很重要,比如有一些欧拉公式 #include "common.h" #include <stdio.h> #include <stdlib.h> #in ...
- [CF1303A] Erasing Zeroes
Solution 找到边界然后循环扫一遍数个数即可 #include <bits/stdc++.h> using namespace std; int n; const int N = 1 ...
- 0级搭建类013-CentOS 8.x 安装
CentOS 8 操作系统安装
- 学Python必须背的42个常见单词,看看你都会吗?
这42个单词是学习Python必须背会的单词,也是代码中常见的单词.希望你能都背下来! ! 1. adult [ˈædʌlt] 成年人 2. authentication [ɔːˌθentɪˈke ...
- 获取redis指定实例中所有的key
需求:获取redis指定的实例中所有的key的名字. 千万不要使用keys *,可以使用scan命令的递归方式获取. 以下给出自己写的脚本,经过测试效果还可以. db_ip=5.5.5.101 db_ ...
- 在vue项目中显示实时时间(年月日时分秒)
1.在data中定义一个变量,存储时间 data(){ return { nowTime:'' } }, 2.给定一个div <div>{{nowTime}}</div> 3. ...