ACM 算法艺术与信息学竞赛 1.2.1盒子里面的汽球

原创 2011年10月24日 21:58:42

盒子里面的汽球

题目大意我就不多说了,直看一下刘汝佳的书。这里给出链接,与我出错的总结

盒子里的汽球

Problem 1515 Balloons in a Box

Accept: 171    Submit: 919
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

You must write a program that simulates placing spherical balloons into a rectangular box.

The simulation scenario is as follows. Imagine that you are given a rectangular box and a set of points inside the box. Each point represents a position where you might place a balloon. To place a balloon at a point, center it at the point and inflate the balloon until it touches a side of the box or a previously placed balloon. You may use the points in any order you like, and need not use every point. Your objective is to place balloons in the box in an order that maximizes the total volume occupied by the balloons.

You are required to calculate the volume within the box that is not enclosed by the balloons.

All integer will be in [-1000, 1000].

Input

The input consists of several test cases. The first line of each test case contains a single integer n that indicates the number of points in the set (n ≤ 6). The second line contains three integers that represent the (x, y, z) integer coordinates of a corner of the box, and the third line contains the (x, y, z) integer coordinates of the opposite corner of the box. The next n lines of the test case contain three integers each, representing the (x, y, z) coordinates of the points in the set. The box has non-zero length in each dimension and its sides are parallel to the coordinate axes.

Output

For each test case print one line which indicates the volume of the box not occupied by balloons. Round the volume to the nearest integer.

Sample Input

2 0 0 0 10 10 10 3 3 3 7 7 7

Sample Output

774

Source

FZU 2007 ICPC Qualification Round I


出错总结

1、用GNU C 提交,但是加了//为注释

2、在查看别的点的时候,如果别的点的半径为0,可需要直接跳过

			for (j = 0; j < n; ++j)
			{
				iter = ret[j]; mind = INF;
				for (k = 0; k < n; ++k)
				{
					if (k == iter) continue;  ->改为 if (k == iter || radius[k] == 0) continue;
					d = D[iter][k] - radius[k];
					mind = min(d, mind);
				}
				radius[iter] = min(mind, defined[iter]);
			}
3、可能福州里面使用的编译器版本比较老,我用while语句会出错,但是改成for则没有问题。

void _get_number(int v, const int n)
{
	int idx = 1, now_value = n, iter = n, pos, cnt = -1, j;
	memset(rec, 0, sizeof(rec));
	memset(ret, 0, sizeof(ret));

	do 
	{
		rec[idx] = v % (idx + 1);
		v /= ++idx;
	} while (v);

	while (--iter && --now_value)  ->改为 for (iter = now_value = MAXN -1; iter > 0; --iter, --now_value)
	{
		pos = rec[iter];
		for (cnt = -1, j = n - 1; j >= 0 && cnt != pos; --j) cnt += 0 == ret[j];
		ret[j + 1] = now_value;
	}
}
4、此外,坐标值最好用double来表示。(让我错了无数次,郁闷)

5、四舍五入的处理用printf("%.0lf\n", V);可以实现四舍五入的效果。

附上代码吧,这里用的是枚举的思路来处理的。不知道有没有更好的思路

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

#define  MM 100
const double PI = 3.1415926535;
const double INF = 2147364735.0;
#define  min(a, b)  ((a) > (b) ? (b) : (a))
#define  max(a, b)  ((a) > (b) ? (a) : (b))
#define  abso(a) ((a) > 0 ? (a) : 0 - (a))
const int frac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

typedef struct _node
{
	double x, y, z;
}node;


node point[MM], from, to;
double D[MM][MM];
double R[MM];
int rec[MM];
int ret[MM];
void _get_number(const int v, const int MAXN)
{
	int idx = 1, iter = MAXN, pos, cont, j;
	int value = v;
	memset(rec, 0, sizeof(rec));
	memset(ret, 0, sizeof(ret));

	do
	{
		rec[idx] = value % (idx + 1);
		value /= ++idx;
	} while (value);

	for (iter = MAXN - 1; iter > 0; --iter)
	{
		pos = rec[iter];
		for (cont = -1, j = MAXN - 1; j >= 0; --j)
		{
			if (0 == ret[j])
			{
				++cont;
				if (pos == cont) { ret[j] = iter; break;}
			}
		}
	}
}

