蒜头君收到了一份礼物,是一个最新版的机器人。
这个机器人有 种指令:
- forward x,前进 米。
- back x,先向后转,然后前进 米。
- left x,先向左转,然后前进 米。
- right x,先向右转,然后前进 米。
现在把机器人放在坐标轴原点,起始朝向为 轴正方向。
经过一系列指令以后,你能告诉蒜头君机器人的坐标位置吗。
坐标轴上一个单位长度表示 米。
输入格式
第一行输入一个整数 表示指令的个数。
接下里 行,每行输入形如上面的指令,其中 。
输出格式
输出两个整数 表示机器人最后坐标。
用空格隔开。
样例输入
10
back -9
left 3
left 8
back 15
right 10
right -7
right -3
left 11
right 17
left 3
样例输出
9 -7
#include <iostream>
#include<string>
using namespace std;
int main() {
string direction;
int n,distance;
cin>>n;
int x=0,y=0;
//true 为此时正对的方向
bool up=false; //向上
bool down=false; //向下
bool left=false; //向左
bool right=true; //向右
for(int i=0;i<n;i++)
{
cin>>direction>>distance;
if(direction=="forward")
{
if(up==true)
{
y=y+distance;
}
else if(down==true)
{
y=y-distance;
}
else if(right==true)
{
x=x+distance;
}
else
{
x=x-distance;
}
}
else if(direction=="back")
{
if(up==true)
{
up=false;
down=true;
y=y-distance;
}
else if(down==true)
{
down=false;
up=true;
y=y+distance;
}
else if(right==true)
{
right=false;
left=true;
x=x-distance;
}
else
{
left=false;
right=true;
x=x+distance;
}
}
else if(direction=="right")
{
if(up==true)
{
up=false;
right=true;
x=x+distance;
}
else if(down==true)
{
down=false;
left=true;
x=x-distance;
}
else if(right==true)
{
right=false;
down=true;
y=y-distance;
}
else
{
left=false;
up=true;
y=y+distance;
}
}
else
{
if(up==true)
{
up=false;
left=true;
x=x-distance;
}
else if(down==true)
{
down=false;
right=true;
x=x+distance;
}
else if(right==true)
{
right=false;
up=true;
y=y+distance;
}
else
{
left=false;
down=true;
y=y-distance;
}
}
}
cout<<x<<" "<<y<<endl;
return 0;
}