链接:http://poj.org/problem?id=1654
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:
For example, this is a legal polygon to be computed and its area is 2.5:
Input
The first line of input is an integer t (1 <= t <= 20), the number of the test polygons. Each of the following lines contains a string composed of digits 1-9 describing how the polygon is formed by walking from the origin. Here 8, 2, 6 and 4 represent North, South, East and West, while 9, 7, 3 and 1 denote Northeast, Northwest, Southeast and Southwest respectively. Number 5 only appears at the end of the sequence indicating the stop of walking. You may assume that the input polygon is valid which means that the endpoint is always the start point and the sides of the polygon are not cross to each other.Each line may contain up to 1000000 digits.
Output
For each polygon, print its area on a single line.
Sample Input
4 5 825 6725 6244865
Sample Output
0 0 0.5 2
题意:从(0,0)出发向八个方向画点,最后回到原点,12346789代表八个方向,求最后的面积
思路:分成若干三角形,相邻两个点与原点形成一个三角形,求所有三角形的和;
注意判断0的情况
//写了好几遍。。明明很简单,想复杂了,提交一直内存超出,最后去看了其他人代码,哎功力还差得远呢,加油!
//别人写的,自己加注释
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 1e9;
const int MOD = 1e9 + 7;
#define ll long long
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int dx[10] = { 0,1,1,1,0,0,0,-1,-1,-1 };
int dy[10] = { 0,-1,0,1,-1,0,1,-1,0,1 };
char str[1000000 + 10];//储存点
ll area, x, y, px, py;//xy是输入的点,px,py是当前点坐标
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%s", str);//输入
int len = strlen(str);
if (len < 3 || '5' == str[0])//判断为0的情况
{
printf("0\n");
continue;
}
area = 0;
x = y = 0;
for (int i = 0; i<len - 1; i++)
{
px = x + dx[str[i] - '0'];
py = y + dy[str[i] - '0'];
area += (x*py - y*px);//叉积
x = px;
y = py;
}
if (area < 0) area = -area;
printf("%lld", area / 2);
if (area % 2) printf(".5");
printf("\n");
}
return 0;
}
//自己写的,内存超了
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<functional>
#include<map>
using namespace std;
const int maxn = 1000000 + 5;
const int INF = (int)1e9;
const double eps = 1e-9;
struct point {
double x;
double y;
point(double a = 0,double b = 0):x(a),y(b){}
};
int n;
point G[maxn];
point operator + (point A, point B) { return point(A.x + B.x, A.y + B.y); }
point tra(int x) {
if (x == 1) return point(1, 1);
if (x == 2) return point(0, -1);
if (x == 3) return point(1, -1);
if (x == 4) return point(-1, 0);
if (x == 5) return point(0, 0);
if (x == 6) return point(1, 0);
if (x == 7) return point(-1, 1);
if (x == 8) return point(0, 1);
}
int dir[10][2] = { { 0, 0 },{ -1, -1 },{ 0, -1 },{ 1, -1 },{ -1, 0 },{ 0, 0 },{ 1, 0 },{ -1, 1 },{ 0, 1 },{ 1, 1 } };
double cross(point a, point b, point c) {
return fabs((a.x - b.x)*(c.y - b.y) - (a.y - b.y)*(c.x - b.x));
}
char s[maxn];
int main() {
scanf("%d", &n);
while (n--) {
scanf("%s", s);
int len = strlen(s);
for (int i = 0; i < len; i++) {
int p = s[i] - '0';
if(i==0)
G[i] = tra(p);
else
G[i] = G[i - 1] + tra(p);
point k = tra(p);
}
G[len-1] = point(0, 0);
double sum = 0;
point k = point(0, 0);
for (int i = 0; i < len-1; i++) {
sum += cross(k, G[i], G[i + 1]) / 2.0;
}
if (sum == 0) printf("0\n");
else printf("%.1f\n", sum);
}
//system("pause");
return 0;
}