double _dist(int i, int j)
{
	double a = point[i].x - point[j].x;
	double b = point[i].y - point[j].y;
	double c = point[i].z - point[j].z;
	return sqrt(a * a + b * b + c * c);
}

double _min_dist(int i)
{
	double t;
	double f = point[i].x - from.x, s = to.x - point[i].x; 
	f = abso(f); s = abso(s); t = min(f, s);

	f = point[i].y - from.y; s = to.y - point[i].y; 
	f = abso(f); s = abso(s); t = min(f, t); t = min(s, t);

	f = point[i].z - from.z; s = to.z - point[i].z; 
	f = abso(f); s = abso(s); t = min(f, t); t = min(s, t);

	return t;
}


int main(void)
{
	int n, i, j, k, next, iter;
	double V, minR, sumV, maxV, d;
	while (scanf("%d", &n) != EOF)
	{
		scanf("%lf%lf%lf%lf%lf%lf", &(from.x), &(from.y), &(from.z), &(to.x), &(to.y), &(to.z));
		V = (to.x - from.x) * (to.y - from.y) * (to.z - from.z); V = abso(V);

		for (i = 0; i < n; ++i) scanf("%lf%lf%lf", &(point[i].x), &(point[i].y), &(point[i].z));
		for (i = 0; i < n; ++i) 
			for (D[i][i] = _min_dist(i), j = i + 1; j < n; ++j) 
				D[i][j] = D[j][i] = _dist(i, j);

		for (maxV = next = 0; next < frac[n]; ++next)
		{
			memset(R, 0, sizeof(R));
			_get_number(next, n);

			for (sumV = i = 0; i < n; ++i)
			{
				iter = ret[i]; minR = INF;
				for (k = 0; k < n; ++k)
				{
					if (k != iter && R[k] > 0)
					{
						d = D[iter][k] - R[k];
						if (d <= 0) { minR = 0; break;}
						else minR = min(minR, d);
					}
				}
				minR = min(minR, D[iter][iter]); R[iter] = minR;
				sumV += 4.0 * PI * minR * minR * minR / 3.0;
			}
			maxV = max(maxV, sumV);
		}
		printf("%.0lf\n", V - maxV);
	}
	return 0;
}


【ACM训练计划】 《算法艺术与信息学竞赛》题目出处 (POJ等)

一.动态规划 参考资料: 刘汝佳《算法艺术与信息学竞赛》 《算法导论》 推荐题目: http://acm.pku.edu.cn/JudgeOnline/problem?id=1141 简单 ht...
  • synapse7
  • synapse7
  • 2013-11-23 11:03:52
  • 1710

算法艺术与信息学竞赛——习题解答

第二章  数学方法与常见模型 默认情况下,每个题目都有三个链接,第一个是原题,第二个是oj,第三个是解题报告。也有很多情况下原题是找不到的。 2.1.1 序列  http://www.oi...
  • Balloons2012
  • Balloons2012
  • 2012-08-07 09:36:32
  • 2177

算法艺术与信息学竞赛 PDF + PPT

  • 2014年02月07日 15:03
  • 33.83MB
  • 下载

acm算法黑书两本:实用算法的分析与程序设计 + 算法艺术与信息学竞赛

  • 2009年04月15日 13:31
  • 27.85MB
  • 下载

算法艺术与信息学竞赛

  • 2013年07月24日 11:27
  • 1.91MB
  • 下载

算法艺术与信息学竞赛 之 动态规划入门

算法艺术与信息学竞赛
  • ljyfearless
  • ljyfearless
  • 2018-01-20 16:28:01
  • 88

传说中搞ACM的必备黑书《算法艺术与信息学竞赛》

  • 2010年07月25日 22:49
  • 10.73MB
  • 下载

刘汝佳 算法艺术与信息学竞赛 入门 黑书白书等 合集

  • 2017年08月10日 14:50
  • 41.92MB
  • 下载

刘汝佳 算法艺术和信息学竞赛 课件 acm 最大流

  • 2010年05月28日 17:14
  • 1.11MB
  • 下载

刘汝佳 黑书课件 acm 好资源

  • 2010年05月28日 17:12
  • 360KB
  • 下载
收藏助手
不良信息举报
您举报文章:ACM 算法艺术与信息学竞赛 1.2.1盒子里面的汽球
举报原因:
原因补充:

(最多只允许输入30个字)