码迷,mamicode.com
首页 > 其他好文 > 详细

Snapchat - 小车通过雷达区域

时间:2016-10-17 09:06:37      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

有一条路,路两边可以想象为 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     }

 

Snapchat - 小车通过雷达区域

标签:

原文地址:http://www.cnblogs.com/warmland/p/5968446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!