POJ 2826 An easy problem?! 几何基础
2016-03-13 22:08
363 查看
#include <cstring> #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int T; struct Point { double x, y; Point(double x = 0, double y = 0): x(x), y(y) {} }; typedef Point Vector; //typedef vector<Point> Polygon; Point read_point() { double X, Y; scanf("%lf%lf", &X, &Y); return Point(X, Y); } 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); } const double eps = 1e-10; int dcmp(double x)// { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } bool operator ==(const Point &a, const Point &b)// { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } double Dot(Vector A, Vector B)// { return A.x * B.x + A.y * B.y; } double Length(Vector A)// { return sqrt(Dot(A, A)); } double Cross(Vector A, Vector B)// { return A.x * B.y - A.y * B.x; } double Angle(Vector A, Vector B)// { return acos(Dot(A, B) / Length(A) / Length(B)); } double TriArea(Point A, Point B, Point C) // { return fabs(Cross(B - A, C - A)) / 2; } bool SegmentIntersection(Point a1, Point a2, Point b1, Point b2) { double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), c3 = Cross(b2 - a1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); return max(a1.x, a2.x) >= min(b1.x, b2.x) && max(b1.x, b2.x) >= min(a1.x, a2.x) && max(a1.y, a2.y) >= min(b1.y, b2.y) && max(b1.y, b2.y) >= min(a1.y, a2.y) && dcmp(c1) * dcmp(c2) <= 0 && dcmp(c3) * dcmp(c4) <= 0; } Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) // { Vector u = P - Q; double t = Cross(w, u) / Cross(v, w); return P + v * t; } int main(int argc, char const *argv[]) { scanf("%d", &T); while (T--) { Point A1 = read_point(), A2 = read_point(), B1 = read_point(), B2 = read_point(); if (dcmp(A1.y - A2.y) < 0) swap(A1, A2); if (dcmp(B1.y - B2.y) < 0) swap(B1, B2); if (dcmp(A1.y - A2.y) == 0 || dcmp(B1.y - B2.y) == 0 || SegmentIntersection(A1, A2, B1, B2) == false || SegmentIntersection(A1, Point(A1.x, 100000), B1, B2) || SegmentIntersection(B1, Point(B1.x, 100000), A1, A2)) printf("0.00\n"); else { Point P = GetLineIntersection(A1, A2 - A1, B1, B2 - B1); Point P1 = GetLineIntersection(A1, A2 - A1, Point(1000000, B1.y), B1 - Point(1000000, B1.y)); Point P2 = GetLineIntersection(B1, B2 - B1, Point(1000000, A1.y), A1 - Point(1000000, A1.y)); printf("%.2lf\n", min(TriArea(B1, P, P1), TriArea(A1, P, P2)) + eps); } } return 0; }
感觉就是一个字:妙!
写完就交,秒过,就一个字,爽~~~~!
虽然交了两次ce,还有两次输出了中间结果。。。。。
这是一道非常重要的几何基础题,考验对精度控制的理解(dcmp,EPS),对模板的熟悉程度。
还是建议采用刘汝佳的模板,他的模板才是经得起考验的,值得信赖的,虽然少了一些函数,自己修改就好啦。
相关文章推荐
- 链表中倒数第k个节点
- 【bzoj3039】玉蟾宫
- 搜索题,留着以后慢慢刷
- OC中委托和协议
- 画外因 | 笛卡尔与瑞典女王在 “数学课”上谈人生
- java dya01 HelloWorld与环境变量
- Unix高级环境编程—进程控制(一)
- 信息系统项目管理师第二节课重点
- MVC架构的缺点
- jsnunumber
- 二叉查找树的各种操作
- leetcode 76. Minimum Window Substring
- 【bzoj3039】玉蟾宫 悬线法(单调栈)
- hibernate 二级缓存
- 对象概念理解
- test
- jeesite自定义主题
- CentOS 6.5 生产环境编译安装LNMP
- 高德切换城市
- 经典算法学习_动态规划_最长公共子序列