2021年度训练联盟热身训练赛第二场 The Eternal Quest for Caffeine

The Eternal Quest for Caffeine

Like most engineering students, Jason relies on caffeine, soda in particular, to get him through long nights in the lab. He’s not too picky about the brand; as long as it’s fizzy, loaded with caffeine, and not the “diet” version, he’s happy. When the new Harris Engineering Center opened on the UCF Campus, he was pleased to see a soda machine on every floor. Not just any soda machine, either. These machines are the fancy kind that display all of the soda stock arranged in rows stacked on top of each other (like most snack machines). When a soda is purchased, a conveyor belt rises to the correct row, where the soda is surgically picked up by a robotic clasp that can travel left and right on the conveyor. The conveyor then descends to the vending slot, where the clasp gently deposits the soda. Finally, the slot unlocks and tilts forward, allowing the buyer to retrieve his or her soda. Engineering perfection! And as a bonus, the soda isn’t subjected to all the usual mechanical clatter that causes it to fizz over when it’s opened.

Unfortunately, these elaborate machines seem to have a propensity for component failure. On one soda mission from his lab, Jason discovered that the vending slot was broken on the machine on his floor, which prevented it from working altogether. He went to the next floor down and saw that that machine’s vending slot was fine, but the conveyor was broken. He went down to the ground floor and saw that that machine was in perfect order, but only had caffeine free diet soda! At this point, Jason devised a plan. It’s a simple matter for him to open up the working machine and harvest the parts he needs for the machine upstairs, then to hike back upstairs and repair the machine that houses the soda he needs. Sure, hecouldjust take the soda he wants while the machine is open, but what fun would that be?

The one issue with this plan is that while Jason does enjoy the engineering challenge, he hates the walking between various broken machines each time he goes to get a coke, so he’s asked you, a computer science student and fellow resident of the Harris Engineering Center to help. He can devise a way to monitor each machine in the building and ascertain what parts are working. He needs you to write a program that will allow him to get all the parts he needs from the various machines in the building, traveling up and down as few flights of stairs as possible (he doesn’t trust the elevators because he’s never been allowed to see how they work). Assume he can carry an unlimited number of parts. He also wants this algorithm to work for various kinds of coke machines and various buildings, in case the vendors decide to change out their machines one day, or the administration decides to relocate the EECS department again (you still can assume that there will always be exactly one coke machine on each floor).

The Problem:

Given the number of floors in the building, the number of parts required for a working machine, a description of the working parts in each machine in the building, and whether or not each machine has the desired kind of soda, determine the smallest number of floor changes required to

assemble a working machine that is already stocked with the desired soda. Jason will always start from his lab and return there after getting his soda.

输入描述:
There will be multiple sodamachine arrangements to process. Inputwill begin with three integers,N,F, andP(1 ≤N,F,P≤ 10), each separated by a singlespace with no leading or trailingspaces.Ndescribes the number of floors in the building,Findicates which floor Jason’s lab is on, andPindicates the number of different partsin each of the building’s soda machines.

On the nextNlines will be a set of integersfollowed by a single letter. Eachline describes the soda machine onone floor (starting with the ground floor, and proceeding upward in order). The characterson a line are separated by a single space, with no leading or trailing spaces.The first integers on each line will beS(0 ≤S≤P), indicating the number of workingparts in the machine.Sintegerswill follow, each indicating a working part in the machine (each of these integers will be unique and will bebetween 1 andP). Finally, there willbe a single character “Y” or “N”, where “Y” indicates that themachine has a kind of soda that Jason likes, and “N” indicates that it does not.

End of input will beindicated by a value of 0forN,F, andP. This case should not be processed.

输出描述:
For each soda machinearrangement, print the case number (starting with 1) and a single integer indicating the minimum number of timesJason will have to travel up or down a staircase to collect the parts he needs to repair a soda machine, get a sodathat he wants, and return to his lab. Ifthere is no way for Jason to get a soda, print “Impossible” instead of the integer. Leavea blank line after the outputfor each test case.

翻译

像大多数工科学生一样,杰森依靠咖啡因,尤其是苏打水来熬过实验室里的漫漫长夜。只要它是起泡的,富含咖啡因的,而不是“减肥”版本的,他就很开心。当新的哈里斯工程中心在UCF校区开放时,他很高兴地看到每个楼层都有一台汽水机。也不是普通的汽水贩卖机。这些机器很花哨,把所有的苏打水排成一排,摞在一起(像大多数零食机器一样)。当购买了一罐苏打水后,传送带就会上升到正确的位置,在那里,苏打水就会像手术一样由一个机器人扣在传送带上左右移动。然后传送带下降到售货口,在那里扣子轻轻地存放苏打水。最后,槽打开并向前倾斜,让买家取回他或她的苏打水。工程完美!另外,这种苏打水在打开时也不会像平常那样受到机械碰撞,产生嘶嘶声。

