标签:
有一条路,路两边可以想象为 y = 0 和y = 1两条直线。现在给你list of radar,每个雷达为(横坐标,纵坐标,辐射半径)。问你一辆车能否通过这条路。
代码说话:
1 static class Radar { 2 double x; 3 double y; 4 double r; 5 public Radar(double x, double y, double r) { 6 this.x = x; 7 this.y = y; 8 this.r = r; 9 } 10 } 11 12 class Area { 13 List<Radar> radars; 14 double upperbound; 15 double lowerbound; 16 17 public Area() { 18 radars = new ArrayList<Radar>(); 19 upperbound = 0; 20 lowerbound = 1; 21 } 22 23 private boolean canMerge(Radar r1, Radar r2) { 24 return Math.pow(r1.r + r2.r, 2) >= Math.pow(r1.x - r2.x, 2) + Math.pow(r1.y - r2.y, 2); 25 } 26 27 private boolean merge(Radar r) { 28 for(Radar radar: radars) { 29 if(canMerge(r, radar)) { 30 upperbound = Math.max(upperbound, r.x + r.r); 31 lowerbound = Math.min(lowerbound, r.y - r.r); 32 radars.add(r); 33 return true; 34 } 35 } 36 return false; 37 } 38 39 private boolean cannotPass() { 40 return upperbound >= 1 && lowerbound <= 0; 41 } 42 } 43 44 45 public boolean canCarPass(List<Radar> radars) { 46 List<Area> area = new ArrayList<Area>(); 47 for(Radar radar: radars) { 48 boolean merged = false; 49 for(Area a: area) { 50 merged = a.merge(radar); 51 if(merged) { 52 break; 53 } 54 } 55 if(!merged) { 56 Area a = new Area(); 57 a.radars.add(radar); 58 area.add(a); 59 } 60 } 61 for(Area a : area) { 62 if(a.cannotPass()) { 63 return false; 64 } 65 } 66 return true; 67 } 68 69 public static void main(String[] args) { 70 RadarDetect rd = new RadarDetect(); 71 //List<Radar> radars = Arrays.asList(new Radar(1, 0.5, 0.49), new Radar(3, 1.5, 1), new Radar(3, 0.4, 0.3)); 72 List<Radar> radars = Arrays.asList(new Radar(1, 0, 1.5), new Radar(3, 1, 1.5)); 73 System.out.println(rd.canCarPass(radars)); 74 }
标签:
原文地址:http://www.cnblogs.com/warmland/p/5968446.html