POJ 1654(计算几何基础多边形面积)
来源:互联网 发布:数据采集模块 编辑:程序博客网 时间:2024/05/16 14:30
题意:给你一串数字,每个数字分别代表不同的方向,一定是在1x1的格子中走动,问最后围成的多边形面积是多少
题解:将整个多边形划分,
ans = (∑相邻两个点分别与原点构成的线段的叉积 )/ 2
由叉积的几何意义可知求得的结果是一个平行四边形的面积
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <map>#include <cmath>#include <queue>#include <set>#define INF 0x3f3f3f3fusing namespace std;const int maxn = 1e6 + 5;typedef long long LL;int dx[] = {0,-1,0,1,-1,0,1,-1,0,1};int dy[] = {0,-1,-1,-1,0,0,0,1,1,1};struct Point{ int x,y; Point(){} Point(int xx, int yy):x(xx),y(yy){}};LL cross(Point a, Point b, Point c){ return (c.x-a.x)*(c.y-b.y)-(c.x-b.x)*(c.y-a.y);}int main(){ int n; char s[maxn]; scanf("%d",&n); while(n--){ scanf("%s",s); int len = (int)strlen(s); Point a(0,0); Point dot(0,0); LL ans = 0; for(int i=0; i<len; i++){ Point b(a.x+dx[s[i]-'0'],a.y+dy[s[i]-'0']); ans += cross(a,b,dot); a = b; } if(ans < 0) ans = -ans; printf("%lld",ans/2); if(ans % 2 == 1) printf(".5"); printf("\n"); }}
阅读全文