栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1886 Hotter Colder

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1886 Hotter Colder

#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cmath>using namespace std;const double eps = 1e-8;const double pi = acos(-1.0);const int N = 60;const double maxl = 10;struct cpoint {    double x, y;    cpoint(double xx = 0, double yy = 0): x(xx), y(yy) {};};int dcmp(double x) {    if (x < -eps) return -1; else return x > eps;}double cross(cpoint p0, cpoint p1, cpoint p2) {    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);}bool EqualPoint(cpoint a, cpoint b) {    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}struct cvector {    cpoint s, e;    double ang, d;};void setline(double x1, double y1, double x2, double y2, cvector &v) {    v.s.x = x1; v.s.y = y1;    v.e.x = x2; v.e.y = y2;    v.ang = atan2(y2 - y1, x2 - x1);    if (dcmp(x1 - x2))          // x1 > x2v.d = (x1 * y2 - x2 * y1) / fabs(x1 - x2);    else v.d = (x1 * y2 - x2 * y1) / fabs(y1 - y2);}bool parallel(const cvector &a, const cvector &b) {    double u = (a.e.x - a.s.x) * (b.e.y - b.s.y) - (a.e.y - a.s.y) * (b.e.x - b.s.x);    return dcmp(u) == 0;}cpoint CrossPoint(const cvector &a, const cvector &b) {    cpoint res;    double u = cross(a.s, a.e, b.s), v = cross(a.e, a.s, b.e);    res.x = (b.s.x * v + b.e.x * u) / (u + v);    res.y = (b.s.y * v + b.e.y * u) / (u + v);    return res;}static bool VecCmp(const cvector &l, const cvector &r) {    if (dcmp(l.ang - r.ang)) return l.ang < r.ang;    return l.d < r.d;}cvector deq[N];void HalfPanelCross(cvector vec[], int n, cpoint cp[], int &m) {    int i, tn; m = 0;    sort(vec, vec + n, VecCmp);    for(i = tn = 1; i < n; ++i)         if(dcmp(vec[i].ang - vec[i - 1].ang) != 0) vec[tn++] = vec[i];n = tn;int bot = 0, top = 1;deq[0] = vec[0];deq[1] = vec[1]; for (i = 2; i < tn; ++i) {if (parallel(deq[top], deq[top - 1]) || parallel(deq[bot], deq[bot + 1]) ) return ;while ( bot < top && dcmp( cross(vec[i].s, vec[i].e, CrossPoint(deq[top], deq[top - 1])) ) < 0 ) top--;while ( bot < top && dcmp( cross(vec[i].s, vec[i].e, CrossPoint(deq[bot], deq[bot + 1])) ) < 0 ) bot++;deq[++top] = vec[i];}while ( bot < top && dcmp( cross(deq[bot].s, deq[bot].e, CrossPoint(deq[top], deq[top - 1])) ) < 0 ) top--;while ( bot < top && dcmp( cross(deq[top].s, deq[top].e, CrossPoint(deq[bot], deq[bot + 1])) ) < 0 ) bot++;if (top <= bot + 1) return ; for (i = bot; i < top; i ++)cp[m++] = CrossPoint(deq[i], deq[i + 1]);if (bot < top + 1)cp[m++] = CrossPoint(deq[bot], deq[top]);m = unique(cp, cp + m, EqualPoint) - cp;}double PolygonArea(cpoint p[], int n) {    if (n < 3) return 0;    double s = p[0].y * (p[n - 1].x - p[1].x);    for (int i = 1; i < n; ++i)        s += p[i].y * (p[i - 1].x - p[(i + 1) % n].x);    return fabs(s / 2); }cpoint Whirl(double cosl, double sinl, cpoint a, cpoint b){    b.x -= a.x; b.y -= a.y;    cpoint c;    c.x = b.x * cosl - b.y * sinl + a.x;    c.y = b.x * sinl + b.y * cosl + a.y;    return c;}int n, m;cvector v[N];cpoint cp[N],cur,next;void solve() {cpoint mid;char str[10];scanf("%s",str);mid.x=(cur.x+next.x)/2; mid.y=(cur.y+next.y)/2;cpoint kp;    if(strcmp(str,"Colder")==0)kp=Whirl(0,1,mid,next);else if(strcmp(str,"Hotter")==0)kp=Whirl(0,-1,mid,next);cur=next;setline(mid.x,mid.y,kp.x,kp.y,v[n++]);    HalfPanelCross(v, n, cp, m);    if (m < 3) printf("0.00n");    else printf("%.2lfn", PolygonArea(cp, m));}int main() {cur.x = cur.y = 0.0;setline(0, 0, maxl, 0, v[0]);    setline(maxl, 0, maxl, maxl, v[1]);    setline(maxl, maxl, 0, maxl, v[2]);    setline(0, maxl, 0, 0, v[3]);    n = 4;    while (~scanf("%lf%lf",&next.x,&next.y)){        solve();    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377695.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号