Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Tree's a Crowd

152.c

/* 150 - Tree's a Crowd */
/* Thu Jun 10 18:14:47 CEST 2010 */

#include <stdio.h>

int ntrees;
int trees[5010][3];
int closest[5010];
int distances[10];

int
dist(int a, int b) {
	int d1, d2, d3;
	d1 = trees[a][0]-trees[b][0];
	d2 = trees[a][1]-trees[b][1];
	d3 = trees[a][2]-trees[b][2];

	return d1*d1 + d2*d2 + d3*d3;
}

int
main(void) {
	int i,j;
	int a,b,c;

	for (i=0; ; i++) {
		scanf("%d %d %d", &a, &b, &c);
		if (a!=0 || b!=0 || c!=0) {
			trees[ntrees][0] = a;
			trees[ntrees][1] = b;
			trees[ntrees][2] = c;
			ntrees++;
		} else {
			break;
		}
	}
	for (i=0; i<ntrees; i++) {
		closest[i] = 256*256*256;
	}
#if DEBUG
	printf("%d trees\n", ntrees);
#endif
	for (i=0; i<ntrees-1; i++) {
		for (j=i+1; j<ntrees; j++) {
			int d = dist(i,j);
			if (d < closest[i]) {
				closest[i] = d;
			}
			if (d < closest[j]) {
				closest[j] = d;
			}
#if DEBUG
			printf("dist(%d,%d) = %d\n", i,j,d);
#endif
		}
	}
	for (i=0; i<ntrees; i++) {
#if DEBUG
		printf("closest(%d) = %d\n", i, closest[i]);
#endif
		for (j=1; j<=10; j++) {
			if (closest[i] < j*j) {
				distances[j-1]++;
				break;
			}
		}
	}
	for (i=0; i<10; i++) {
		printf("%4d", distances[i]);
	}
	printf("\n");
	return 0;
}