WOJ1065-2D Path Problem

来源:互联网 发布:网络侮辱罪案例 编辑:程序博客网 时间:2024/06/03 10:46

Consider a 2D path drawn in the following manner: Starting at the origin point, we can move only up or right. The path will be described as a string made of zero or more {'U','R'} letters. For each 'U' we'll move one unit up, while 'R' moves one unit to the right. In the following figure, the path constructed by the string RRURRUUURRRRRUUR is drawn in a thick line.


Imagine that we draw a straight line that connects the last point to the origin point in the path. (The line that is drawn in dots in the figure above) In the figure above, we can see each segment has its own direction in the drawing process. And the figure is divided into several parts. Now, we want to know the total area of all the parts. But, each part's area is a little different from common. In each part, if its boundary is clockwise, the area of this part is negative, otherwise, its area is positive.
Now, given the drawing path, can you tell me the total area described above'

输入格式

There are several test cases in input file. The first line of the input is a single integer which is the number of test cases. T
For each test case, it contains only one string line which is consisted of 'U' or 'R' letters, representing the drawing process described above.

You are ensured that there will no space between two letters and the length of the string is no more than 5000.

输出格式

For each test case, output the total area and the value must be accurate up to 3 decimal places.

样例输入

3RURUURURRRURRUUURRRRRUUR

样例输出

1.000-1.0002.000

#include<stdio.h>#include<string.h>char a[5001];int main(){    int n,x,y,sum,i;    scanf("%d",&n);    while(n-->0){        scanf("%s",&a);        int l=strlen(a);x=0,y=0,sum=0;        for(i=0;i<l;i++){            if(a[i]=='U')                y++;            else if(a[i]=='R'){                sum+=y;                x++;            }        }        double res=(double)(x*y)/2;        res-=sum;        printf("%.3lf\n",res);    }}

原创粉丝点击