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

zoj 3720 Magnet Darts

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

zoj 3720 Magnet Darts

#include <iostream>#include <iomanip>#include <sstream>#include <string>#include <cstdio>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#define maxn 100010#define ee 1e-10using namespace std;struct Point{ double x,y; Point(double x=0,double y=0):x(x),y(y){}};typedef Point Vector;Point p0,p1;Point a[maxn];vector<Point> vec;int n,A,B;Vector operator+(Vector a,Vector b){ return Vector(a.x+b.x,a.y+b.y);}Vector operator-(Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y);}Vector operator*(Vector a,double x){ return Vector(a.x*x,a.y*x);}int dcmp(double x){ if (fabs(x)<ee) return 0;else return x<0?-1:1;}bool operator==(Vector a,Vector 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 Cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x;}bool onSegment(Point p,Point a1,Point a2){ if (p==a1 || p==a2) return true; return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0;}bool InPolygon(Point p){ int wn=0; for (int i=1;i<=n;i++) { if (onSegment(p,a[i],a[i+1])) return true; int k=dcmp(Cross(a[i+1]-a[i],p-a[i])); int d1=dcmp(a[i].y-p.y); int d2=dcmp(a[i+1].y-p.y); if (k>0 && d1<=0 && d2>0) wn++; if (k<0 && d2<=0 && d1>0) wn--; } if (wn!=0) return true; return false;}double solve(Point p){ double xx=1,yy=1; if (dcmp(p.x-p0.x-0.5)<=0) xx-=0.5-p.x+p0.x; if (dcmp(p1.x-p.x-0.5)<=0) xx-=0.5-p1.x+p.x; if (dcmp(p.y-p0.y-0.5)<=0) yy-=0.5-p.y+p0.y; if (dcmp(p1.y-p.y-0.5)<=0) yy-=0.5-p1.y+p.y; return xx*yy;}int main(){ while (cin>>p0.x>>p0.y>>p1.x>>p1.y) { cin>>n>>A>>B; for (int i=1;i<=n;i++) cin>>a[i].x>>a[i].y; a[n+1]=a[1]; vec.clear(); for (int i=floor(p0.x)-1;i<=ceil(p1.x)+1;i++) for (int j=floor(p0.y)-1;j<=ceil(p1.y)+1;j++) if (InPolygon(Point(i,j)))  vec.push_back(Point(i,j)); double area=(p1.x-p0.x)*(p1.y-p0.y),ans=0; for (int i=0;i<vec.size();i++) { int tmp=A*vec[i].x+B*vec[i].y; double are=solve(vec[i]); ans+=are*(tmp+0.0); } printf("%.3lfn",ans/area); } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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