POJ 1328 Java實現

來源:互聯網
上載者:User

標籤:

本題借鑒了別人的做題思想,主要如下
/**
 *對要掃描到的每個島求出雷達可以安置在海岸上的區間[left,right],  *對這些區間按left從小到大排序
 *從第一個區間的right點開始安置雷達,然後依次檢索下一個區間,
 *若下一個區間完全在剛剛安置雷達的區間內(new.left>=pre.left &&
 * new.right<=pre.right ),則將剛剛安置的雷達重新安置在這個區間(new)的右端;  *若下一個區間的左端點在剛剛安置雷達的區間內,而右端點在剛剛安置雷達的  *區間右側,則可忽略這個區間向下進行;
 *若下一個區間與剛剛安置雷達的區間完全不相交,則在這個新區間右端內安置  *一個新雷達 */
第一次提交沒有通過,顯示Runtime_error,測試了很多資料沒有錯誤,後來吧數組大小改為1000,順利通過.

 1 public class Point implements Comparable<Point> { 2  3     public Point() { 4         // TODO Auto-generated constructor stub 5  6     } 7  8     public double left; 9     public double right;10 11     private void setLeft(double left) {12         this.left = left;13     }14 15     private void setRight(double right) {16         this.right = right;17     }18 19     @Override20     public int compareTo(Point o)// 實現介面21     {22         if (o != null && o instanceof Point) {23             Point p = (Point) o;24             if (left < p.left)25                 return -1;26             else if (left == p.left)27                 return 0;28             else29                 return 1;30         } else31             return -1;32     }33 34     public static void main(String[] args) {35         // TODO Auto-generated method stub36         int n, d; // 島嶼個數和雷達偵測距離37         Point[] poi = new Point[100]; // 儲存每個島嶼38         Scanner sc = new Scanner(System.in);39         int num = 1; // case個數40         int[] casenum = new int[100]; // 每個case的所需雷達數41         n = sc.nextInt();42         d = sc.nextInt();43         while (n != 0 && d != 0) {44             boolean legal = true;     //判斷一個case是否合法,即能否被雷達覆蓋45             int x, y;46             for (int i = 0; i < n; i++) {47                 x = Integer.parseInt(sc.next());48                 y = Integer.parseInt(sc.next());49                 poi[i] = new Point();50                 // System.out.println(x-Math.sqrt(d*d-y*y));51                 // double temp1=x-Math.sqrt(d*d-y*y);52                 poi[i].setLeft(x - Math.sqrt(d * d - y * y));53                 poi[i].setRight(x + Math.sqrt(d * d - y * y));54                 if (y > d)55                     legal = false;56             }57             Arrays.sort(poi, 0, n);   //用到了上面重寫的compareTO58             if (!legal) {59                 // System.out.println("Case "+num+": -1");60                 casenum[num - 1] = -1;61                 num++;62             } else {63                 double pre = poi[0].right;64                 int radar_num = 1;65                 for (int i = 1; i < n; i++) {66                     if (poi[i].left > pre) {67                         radar_num++;68                         pre = poi[i].right;69                     } else if (poi[i].right < pre) {70                         pre = poi[i].right;71                     }72                 }73                 // System.out.println("Case "+num+": "+radar_num);74                 casenum[num - 1] = radar_num;75                 num++;76             }77             n = sc.nextInt();78             d = sc.nextInt();79 80         }81         num--;82         for (int i = 0; i < num; i++) {83             int j = i + 1;84             System.out.println("Case " + j + ": " + casenum[i]);85         }86 87     }88 89 }
View Code

 

POJ 1328 Java實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.