Description
自从xiao_wu发明了只能向左转与向右转的机器人以后,热血沸腾的他又给机器人加了一个操作。假设机器人在二维坐标系的原点,一开始面向Y轴正方向(北N),现在给你一个仅由’L’,’R’,’M’的串,其中L表示向左转,R表示向右转,M表示向所面对的方向走一个单位的距离,试问经过操作过后,机器人的坐标和所面对的方向。
北(N),西(W),东(E),南(S)。
Input
第一行输入一个T(T<150),表示任务的个数
对于每个任务,输入一个串。(每个任务开始前机器人在原点,面向北(N),既Y轴正方向)
串长度不大于100
Output
对于每个任务,输出两个数表示机器人的坐标,一个字符表示机器人的面朝的方向。
Sample
Input
2
LRMLL
LMRMMLLL
Output
0 1 S
-1 2 E
Hint
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
int i, j;
String s;
char direction;
int x, y;
for(i = 1; i <= t; i++) {
s = reader.next();
char a[] = s.toCharArray();
x = y = 0; //初始化坐标
direction = 'N';//方向初始化
for(j = 0; j < s.length(); j++) {
if(a[j] == 'L') {
if(direction == 'N')
direction = 'W';
else if(direction == 'W')
direction = 'S';
else if(direction == 'S')
direction = 'E';
else if(direction == 'E')
direction = 'N';
} else if(a[j] == 'R') {
if(direction == 'N')
direction = 'E';
else if(direction == 'E')
direction = 'S';
else if(direction == 'S')
direction = 'W';
else if(direction == 'W')
direction = 'N';
} else if(a[j] == 'M') {
if(direction == 'N')
y++;
else if(direction == 'W')
x--;
else if(direction == 'S')
y--;
else if(direction == 'E')
x++;
}
}
System.out.println(x+" "+y+" "+direction);
}
reader.close();
}
}