POJ 1654 多边形面积

模板题。

 

View Code
 1 #include <iostream>

 2 #include <cstring>

 3 #include <cstdio>

 4 #include <cstdlib>

 5 #include <algorithm>

 6 

 7 #define N 1000010

 8 

 9 using namespace std;

10 

11 struct PO

12 {

13     int x,y;

14 }p[N];

15 

16 char str[N];

17 int n;

18 int dx[10]={0,-1,0,1,-1,0,1,-1,0,1};

19 int dy[10]={0,-1,-1,-1,0,0,0,1,1,1};

20 

21 inline int cross(PO &a,PO &b,PO &c)

22 {

23     return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);

24 }

25 

26 inline long long llabs(long long x)

27 {

28     if(x>0) return x;

29     return -x;

30 }

31 

32 inline void go()

33 {

34     scanf("%s",str+1);

35     n=strlen(str+1);

36     int a=0,b=0;

37     for(int i=1;i<=n;i++)

38     {

39         a+=dx[str[i]-'0'];

40         b+=dy[str[i]-'0'];

41         p[i].x=a; p[i].y=b;

42     }

43     long long area=0;

44     for(int i=1;i<n;i++) area+=(long long)cross(p[0],p[i],p[i+1]);

45     area=llabs(area);

46     if(area&1LL) printf("%lld.5\n",area>>1LL);

47     else printf("%lld\n",area>>1LL);

48 }

49 

50 int main()

51 {

52     int cas; scanf("%d",&cas);getchar();

53     while(cas--) go();

54     return 0;

55 }

 

 

你可能感兴趣的:(poj)