Assignment 1-3 :Pattern Recognition

题目描述

大概意思就是让你找出包含四个点以上的直线。

Point API 的实现略(因为不熟悉 Java 的 Comparator,是对着课件的模板写的。。。)

Brute force 要求:The order of growth of the running time of your program should be N4 in the worst case and it should use space proportional to N plus the number of line segments returned.  And will not supply any input to BruteCollinearPoints that has 5 or more collinear points.  

因为输入中不会出现5点一线的情况,因此该API的实现大大简化了。

想法:4个循环,每个循环的初值都是前一个循环的值+1。在每个循环中检测斜率,如果两两之间斜率相等,那么就成立。但实现后发现线段长度比他提供的样例输出短,想到我们没有正确辨识出端点。因此在我们的循环中不断更新两个变量 Point 类的变量 min,max,其值为四个端点中的最大最小点(即为线段的端点)。

 

FastCollinearPoints :The order of growth of the running time of your program should be N2 log N in the worst case and it should use space proportional to N plus the number of line segments returned. FastCollinearPoints should work properly even if the input has 5 or more collinear points.   

根据提示,运用排序简化。但面临一个问题:在组成线段的点大于四个时,我们如何判断出两个点是线段的端点?思想是一样的,就是找到线段的最小,最大两点(即两个端点)。但由于组成线段的点可能大于四个,因此我们要稍微改变下策略。我们可以先复制一个 points 数组,然后在其中将 point 根据大小排序。然后在我们寻找线段过程中可以引入这样一个测试来检测是否为两个端点:如果在斜率相同的点间,有点小于比价的点,那么这个点肯定就不可能是端点(出现这种情况表示,那么更小的点,更适合当端点)。大概方法是这样,但还要处理其他细节(那个相同的点怎么办? 开始不知道线段的多少,因此该如何存储线段?)

具体代码见 github

 

 

 

2015-10-03 

posted @ 2015-10-03 01:33  whu.yt  阅读(447)  评论(0编辑  收藏  举报