不幸的是,这些精密的机器似乎有部件故障的倾向。在他实验室的一次汽水任务中,杰森发现他地板上的售货机的自动售货槽坏了,导致它无法正常工作。他走到下一层,发现那台机器的自动售货口没问题,但传送带坏了。他走到一楼,看到那台机器完好无损,但只有不含咖啡因的无糖汽水!这时,杰森想出了一个计划。对他来说,打开工作的机器,收集他楼上机器需要的部件,然后爬回楼上,修理存放他需要的苏打水的机器是一件很简单的事情。当然,他可以在机器开着的时候拿着他想要的苏打水,但那又有什么乐趣呢?

这个计划的一个问题是,虽然杰森很享受工程挑战,但他讨厌每次去买可乐时在各种坏掉的机器之间走动,所以他请求你,一个计算机科学的学生和哈里斯工程中心的同事来帮助他。他可以设计出一种方法来监控大楼里的每台机器,并确定哪些部件在工作。他需要你写一个程序,让他得到他所需要的全部零部件,各种机器的旅行向上和向下的楼梯尽可能少(他不相信电梯因为他从未被允许看到它们是如何工作的)。假设他可以携带无限多个零件。他也希望这个算法为各种各样的可口可乐机器工作和各种建筑,以防供应商决定改变他们的机器一天,或政府决定再次搬迁EECS的部门(你还可以假设总是会有一个可口可乐机每层)。

存在的问题:

给定建筑物的楼层数量,一台工作机器所需的部件数量,对建筑物中每台机器的工作部件的描述,以及每台机器是否有所需的苏打水,确定所需的最小楼层更改数量

组装一台已经备好所需苏打水的工作机器。杰森总是从他的实验室开始,拿到苏打水后再回来。

输入描述:

将有多个sodamachine安排来处理。输入将以三个整数开始,N,F,和P(1≤N,F,P≤10),每一个由一个没有前导或尾部空格的单个空格分隔。描述了大楼的楼层数,指出了杰森的实验室在哪一层,并指出了大楼里每个汽水机不同部件的数量。

下一行是一组整数,后面跟着一个字母。Eachline描述了一层的饮水机(从一楼开始,依次向上)。一行中的字符由一个空格分隔,没有前导空格或尾随空格。每一行的第一个整数将是beS(0≤S≤P),表示机器中工作部件的数量。接下来是整数,每一个都表示机器的一个工作部分(这些整数都是唯一的,将在1和p之间)。最后,会有一个单独的字符“Y”或“N”,其中“Y”表示机器有Jason喜欢的汽水,“N”表示没有。

输入的结束将由一个值0forN,F,和p表示。这个案子不应该被处理。

输出描述:

为每个苏打machinearrangement,打印数量(从1开始)和一个整数表示timesJason必须旅行的最小数量上下楼梯收集部分他需要修复一个汽水机,sodathat他希望,回到他的实验室,如果杰森没有办法得到一个苏打水,打印“不可能”,而不是整数。在每个测试用例的输出之后留下一个空白行。

思路:用二进制模拟一下每一层的状态有某种零件x就将其1<<x)设置为1即可(苏打水也看作一种状态。
这几层能修好机器的状态就是走过的层数a[i]或运算结果为quanbu

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int a[20];				//位运算永远滴神
int quanbu;    //全集

int main()
{
	int cnt=1;
	int n,po,sum;
	while(cin>>n>>po>>sum)
	{
		if(n==0&&po==0&&sum==0)break;
		quanbu=0;
		memset(a,0,sizeof(a));
		for(int i=1;i<=n;i++)  //n层楼
		{
			int x,now;
			cin>>x;
			while(x--)
			{
				cin>>now;   //第now个零件
				a[i]+=(1<<now);  //零件加入状态
			}
			char c;
			cin>>c;
			if(c=='Y')a[i]+=(1<<(sum+1));  //是否有水加入状态

		}
		for(int i=1;i<=sum+1;i++)quanbu+=(1<<i); // 1-n个零件和有水一共sum+1个状态加进去(全集)
		int ans=inf;
		int no=0;
		for(int i=po;i<=n;i++)  //向上走当前层数为i
		{
			no|=a[i];     //向上走的状态
			int now=no;
			for(int j=po;j>=1;j--)  //向下走当前层数为j
			{
				now|=a[j];       //当前层加入已经走过的状态
				if(now==quanbu)      //当前等于全部
				{
					ans=min(ans,i-j);    //更新答案
					break;
				}
			}
		}
		if(ans<inf)printf("Test case #%d: %d\n",cnt++,ans*2);
		else printf("Test case #%d: Impossible\n",cnt++);
		cout<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值