迭代加深搜索基础

题目描述

A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don’t like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.

The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.

给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)

输入输出格式

输入格式:

  • Line 1: N and W separated by a space.

  • Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.

输出格式:

  • Line 1: A single integer, R, indicating the minimum number of elevator rides needed.

  • Lines 2..1+R: Each line describes the set of cows taking

one of the R trips down the elevator. Each line starts with an integer giving the number of cows in the set, followed by the indices of the individual cows in the set.

输入输出样例

输入样例#1: 复制

4 10
5
6
3
7

输出样例#1: 复制

3
2 1 3
1 2
1 4

说明

There are four cows weighing 5, 6, 3, and 7 pounds. The elevator has a maximum weight capacity of 10 pounds.

We can put the cow weighing 3 on the same elevator as any other cow but the other three cows are too heavy to be combined. For the solution above, elevator ride 1 involves cow #1 and #3, elevator ride 2 involves cow #2, and elevator ride 3 involves cow #4. Several other solutions are possible for this input.

思路

  • 迭代加深搜索:这是一种类似广搜的深搜,但是它不需要广搜如此大的空间,它的空间与深搜的空间一样
    可以看做带深度限制的DFS。
    首先设置一个搜索深度,然后进行DFS,当目前深度达到限制深度后验证当前方案的合理性,更新答案。
    不断调整搜索深度,直到找到最优解。
    本题中枚举电梯数num,就是搜索的深度
  • 剪枝: 可证第i奶牛放到i后车厢没有意义

代码

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
int n, m, c[19], tot(0), ans(0), v[19];
bool dfs(int x, int num){
for(re i=1;i<=x&&i<=num;++i)
if(v[i]+c[x]<=m){
v[i]+=c[x];
if(x==n) return 1;
if(dfs(x+1,num)) return 1;
v[i]-=c[x];
}
return 0;
}
int main(){
scanf("%d%d",&n,&m);
for(re i=1;i<=n;++i) scanf("%d", &c[i]);
for(re i=1;i<=n;++i){//枚举厢数
memset(v,0,sizeof(v));
if (dfs(1,i)){
printf("%d\n",i);
break;
}
}
return 0;
}

【题解】Luogu P3052 【USACO12】摩天大楼里的奶牛Cows in a Skyscraper的更多相关文章

  1. LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  2. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  3. 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  4. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...

  5. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp

    这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...

  6. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  7. [USACO12MAR] 摩天大楼里的奶牛 Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  8. [bzoj2621] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta] ...

  9. [luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)

    传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f ...

随机推荐

  1. SQL Server 查看进程阻塞及处理

    修改或删除数据前先备份,先备份,先备份(重要事情说三遍)! 1.首先,查看线程,分析是否存在阻塞进程,blocked>0都是当前被阻塞的进程  SELECT * FROM sysprocesse ...

  2. Jetpack Compose What and Why, 6个问题

    Jetpack Compose What and Why, 6个问题 1.这个技术出现的背景, 初衷, 要达到什么样的目标或是要解决什么样的问题. Jetpack Compose是什么? 它是一个声明 ...

  3. primary key

    只要使用innodb就要为表指定主键: 如果不指定mysql就会自己找不为空且为一的作为主键,如果找不到,就会使用默认的(软件自己预定好的)作为主键: 主键分为单列主键和复合主键,用法和单列唯一,联合 ...

  4. 戴尔服务器如何配置远程管理卡(IDRAC9)适用于戴尔R740服务器

    戴尔服务器如何配置远程管理卡(IDRAC9)适用于戴尔R740服务器 转: DELL IDRAC9 该配置方法适合于所有戴尔14G服务器,包括全系列戴尔服务器,标准版适用于R440/R540/R640 ...

  5. cut命令用于按“列”提取文本字符,格式为“cut [参数] 文本”

    8.cut命令 cut命令用于按"列"提取文本字符,格式为"cut [参数] 文本". 在Linux系统中,如何准确地提取出最想要的数据,这也是我们应该重点学习 ...

  6. Linux_进程之间的通信

    一.进程间的通信 1️⃣:进程间通信(IPC:Inter Process Communication) 2️⃣:进程之间通信方式: 同一主机 共享内存 信号:Signal 不同主机 rpc:remot ...

  7. 用urllib库几行代码实现最简单爬虫

    """ 使用urllib.request()请求一个网页内容,并且把内容打印出来. """ from urllib import reque ...

  8. Python基础之变量、输入、输出

    一.Hello World程序 在Windows终端执行python C:\Users\renyz02>python Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 2 ...

  9. 任务相关的API函数-uxTaskGetSystemState

    uxTaskGetSystemState:此函数用于获取系统中所有的任务状态,每个任务的状态信息保存在一个TaskStatus_t类型的结构体里面.要使用此函数必须把 configUSE_TRACE_ ...

  10. 3 当某个应用的CPU使用达到100%,该怎么办?

    你最常用什么指标来描述系统的 CPU 性能呢?我想你的答案,可能不是平均负载,也不是 CPU 上下文切换,而是另一个更直观的指标-- CPU 使用率.CPU 使用率是单位时间内 CPU 使用情况的统计 ...