zcmu 1901: Problem E: Logo

http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1901


1901: Problem E: Logo

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 130   Solved: 37
[ Submit][ Status][ Web Board]

Description

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

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

Sample Output

0



题目大意是给小乌龟F个指令(fd前行,bk后退,lt左转,rt右转),求最后小乌龟与起点的距离


解法:模拟小乌龟的行动


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define maxn 2222
#define pi 3.141592653589793
using namespace std;
struct zb
{
    double x,y;
    double fxx,fxy;
};
int T,F;
double d,d1=0;
zb end;
char s[3];
void move()
{
    if(s[0]=='f'){
        end.x+=end.fxx*d;
        end.y+=end.fxy*d;
    }
    if(s[0]=='b'){
        end.x-=end.fxx*d;
        end.y-=end.fxy*d;
    }
    if(s[0]=='l'){
            d=d+d1;
            d1=d;
            d=(180-d)*pi/180;
        end.fxx=-cos(d);
        end.fxy=sin(d);
    }
    if(s[0]=='r'){
            d=d1-d;
            d1=d;
            d=(180-d)*pi/180;
        end.fxx=-cos(d);
        end.fxy=sin(d);
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        d1=0;
        end.x=end.y=0;
        end.fxx=1,end.fxy=0;
        scanf("%d",&F);
        while(F--)
        {
            scanf("%s%lf",s,&d);
            move();
         //   printf("%lf %lf\n",end.x,end.y);
        }
        printf("%.0lf\n",sqrt(end.x*end.x+end.y*end.y));
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值