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");    }}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 拉里佩奇 啥是佩奇 wwe佩奇 小朱佩奇 小豬佩奇 小猜佩奇 佩奇小猪 小zhu佩奇 小者佩奇 小1猪佩奇第一季 猪佩奇第一季 小猎佩奇全集播放 啥是佩奇引热议 小猪小猪 小猪贝奇 小猪佩齐 paige 佩琪小猪 wwe佩琪 佩齐 小朱佩琦 小猪配齐 小猪配奇 小猪偑奇 小猪派奇 xiaozhupeiqi 小猪佩其 小猪配其 小猪配 小猪佩琦 佩妮 佩姬 佩字组词 佩形近字 佩尔 佩尔英语 伊莎贝尔于佩尔 佩恩 佩恩六道 佩恩图片 鸣人佩恩