hdu1417 (求相交立方体的体积)

Problem Description

As chief engineer of the Starship Interprize, the task of repairing the hyperstellar, cubic, transwarped-out software has fallen on your shoulders. Simply put, you must compute the volume of the intersection of anywhere from 2 to 1000 cubes.

Input

The input data file consists of several sets of cubes for which the volume of their intersections must be computed. The first line of the data file contains a number (from 2 to 1000) which indicates the number of cubes which follow, one cube per line. Each line which describes a cube contains four integers. The first three integers are the x, y, and z coordinates of the corner of a cube, and the fourth integer is the positive distance which the cube extends in each of the three directions (parallel to the x, y, and z axes) from that corner.

Output

Following the data for the first set of cubes will be a number which indicates how many cubes are in a second set, followed by the cube descriptions for the second set, again one per line. Following this will be a third set, and so on. Your program should continue to process sets of cubes, outputting the volume of their intersections to the output file, one set per line, until a zero is read for the number of cubes.

Note that the data file will always contain at least one set of cubes, and every set will contain at least 2 and at most 1000 cubes. For any given set of cubes, the volume of their intersections will not exceed 1,000,000 units.
这个题题意看懂了,然后明白其中的道理就很好写。一开始我就很懵逼,不知道从何下手。画了画之后就明白了。这种思路很常用,一边求最小值一边求最大值。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=1e4+10;
struct node{
	int x;
	int y;
	int z;
	int l;
}p[maxx];
int n;

int main()
{
	while(cin>>n,n)
	{
		for(int i=0;i<n;i++) 
		cin>>p[i].x>>p[i].y>>p[i].z>>p[i].l;
		int x1=-inf;
		int x2=inf;
		int y1=-inf;
		int y2=inf;
		int z1=-inf;
		int z2=inf;
		for(int i=0;i<n;i++)
		{
			x1=max(x1,p[i].x);//求最小值x 
			x2=min(x2,p[i].x+p[i].l);//求最大值x 
			y1=max(y1,p[i].y);//求最小值y 
			y2=min(y2,p[i].y+p[i].l);//求最大值y 
			z1=max(z1,p[i].z);//求最小值z 
			z2=min(z2,p[i].z+p[i].l);//求最大值z 
		}
		if(x1>=x2||y1>=y2||z1>=z2) cout<<"0"<<endl;
		else cout<<(x2-x1)*(y2-y1)*(z2-z1)<<endl;
	}
	return 0;
}

注明一下,这里所说的求最大值x(y,z),是要求一开始的时候x(y,z)的最大值,因为是求重合立方体的体积。求最小值的时候也是如此。画一下图就可以了。
努力加油a题啊,ヾ(◍°∇°◍)ノ゙

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值