uva 10084

题意:在一个矩形空间内,左下角坐标(0,0),右上角坐标(10,10),然后两个小孩子玩游戏,乙让甲猜一个物体的位置,初始必须猜(0,0),然后之后甲说出的位置,乙都会根据上一次猜的位置和这一次猜的位置距离正确位置的远近给出一个答案,Colder说明这次猜远了,Hotter说明猜近了,Same说明猜的是正确位置。输出每次询问后,所有可能位置占的总面积。

题解:用切割多边形的方式求半平面交多边形面积。当前的点和之前的点的中点找到,然后之前点到当前点的有向连线逆时针旋转90°得到切割向量,中点加切割向量是另一个点,根据这两个点确定有向直线,如果Colder半平面交取左边,Hotter取右边,Same后面积都是0表示已找到。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1);

int dcmp(double x) {
    if (fabs(x) < eps)
        return 0;
    return x > 0 ? 1 : -1;
}
struct Point {
    double x, y;
    Point (double a = 0, double b = 0): x(a), y(b) {}
};
typedef Point Vector;
typedef vector<Point> Polygon;

Vector operator + (const Vector& a, const Vector& b) { return Vector(a.x + b.x, a.y + b.y); }
Vector operator - (const Vector& a, const Vector& b) { return Vector(a.x - b.x, a.y - b.y); }
Vector operator * (const Vector& a, double b) { return Vector(a.x * b, a.y * b); }
Vector operator / (const Vector& a, double b) { return Vector(a.x / b, a.y / b); }
bool operator == (const Vector& a, const Vector& b) { return !dcmp(a.x - b.x) && !dcmp(a.y - b.y); }
bool operator < (const Vector& a, const Vector& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
double Dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }
double Length(const Vector& a) { return sqrt(Dot(a, a)); }
double Cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }
double Angle(const Vector& a, const Vector& b) { return acos(Dot(a, b) / Length(a) / Length(b)); }
//向量A旋转rad弧度,rad负值为顺时针旋转
Point Rotate(Point A, double rad) {
    return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
double PolygonArea(Polygon& res, int m) {
    double area = 0;
    for (int i = 1; i < m - 1; i++)
        area += Cross(res[i] - res[0], res[i + 1] - res[0]);
    return area / 2;
}

Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
    Point u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v * t;
}

bool OnSegment(const Point& p, const Point& a1, const Point& a2) {
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

//切割多边形,返回有向直线A->B左边的多边形
Polygon CutPolygon(Polygon poly, Point A, Point B) {
    Polygon newpoly;
    int n = poly.size();
    for (int i = 0; i < n; i++) {
        Point C = poly[i];
        Point D = poly[(i + 1) % n];
        if (dcmp(Cross(B - A, C - A)) >= 0)
            newpoly.push_back(C);
        if (dcmp(Cross(B - A, C - D)) != 0) {
            Point ip = GetLineIntersection(A, B - A, C, D - C);
            if (OnSegment(ip, C, D))
                newpoly.push_back(ip);
        }
    }
    return newpoly;
}

Point pre, now;
char str[10];
Polygon poly;

int main() {
    pre = Point(0, 0);
    poly.push_back(pre);
    poly.push_back(Point(10, 0));
    poly.push_back(Point(10, 10));
    poly.push_back(Point(0, 10));
    int flag = 0;
    while (scanf("%lf%lf%s", &now.x, &now.y, str) == 3) {
        if (flag) {
            printf("0.00\n");
            continue;
        }
        Point mid = Point((pre.x + now.x) * 0.5, (pre.y + now.y) * 0.5);
        Point temp = mid + Rotate(now - pre, PI * 0.5);
        if (str[0] == ‘C‘)
            poly = CutPolygon(poly, mid, temp);
        else if (str[0] == ‘H‘)
            poly = CutPolygon(poly, temp, mid);
        else {
            printf("0.00\n");
            flag = 1;
            continue;
        }
        printf("%.2lf\n", PolygonArea(poly, poly.size()));
        pre = now;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-11 01:31:09

uva 10084的相关文章

uva 10084 Hotter Colder

Problem E: Hotter Colder The children's game Hotter Colder is played as follows. Player A leaves the room while player B hides an object somewhere in the room. Player A re-enters at position (0,0) and then visits various other positions about the roo

半平面交算法及简单应用

半平面:一条直线把二维平面分成两个平面. 半平面交:在二维几何平面上,给出若干个半平面,求它们的公共部分 半平面交的结果:1.凸多边形(后面会讲解到)2.无界,因为有可能若干半平面没有形成封闭3.直线,线段,点,空(属于特殊情况吧) 算法:1:根据上图可以知道,运用给出的多边形每相邻两点形成一条直线来切割原有多边形,如果多边形上的点i在有向直线的左边或者在直线上即保存起来,否则判断此点的前一个点i-1和后一个点i+1是否在此直线的左边或线上,在的话分别用点i和点i-1构成的直线与此时正在切割的直

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica

[2016-02-03][UVA][514][Rails]

时间:2016-02-03 22:24:52 星期三 题目编号:UVA 514 题目大意:给定若干的火车(编号1-n),按1-n的顺序进入车站, 给出火车出站的顺序,问是否有可能存在 分析:    FIFO,用栈模拟一遍即可, 方法:    根据输入的顺序,从1-n开始,当前操作的为i 如果i是当前对应的编号,那么直接跳过(进入B) 如果不是,根据当前需求的编号,小于i,就从栈顶弹出一个元素, 看这个元素是否是需求的,是则继续.否则NO 1 2 3 4 5 6 7 8 9 10 11 12 13