POJ1017

Packets

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 61851 Accepted: 20957

Description

A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.

Input

The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.

Output

The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last ``null'' line of the input file.

Sample Input

0 0 4 0 0 1 
7 5 1 0 0 0 
0 0 0 0 0 0 

Sample Output

2 
1 

大致题意是把底面为不同大小正方形的等高长方体放进等高盒子里需要多少个盒子。

借鉴了一些优秀的经验,可以总结出:

首先放6*6的,每个需要一个盒子;

再放5*5和4*4的,每个同样需要一个盒子,分别可以剩下1*1和2*2的空间;

然后放3*3的,根据3*3的长方体数量的不同,剩余1*1和2*2的空间就不同;

把2*2的空间用2*2的长方体填充,多退少补,得到总共的1*1的空间;

用1*1的长方体填充1*1的空间,便可计算出总共需要的盒子数。

代码如下:

#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int street_count=0;
int juc[50][1996];
int mark[1996];
int degree[50];
stack<int> s;
void record(int a,int b,int c)
{
	juc[a][c]=b;
	juc[b][c]=a;
	degree[a]++;
	degree[b]++;
	street_count++;
}
void dfs(int j)
{
	for(int i=1;i<=street_count;i++)
	{
		if(juc[j][i]&&mark[i]==0)
		{
			mark[i]=1;
			dfs(juc[j][i]);
			s.push(i);
		}
	}
}
int main()
{
	while(1)
	{
		int a,b,c,start;
		scanf("%d%d",&a,&b);
		if(a==0&&b==0)
		{
			street_count=0;
			break;
		}
		memset(juc,0,sizeof(juc));
		memset(mark,0,sizeof(mark));
		memset(degree,0,sizeof(degree));
		while(!s.empty())
			s.pop();
		if(a<b)
			start=a;
		else
			start=b;
		scanf("%d",&c);		
		record(a,b,c);
		while(1)
		{
			scanf("%d%d",&a,&b);
			if(a==0&&b==0)
				break;
			scanf("%d",&c);
			record(a,b,c);
		}
		bool flag=true;
		for(int i=1;i<=44;i++)
		{
			if(degree[i]%2==1)
			{
				flag=false;
				break;
			}
		}
		if(flag==false)
			printf("Round trip does not exist.\n");
		else
		{
			dfs(start);
			while(!s.empty())
			{
				int tmp=s.top();
				printf("%d",tmp);
				s.pop();
				if(s.empty())
					printf("\n");
				else
					printf(" ");
			}
		}
		street_count=0;
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值