poj 1039 Pipe

来源:互联网 发布:淘宝偷换宝贝处罚 编辑:程序博客网 时间:2024/05/01 03:36
#include <iostream>#include <cmath>#include <algorithm>#include <iomanip>using namespace std;const double precision = 1e-3, inf = 99999.0;struct Point{double x, y;};inline int dblcmp(double p){if (fabs(p) < precision) return 0;return p > 0 ? 1 : -1;}inline double det(double x1, double y1, double x2, double y2){return x1 * y2 - x2 * y1;}inline double cross(Point A, Point B, Point P){return det(B.x - A.x, B.y - A.y, P.x - A.x, P.y - A.y);}inline bool check(Point A, Point B, Point C, Point D){return (dblcmp(cross(A, B, C)) * dblcmp(cross(A, B, D)) <= 0);}inline double intersection(Point A, Point B, Point C, Point D){double area1 = cross(A, B, C), area2 = cross(A, B, D);int c = dblcmp(area1), d = dblcmp(area2);if (c*d<0) return (area2*C.x - area1*D.x)/(area2 - area1);if (c*d == 0)    {        if (c == 0) return C.x;else return D.x;    }return -inf;}int main(void){int n;while(cin >> n && n){if (!n) break;Point* up = new Point[n + 1];Point* down = new Point[n + 1];double max_x = - inf;for (int i = 1; i <= n; i++){cin >> up[i].x >> up[i].y;down[i].x = up[i].x;down[i].y = up[i].y - 1;}bool flag = 0;for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++)            {                if (i != j){    int k;for (k = 1; k <= n; k++) if (!check(up[i], down[j], up[k], down[k])) break;if (k > n){flag = 1;break;}else if (k > max(i, j)){double temp = intersection(up[i], down[j], up[k], up[k - 1]);if (max_x < temp) max_x = temp;temp = intersection(up[i], down[j], down[k], down[k - 1]);if (max_x < temp) max_x = temp;}}            }if (flag) break;}if (flag) cout << "Through all the pipe." << endl;        else cout << fixed << setprecision(2) << max_x << endl;delete up, down;}return 0;}

0 0
原创粉丝点击