您的位置:首页 > 其它

poj 1654 Area (多边形求面积)

2014-08-10 15:26 453 查看
链接:http://poj.org/problem?id=1654

Area

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 14952Accepted: 4189
Description

You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orthogonal coordinate system. From this vertex, you may go step by step to the following vertexes of the polygon until back to the initial vertex. For each step you may go North, West, South or East with step length of 1 unit, or go Northwest, Northeast, Southwest or Southeast with step length of square root of 2.

For example, this is a legal polygon to be computed and its area is 2.5:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>

#define MAXX 1000002
#define eps 1e-6
typedef struct
{
double x;
double y;
} point;

double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}

bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool dd(double x,double y)
{
return fabs(x-y)<eps;
}

int main()
{
int n,m,i,j,x,y;
scanf("%d",&n);
char str[MAXX];
int move[10][2]= {{0,0},{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}};
for(i=0; i<n; i++)
{
scanf("%s",str);
int len=strlen(str);
int x1=0,y1=0,x2,y2;
long long ans=0;
for(j=0; j<len-1; j++)
{
x2=x1+move[str[j]-'0'][0];
y2=y1+move[str[j]-'0'][1];
ans+=((x1*y2)-(x2*y1));
x1=x2;
y1=y2;
}
ans = ans > 0 ? ans : (-1)*ans;
if(ans == 0)
printf("0\n");
else if(ans % 2 == 0)
printf("%lld\n",ans/2);
else if(ans % 2 != 0)
printf("%lld.5\n",ans/2);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: