/** * Sample solution to Subway Planning * Author: Stein Norheim * * My first Java program ever!!! * * Geometry and Dynamic Programming. * * To get rid of floating point truncation problems, this * solution only uses integers, except for the initial * sorting on angles. */ import java.util.*; import java.io.*; class CPointComparator implements Comparator { public int compare(Object a, Object b) { double angleA = ((CPoint)a).angle; double angleB = ((CPoint)b).angle; if (angleA>angleB) return 1; if (angleAd*d; } int r2 () { return (x*x+y*y); } boolean overlaps(CPoint o) { if (!needsSubway() && !o.needsSubway()) return true; int templeft = x*o.x+y*o.y+d*d; if (templeft<0) return false; int leftside = templeft*templeft; int rightside = (r2()-d*d)*(o.r2()-d*d); return leftside >= rightside; } }; class DynTable { int[] a; int[] solution; CPoint[] p; boolean[][] overlaps; int n; DynTable (int dimension) { n = 0; a = new int [dimension]; solution = new int[dimension]; p = new CPoint[dimension]; overlaps = new boolean[dimension][dimension]; } void addPoint(CPoint newPoint) { p[n]=newPoint; n++; } void resetA() { for (int i=0; i=0) return a[current]; int i=current; int best; int lastincluded; if ((i+1)%n==starter) { best = 1; lastincluded = current; } else { lastincluded = i; i=(i+1)%n; best =1+getNeededLines(starter,i); // worst case! int candidate; while ((i!=starter) && (overlaps[current][i])) { if ((i+1)%n==starter) { best=1; lastincluded = i; break; } else { candidate = 1 + getNeededLines(starter,(i+1)%n); if (candidate