POJ1113 Wall

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1113

思路:求出凸包加上一个圆的周长,第一道凸包题

 

 

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class Main {
  5.     class Point {
  6.         int x;
  7.         int y;
  8.         public Point(int x, int y) {
  9.             this.x = x;
  10.             this.y = y;
  11.         }
  12.     }
  13.     public static void main(String[] args) throws IOException {
  14.         Main main = new Main();
  15.         BufferedReader read = new BufferedReader(new InputStreamReader(
  16.                 System.in));
  17.         String[] s;
  18.         s = read.readLine().split(" ");
  19.         int n = Integer.parseInt(s[0]);
  20.         int l = Integer.parseInt(s[1]);
  21.         int x, y;
  22.         Point[] p = new Point[n];
  23.         for (int i = 0; i < n; i++) {
  24.             s = read.readLine().split(" ");
  25.             x = Integer.parseInt(s[0]);
  26.             y = Integer.parseInt(s[1]);
  27.             p[i] = main.new Point(x, y);
  28.         }
  29.         Point[] ch = new Point[n];
  30.         int len = 0;
  31.         if (n >= 3) {
  32.             len = Graham_scan(p, ch, n);
  33.         }
  34.         double sum = 0;
  35.         for (int i = 0; i < len - 1; i++) {
  36.             sum += distance(ch[i], ch[i + 1]);
  37.         }
  38.         if (len > 1) {
  39.             sum += distance(ch[len - 1], ch[0]);
  40.         }
  41.         sum += 2 * l * Math.PI;
  42.         System.out.println(Math.round(sum));
  43.     }
  44.     public static double multiply(Point p1, Point p2, Point p0) {
  45.         return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
  46.     }
  47.     public static double distance(Point p1, Point p2) {
  48.         return (Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)
  49.                 * (p1.y - p2.y)));
  50.     }
  51.     public static int Graham_scan(Point[] PointSet, Point[] ch, int n) {
  52.         int i, j, k = 0, top = 2;
  53.         Point tmp;
  54.         for (i = 1; i < n; i++)
  55.             if ((PointSet[i].y < PointSet[k].y)
  56.                     || ((PointSet[i].y == PointSet[k].y) && (PointSet[i].x < PointSet[k].x)))
  57.                 k = i;
  58.         tmp = PointSet[0];
  59.         PointSet[0] = PointSet[k];
  60.         PointSet[k] = tmp;
  61.         for (i = 1; i < n - 1; i++) {
  62.             k = i;
  63.             for (j = i + 1; j < n; j++)
  64.                 if ((multiply(PointSet[j], PointSet[k], PointSet[0]) > 0)
  65.                         || ((multiply(PointSet[j], PointSet[k], PointSet[0]) == 0) && (distance(
  66.                                 PointSet[0], PointSet[j]) < distance(
  67.                                 PointSet[0], PointSet[k]))))
  68.                     k = j;
  69.             tmp = PointSet[i];
  70.             PointSet[i] = PointSet[k];
  71.             PointSet[k] = tmp;
  72.         }
  73.         ch[0] = PointSet[0];
  74.         ch[1] = PointSet[1];
  75.         ch[2] = PointSet[2];
  76.         for (i = 3; i < n; i++) {
  77.             while (top > 0 && multiply(PointSet[i], ch[top], ch[top - 1]) >= 0)
  78.                 top--;
  79.             ch[++top] = PointSet[i];
  80.         }
  81.         return top + 1;
  82.     }
  83. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值