WOJ1065-2D Path Problem

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.

样例输入

3
RURU
URUR
RRURRUUURRRRRUUR

样例输出

1.000
-1.000
2.000


解析:

拿题目中的例子来说:

WOJ1065-2D Path Problem_第1张图片

题目中所要求的面积大小就是三角形OAB的面积再减去所有标蓝点的正方形的面积

顺着这个思路就会有以下代码:


#include
#include
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

你可能感兴趣的:(WOJ)