题目链接:LOGO
题目:
Problem E: Logo
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
Sample Output
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#define inf 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
double x,y,ang;
void go(int l)
{
x=x+sin(PI*ang/180)*l;
y=y+cos(PI*ang/180)*l;
}
void turn(int a)//这里注意时刻更新角度
{
ang+=a;
while(ang>360)
ang-=360;
while(ang<0)
ang+=360;
}
int main()
{
int t,n,k;
string s;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
x=y=ang=0;
for(int i=0;i<n;i++)
{
cin>>s;
scanf("%d",&k);
if(s=="fd")
go(k);
else if(s=="bk")
go(-k);
else if(s=="lt")
turn(k);
else if(s=="rt")
turn(-k);
}
printf("%.0lf\n",sqrt(x*x+y*y));//(x,y)到(0,0)的距离
}
return 0;
}