ZOJ 1450(最小圆覆盖)

题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364949

上交NB!

代码:

# define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
using namespace std;
const double eps = 1e-10;

struct Point { double x, y; Point(double x = 0, double y = 0) :x(x), y(y) {} };
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
bool operator < (const Point& A, const Point& B) { return A.x < B.x || (A.x == B.x && A.y < B.y); }
int dcmp(double x) { if (fabs(x) < eps)return 0; else return x < 0 ? -1 : 1; }
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }


void circle_center(Point p0, Point p1, Point p2, Point& cp) {
  double a1 = p1.x - p0.x, b1 = p1.y - p0.y, c1 = (a1 * a1 + b1 * b1) / 2;
  double a2 = p2.x - p0.x, b2 = p2.y - p0.y, c2 = (a2 * a2 + b2 * b2) / 2;
  double d = a1 * b2 - a2 * b1;
  cp.x = p0.x + (c1 * b2 - c2 * b1) / d;
  cp.y = p0.y + (a1 * c2 - a2 * c1) / d;
}
void circle_center(Point p0, Point p1, Point& cp) {
  cp.x = (p0.x + p1.x) / 2;
  cp.y = (p0.y + p1.y) / 2;
}
bool point_in(const Point& p, Point& center, double& radious) {
  return dcmp(Length(p - center) - radious) < 0;
}

void min_circle_cover(vector& p, int n, Point& center, double& radious) {
  radious = 0;
  center = p[0];
  for (int i = 1; i < n; i++) {
    if (!point_in(p[i], center, radious)) {
      center = p[i];
      radious = 0;
      for (int j = 0; j < i; j++)
        if (!point_in(p[j], center, radious)) {
          circle_center(p[i], p[j], center);
          radious = Length(p[j] - center);
          for (int k = 0; k < j; k++) {
            if (!point_in(p[k], center, radious)) {
              circle_center(p[i], p[j], p[k], center);
              radious = Length(p[k] - center);
            }
          }
        }
    }
  }
}

signed main() {
#ifdef ONLINE_JUDGE
#else
  freopen("Data.txt", "r", stdin);
#endif
  int n;
  Point center;//最小覆盖圆的圆心
  double radious;//最小覆盖圆的半径
  while (~scanf("%d", &n) && n) {
    vectorp;
    for (int i = 0; i < n; i++) {
      double x, y;
      scanf("%lf%lf", &x, &y);
      p.push_back(Point(x, y));
    }
    min_circle_cover(p, n, center, radious);
    printf("%.2f %.2f %.2f\n", center.x, center.y, radious);
  }
}

 

你可能感兴趣的:(0x72圆/球/圆锥,0x71二维几何基础)