问题描述:

Logo is a programming language built around a turtle. Commands in the language cause the turtle to move. The turtle has a pen attached to it. As the turtle moves, it draw lines on the page. The turtle can be programmed to draw interesting pictures.

We are interested in making the turtle draw a picture, then return to the point that it started from. For example, we could give the turtle the following program:

fd 100 lt 120 fd 100 lt 120 fd 100 

The command fd causes the turtle to move forward by the specified number of units. The command lt causes the turtle to turn left by the specified number of degrees. Thus the above commands cause the turtle to draw an equilateral triangle with sides 100 units long. Notice that after executing the commands, the turtle ends up in the same place as it started. The turtle understands two additional commands. The command bk causes the turtle to move backward by the specified number of units. The command rt causes the turtle to turn right by the specified number of degrees.

After executing many commands, the turtle can get lost, far away from its starting position. Your task is to determine the straight-line distance from the turtle's position at the end of its journey back to the position that it started from.

Input

The first line of input contains one integer specifying the number of test cases to follow. Each test case starts with a line containing one integer, the number of commands to follow. The commands follow, one on each line. Each test case will contain no more than 1000 commands.

Output

For each test case, output a line containing a single integer, the distance rounded to the nearest unit.

Sample Input

1
5
fd 100
lt 120
fd 100
lt 120
fd 100

Sample Output

0

题目题意:题目给了我们四个操作,fd 原方向前进,lt 向左改变方向,bt 原方向后退,rt 向右改变方向,我们从起点出发,原方向是轴正方向,让我们求经过n次操作后的终点与起点的距离。

题目分析:我们可以用一个矢量表示方向,然后遇到前进或者后退的就可以沿着矢量方向前进或者后退(简单的画图就知道怎么写公式了),遇到改变方向的,我们就可以通过矢量的旋转来改变矢量的方向。

二维矢量旋转公式:点p(x1,y1) 绕点0(x0,y0)旋转r角度d(逆时针)的坐标 (我们可以将绕点定为原点,因为我们只关注它的方向)

X=(x1-x0)*cos(r)-(y1-y0)*sin(r)+x0;

Y=(x1-x0)*sin(r)+(y1-y0)*cos(r)+y0;

顺时针就将r换成-r

代码如下;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;const double PI=acos(-1.0);
struct Point
{double x,y;
}s,e,v;double get_dis(Point a,Point b)
{return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{int t;scanf("%d",&t);while (t--) {int n,len;Point cur;char str[5];scanf("%d",&n);cur.x=cur.y=0;v.x=1,v.y=0;//初始方向s=cur;for (int i=1;i<=n;i++) {scanf("%s%d",str,&len);if (strcmp(str,"fd")==0) {cur.x=cur.x+(v.x/sqrt(v.x*v.x+v.y*v.y))*len;cur.y=cur.y+(v.y/sqrt(v.x*v.x+v.y*v.y))*len;}else if (strcmp(str,"bk")==0) {cur.x=cur.x-(v.x/sqrt(v.x*v.x+v.y*v.y))*len;cur.y=cur.y-(v.y/sqrt(v.x*v.x+v.y*v.y))*len;}else if (strcmp(str,"lt")==0) {while (len>=360) len-=360;double r=len/(180.0)*PI;Point curv=v;v.x=curv.x*cos(r)-curv.y*sin(r);v.y=curv.x*sin(r)+curv.y*cos(r);}else {while (len>=360) len-=360;double r=len/(180.0)*PI;Point curv=v;v.x=curv.x*cos(-r)-curv.y*sin(-r);v.y=curv.x*sin(-r)+curv.y*cos(-r);}}e=cur;printf("%.0f\n",get_dis(s,e));}return 0;
}

HDU - 3174(计算几何)相关推荐

  1. *HDU 2108 计算几何

    Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  2. hdu 3320 计算几何(三维图形几何变换)

    openGL Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. HDU 2202 计算几何

    最大三角形 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  4. HDU - 1174(计算几何)

    问题描述: gameboy是一个CS高手,他最喜欢的就是扮演警察,手持M4爆土匪的头.也许这里有人没玩过CS,有必要介绍一下"爆头"这个术语:所谓爆头,就是子弹直接命中对方的头部, ...

  5. HDU - 1700(计算几何)

    问题描述: There is a cycle with its center on the origin.  Now give you a point on the cycle, you are to ...

  6. 汉诺塔实践python_汉诺塔的python 动画演示

    1.简介 古代有一座汉诺塔,塔内有3个座A.B.C,A座上有n个盘子,盘子大小不等,大的在下,小的在上,如图所示.有一个和尚想把这n个盘子从A座移到C座,但每次只能移动一个盘子,并且自移动过程中,3个 ...

  7. hdu 1174:爆头(计算几何,三维叉积求点到线的距离)

    爆头 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  8. 计算几何(判断顺时针/逆时针) - Clockwise or Counterclockwise - HDU 6857

    计算几何(判断顺时针/逆时针) - Clockwise or Counterclockwise - HDU 6857 2020 Multi-University Training Contest 8 ...

  9. hdu 4717 The Moving Points(三分+计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 说明下为啥满足三分: 设y=f(x) (x>0)表示任意两个点的距离随时间x的增长,距离y ...

  10. hdu 5964:平行四边形 【计算几何】

    打重现赛时,一点思路也没有,然后又看到这题AC数那么少,就直接放弃了.今天重新看了看,借鉴了下别人的,发现此题应该算是一道可解题. 看上去,这题的ans是同时有两个点作为自变量的函数(然而n^2复杂度 ...

最新文章

  1. 怎样测试运算放大器的输入失调电压?
  2. python文件流习题解析
  3. Java虚拟机详解(五)------JVM参数(持续更新)
  4. 大数据之-Hadoop3.x_MapReduce_数据压缩---大数据之hadoop3.x工作笔记0138
  5. 串行异步通信_每天学一点/ 电工:PLC:串行通信
  6. python 基础 信息量很大很好,适合复习
  7. 11.2. simpara
  8. android源码国内镜像,Fuchsia OS 源代码国内镜像上线
  9. RabbitMQ官方教程一 Hello World!
  10. c++求数组中出现频率最高的数
  11. 2.GET与POST的区别
  12. 快速定位iOS线上BUG在哪个控制器崩溃
  13. Improving Opencv 6: The Core Functionality :Changing the contrast and brightness of an image!
  14. 戴尔部分笔记本存GPU故障
  15. SPSS T检测原理及结果分析
  16. 【C语言】蓝桥杯/ACM竞赛入门 A+B for Input-Output Practice
  17. 彩光价格一般是多少_复合彩光祛痘印价格多少钱?
  18. UE4人物冲刺瞬移多段跳
  19. 包含下载,数据安全,数据备份16条军规
  20. GlyphRun 对象和 Glyphs 元素简介

热门文章

  1. Ubuntu界面美化
  2. Q-learning原理及其实现方法
  3. 罗丹明RB/四甲基罗丹明标记酰胺化果胶Amidated Pectin, Rhodamine B/TRITC labeled;Rhodamine B/TRITC-Amidated Pectin
  4. python实现雪花飘落的效果_简单说 JavaScript实现雪花飘落效果
  5. 赶上时代步伐,我们也来做“菱形图片”
  6. 关于H5唤起地图导航小结
  7. dbm与功率之间简单换算
  8. 免费天气API接口,全国天气免费接口,2018年3月测试稳定OK
  9. treefrog之视图 ERB
  10. swiper 移动端选项卡_基于swiper的Tab选项卡