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


解析:

拿题目中的例子来说:


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

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


#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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值