最痛苦的一周——第三周作业Collinear
这个作业已经有点儿久远了,但是给我留下了很深的印象。这五周的作业中,就这个最困难了。自己真的花了两天时间在这次作业上面。最后解题思路还是在Coursera讨论上看到的。
brute-force解法就不多说了,但还是踩了一些坑。
重点在FastCollinear的实现,回过头来发现只有一百多行代码真的不敢置信。这次作业是对sort的应用。按每个点和其它点形成的斜率,然后排序。所以如果有四点在一个线上,非常容易寻找。但是最困的是找出重复的线段和子线段。在论坛找到的思路是遍历按斜率排序好的队列,将几个相同的放到Stack中,将保存到Stack中的同线点排序,于是就找出并保存了最大线段。
还有一个坑是,因为循环写法的原因,这个点的判断在下一次加入到队列中。所以最后一个点没有加入进去。而垂直的线斜率最大,正好排在最后。因此所有垂直的线丢没有找出来。
BruteCollinearPoints.java
···
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import java.util.Arrays;
public class BruteCollinearPoints {
private LineSegment[] segments;
private int endpoint;
private int nsegments;public BruteCollinearPoints(Point[] pointss) {if(pointss == null)throw new IllegalArgumentException();for(int i=0; i<pointss.length; i++) {if(pointss[i] == null)throw new IllegalArgumentException();}for(int i=0; i<pointss.length; i++) {for(int j=0; j<pointss.length; j++) {if(i == j)continue;if(pointss[i].slopeTo(pointss[j]) == Double.NEGATIVE_INFINITY)throw new IllegalArgumentException();}}Point[] points = new Point[pointss.length];for(int i=0;i<points.length;i++) {points[i] = pointss[i];}Arrays.sort(points);segments = new LineSegment[points.length];endpoint = points.length - 1;try {outer:for(int p=0; p<points.length; p++) {for(int q=endpoint; q>p; q--) {double pq = points[p].slopeTo(points[q]);for(int r=q-1; r>p; r--) {double qr = points[q].slopeTo(points[r]);if(qr == pq)for(int s=r-1; s>p; s--) {double rs = points[r].slopeTo(points[s]);if(qr == rs) {segments[nsegments++] = new LineSegment(points[p], points[q]);//continue outer;}}}}}} catch (NullPointerException e) {throw new IllegalArgumentException();}
}
public int numberOfSegments() {return nsegments;
}
public LineSegment[] segments() {LineSegment[] lineSegements = new LineSegment[nsegments];for(int i=0; i<nsegments; i++) {lineSegements[i] = segments[i];}return lineSegements;
}public static void main(String[] args) {// read the n points from a fileIn in = new In(args[0]);int n = in.readInt();Point[] points = new Point[n];for (int i = 0; i < n; i++) {int x = in.readInt();int y = in.readInt();points[i] = new Point(x, y);}// draw the pointsStdDraw.enableDoubleBuffering();StdDraw.setXscale(0, 32768);StdDraw.setYscale(0, 32768);for (Point p : points) {p.draw();}StdDraw.show();// print and draw the line segmentsBruteCollinearPoints collinear = new BruteCollinearPoints(points);for (LineSegment segment : collinear.segments()) {StdOut.println(segment);segment.draw();}StdDraw.show();
}
}
···
FastCollinearPoints.java
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;import java.util.Arrays;public class BruteCollinearPoints {private LineSegment[] segments;private int endpoint;private int nsegments;public BruteCollinearPoints(Point[] pointss) {if(pointss == null)throw new IllegalArgumentException();for(int i=0; i<pointss.length; i++) {if(pointss[i] == null)throw new IllegalArgumentException();}for(int i=0; i<pointss.length; i++) {for(int j=0; j<pointss.length; j++) {if(i == j)continue;if(pointss[i].slopeTo(pointss[j]) == Double.NEGATIVE_INFINITY)throw new IllegalArgumentException();}}Point[] points = new Point[pointss.length];for(int i=0;i<points.length;i++) {points[i] = pointss[i];}Arrays.sort(points);segments = new LineSegment[points.length];endpoint = points.length - 1;try {outer:for(int p=0; p<points.length; p++) {for(int q=endpoint; q>p; q--) {double pq = points[p].slopeTo(points[q]);for(int r=q-1; r>p; r--) {double qr = points[q].slopeTo(points[r]);if(qr == pq)for(int s=r-1; s>p; s--) {double rs = points[r].slopeTo(points[s]);if(qr == rs) {segments[nsegments++] = new LineSegment(points[p], points[q]);//continue outer;}}}}}} catch (NullPointerException e) {throw new IllegalArgumentException();}}public int numberOfSegments() {return nsegments;}public LineSegment[] segments() {LineSegment[] lineSegements = new LineSegment[nsegments];for(int i=0; i<nsegments; i++) {lineSegements[i] = segments[i];}return lineSegements;}public static void main(String[] args) {// read the n points from a fileIn in = new In(args[0]);int n = in.readInt();Point[] points = new Point[n];for (int i = 0; i < n; i++) {int x = in.readInt();int y = in.readInt();points[i] = new Point(x, y);}// draw the pointsStdDraw.enableDoubleBuffering();StdDraw.setXscale(0, 32768);StdDraw.setYscale(0, 32768);for (Point p : points) {p.draw();}StdDraw.show();// print and draw the line segmentsBruteCollinearPoints collinear = new BruteCollinearPoints(points);for (LineSegment segment : collinear.segments()) {StdOut.println(segment);segment.draw();}StdDraw.show();}
}
转载于:https://www.cnblogs.com/hyper-xl/p/8507327.html
最痛苦的一周——第三周作业Collinear相关推荐
- python123《python语言程序设计》程序设计题第一周第二周第三周第四周第五周
第一周学习 eval定义 eval(expression,globals=None,locals=None) -expression:该参数是一个字符串,python会使用globals字典和loca ...
- “进度条”博客——第三周
"进度条"博客 --第三周 第三周 所花时间(包括上课) 上课时间:一周两节课,共3个小时左右 课下时间:周二晚上8::30到晚上10点左右(熟悉同伴.制定规范.搜集相关资料并构思 ...
- 花滑三周连跳_三周半+四周跳来势汹汹 花滑女单将迎难度巨变?
体坛+记者宫珂报道 在过去的两个赛季中,"四周跳大战"一直是贯穿花样滑冰男单赛场的主旋律,相比之下,女单赛场的技术突破却迟迟未能到来.即使强如保持近两个赛季不败的梅德韦杰娃和首战奥 ...
- 《软件工程之美》打卡第三周
这是笔者参加极客时间21天打卡行动第三周,三周的时间无间断刚好21天,这21天里我强迫自己每天都要学习半个小时并写100个字的分享,正是这样的自律让我找回以前的那种感觉,真的好久没这样认认真真做一件事 ...
- 提高班第三周周记(中秋第一天)
这是第三周,也是中秋节.没有月饼的中秋节.但我过得很充实.我很快乐,发自内心的. 中秋第一天早上贾琳师哥讲课.讲他为什么辞职来这里.他的确学成,有着令人羡慕的工资.可是他的问题让我触动. 提到了邓稼先 ...
- 清华贵系的期末大作业:奋战三周,造台计算机!
大数据文摘授权转载自AI科技评论 作者 | 蒋宝尚 编辑丨陈彩娴 本科大三,正在学习计算机组成原理,能做个什么项目? 清华大学贵系说:造台计算机吧! 清华有门本科三年级必修课,名为<计算机组成原 ...
- python能开发什么产品_三周学 Python ?不,三周做个产品
我的同事在看到毫无开发经验的我用三周时间,不但从零基础用上了 Python,还做出了一个客户关系管理系统,强烈邀请我分享经验.惶恐,因为我并没有出色的智商,也没有觉得三周学 Python 是一个体现自 ...
- [32期] 想学PHP来兄弟连是正确的选择 初识兄弟连三周
各位同学,大家好. 我是兄弟连第32期学员,陈文,我有一个用了近10年的个性签名:做一个有思想的好人,喝好咖啡,听好音乐,敏锐有活力,有激情爱探索,感染周围的人.这个签名表达了我积极进取的人生观,鼓励 ...
- python嵩天课堂笔记_[Python机器学习]强化学习笔记(嵩天礼欣老师mooc第三周)
[Python机器学习]强化学习笔记(嵩天礼欣老师mooc第三周) [Python机器学习]强化学习笔记(嵩天礼欣老师mooc第三周) 目录 强化学习 定义 马尔科夫决策过程 基本元素 值函数 最优值 ...
- 在首次发布三周之后,MLflow迎来了0.2版本
在今年的Spark+AI峰会上,MLflow团队推出了MLflow,一个开源的用于简化机器学习生命周期的平台.从首次发布到现在的三周时间里,已经有很多数据科学家和工程师对使用MLflow和为MLflo ...
最新文章
- 使用JackJSON 流式API 创建JSON串【学习记录】
- SAP PP使用ECR去修改Recipe主数据,报错:Generation not supported for change object
- orcale 非非等于_oracle 不等于1怎么查?
- 在featureDataset和workspace下創建featureclass
- Centos7常用命令[系统的关机、重启以及登出]
- mysql 字段 中文_如何配置mysql支持中文字段名与中文字段
- video4linux简介
- WebM VP8 SDK Usage/关于WebM VP8 SDK的用法
- numpy中两个array数值比较,在IDE中显示完全相同,但是bool判断两个array是否相等却返回False
- Windows遇到ERR_NETWORK_ACCESS_DENIED处理方案
- springboot细节挖掘(配置Swagger2)
- clang mingw
- NS-仿真实验--FTP
- PID控制器开发笔记(转)
- mysql char最大长度_MySQL中的CHAR和VARCHAR到底支持多长?
- JS 页面跳转,参数的传递
- Python面向对象编程:数据封装、继承和多态
- uniapp 添加table不显示
- MySQL数据库之分库分表方案
- wow插件补充说明篇