UVa 152 Tree's a Crowd

来源:互联网 发布:三维动态仿真软件 编辑:程序博客网 时间:2024/06/05 16:49

这题做得好憋屈啊,题本身很简单,可就是读不懂题。

蛋蛋的忧伤~

这道题读了好多遍啊,还是没读懂。

给出每棵树的三维坐标。

无耻的看了别人的解法,就是计算每棵树距离其最近的一棵树的距离,如果这个距离小于10则统计在一个数组里。


优化:看到别人对x进行排序,然后如果两棵树的x坐标之差大于10的话,跳出循环。

 Tree's a Crowd 

Dr William Larch, noted plant psychologist and inventor of thephrase ``Think like a tree--Think Fig'' has invented a new classificationsystem for trees. This is a complicated system involving a series ofmeasurements which are then combined to produce three numbers (in the range [0,255]) for any given tree. Thus each tree can be thought of as occupying a pointin a 3-dimensional space. Because of the nature of the process, measurementsfor a large sample of trees are likely to be spread fairly uniformly throughoutthe whole of the available space. However Dr Larch is convinced that there arerelationships to be found between close neighbours in this space. To test thishypothesis, he needs a histogram of the numbers of trees that have closest neighboursthat lie within certain distance ranges.

Write a program that will read in the parameters of up to 5000trees and determine how many of them have closest neighbours that are less than1 unit away, how many with closest neighbours 1 or more but less than 2 unitsaway, and so on up to those with closest neighbours 9 or more but less than 10units away. Thus if   is the distancebetween the i'th point and its nearest neighbour(s) and   , with j and k integers and k = j+1,then this point (tree) will contribute 1 to the j'th bin in the histogram(counting from zero). For example, if there were only two points 1.414 unitsapart, then the histogram would be 0, 2, 0, 0, 0, 0, 0, 0, 0, 0.

Inputand Output

Input will consist of a series of lines, each line consisting of 3numbers in the range [0, 255]. The file will be terminated by a line consistingof three zeroes.

Output will consist of a single line containing the 10 numbersrepresenting the desired counts, each number right justified in a field ofwidth 4.

Sampleinput

10 10 0
10 10 0
10 10 1
10 10 3
10 10 6
10 10 10
10 10 15
10 10 21
10 10 28
10 10 36
10 10 45
0 0 0

Sampleoutput

   2   1   1   1   1   1   1   1   1   1

AC代码:

//#define LOCAL#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define sqr(x) (x)*(x)const int maxn = 5000 + 5;struct Point{int x, y, z;}point[maxn];int main(void){#ifdef LOCALfreopen("152in.txt", "r", stdin);#endifint histogram[15] = {0};int i, cnt = 0, j;int a, b, c;while(scanf("%d %d %d", &a, &b, &c) == 3){if((a + b + c) > 0){point[cnt].x = a;point[cnt].y = b;point[cnt].z = c;++cnt;}}for(i = 0; i < cnt; ++i){int tmin = 10;for(j = 0; j < cnt; ++j){if(i == j)continue;if((point[j].x - point[j].x) > 10)break;int temp = (int)sqrt(sqr(point[i].x - point[j].x) + sqr(point[i].y - point[j].y) + sqr(point[i].z - point[j].z));tmin = min(tmin, temp);}++histogram[tmin];}for(i = 0; i < 10; ++i)printf("%4d", histogram[i]);printf("\n");return 0;}


0 0