暴力解法加简单的剪枝
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#define TREE_NUM 5005
struct node
{
double x;
double y;
double z;
};
double node_length(struct node *p)
{
return sqrt(p->x*p->x+p->y*p->y+p->z*p->z);
}
struct node tree[TREE_NUM];
int result[10];
double get_length(struct node *a, struct node *b)
{
return sqrt(pow(a->x-b->x,2.0) + pow(a->y-b->y,2.0) + pow(a->z-b->z,2.0));
}
double get_min_len(int index, int len)
{
double min_len = 50000.0;
double length;
int i;
for(i=0; i<len; i++)
{
if(i == index)
continue;
if
(
fabs(tree[i].x-tree[index].x) >= min_len ||
fabs(tree[i].y-tree[index].y) >= min_len ||
fabs(tree[i].z-tree[index].z) >= min_len
)
continue;
length = get_length(tree+i, tree+index);
if(min_len > length)
min_len = length;
}
return min_len;
}
void func(int len)
{
int i;
double min_len;
for(i=0; i<len; i++)
{
min_len = get_min_len(i, len);
//printf("min_len=%lf\n", min_len);
if(floor(min_len)>=0 && floor(min_len)<=9)
result[(int)floor(min_len)] ++;
}
for(i=0; i<=9; i++)
printf("%4d", result[i]);
printf("\n");
}
int main(void)
{
int i;
double x, y, z;
i = 0;
while(1)
{
scanf("%lf %lf %lf", &x, &y, &z);
if(!x && !y && !z)
break;
tree[i].x = x;
tree[i].y = y;
tree[i].z = z;
i++;
}
func(i);
return 0;
}