POJ1654 Area【水题】

Area
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 23051 Accepted: 6208

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:
在这里插入图片描述

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

Source

POJ Monthly–2004.05.15 Liu Rujia@POJ

问题链接POJ1654 Area
问题简述:一个人初始在原点,按照题目所给走法,求最后得到的矩形的面积;1~9分别表示八个方向,5表示停止。
问题分析:水题看程序代码不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ1654 Area */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;

int dx[] = {0, 1, 1, 1, 0, 0, 0, -1, -1, -1};
int dy[] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
const int N = 1000000 + 1;
char s[N];

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%s", s);

        int len = strlen(s);
        if(s[0] == '5' || len < 3)
            printf("0\n");
        else {
            LL area = 0, x = 0, y = 0, nx, ny;
            for(int i = 0; i < len - 1; i++) {
                nx = x + dx[s[i] - '0'];
                ny = y + dy[s[i] - '0'];
                area += x * ny - y * nx;
                x = nx, y = ny;
            }
            if(area < 0) area = -area;

            printf("%lld", area / 2);
            if(area % 2) printf(".5");
            printf("\n");
        }
    }

    return 0;
}
发布了2289 篇原创文章 · 获赞 2373 · 访问量 265万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105524096