import org.apache.commons.lang3.StringUtils;
public class TestCarMove {
private int x;
private int y;
//当前方向
private int directionIndex;
private static String[] directions = new String[4];
static {
directions[0] = "N";
directions[1] = "E";
directions[2] = "S";
directions[3] = "W";
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDirectionIndex() {
return directionIndex;
}
public void setDirectionIndex(int directionIndex) {
this.directionIndex = directionIndex;
}
public TestCarMove(int x, int y, String direction) {
this.x = x;
this.y = y;
this.directionIndex = getDirectionIndex(direction);
}
/**
* 根据输入的N E S W返回对应的数组下标
* @param direction
* @return
*/
public int getDirectionIndex(String direction){
int index = 0;
for(int i=0; i<directions.length; i++) {
String d = directions[i];
if(StringUtils.equalsIgnoreCase(direction, d)) {
index = i;
}
}
return index;
}
public void turnLeft() {
//左转,方向下标--
int temp = this.getDirectionIndex();
if(temp == 0) {
temp = directions.length - 1;
}else{
temp--;
}
this.setDirectionIndex(temp);
}
public void turnRight() {
//左转,方向下标++
int temp = this.getDirectionIndex();
if(temp == (directions.length - 1)) {
temp = 0;
}else{
temp++;
}
this.setDirectionIndex(temp);
}
/**
* 修改坐标
* @param enx
* @param eny
*/
public void doMode(int enx, int eny) {
int y = this.getY();
int x = this.getX();
if (StringUtils.equalsIgnoreCase("N", directions[this.getDirectionIndex()])) {
if(y < eny) {
this.setY(++y);
}
} else if(StringUtils.equalsIgnoreCase("E", directions[this.getDirectionIndex()])) {
if(x < enx) {
this.setX(++x);
}
} else if(StringUtils.equalsIgnoreCase("S", directions[this.getDirectionIndex()])) {
if(y > 0) {
this.setY(--y);
}
} else if(StringUtils.equalsIgnoreCase("W", directions[this.getDirectionIndex()])) {
if(x > 0) {
this.setX(--x);
}
}
}
/**
*
* @param enx 右上角x坐标
* @param eny 右上角y坐标
* @param instructs 指令
*/
public void move(int enx, int eny, String instructs) {
String[] array = instructs.split("");
for(String instruct : array) {
switch (instruct) {
case "L" : this.turnLeft(); break;
case "R" : this.turnRight(); break;
case "M" : this.doMode(enx, eny); break;
}
}
System.out.println(this.getX() + " " + this.getY() + " " + directions[this.getDirectionIndex()]);
}
public static void main(String[] args) {
TestCarMove car1 = new TestCarMove(1, 2, "N");
car1.move(5, 5, "LMLMLMLMM");
TestCarMove car2 = new TestCarMove(3, 3, "E");
car2.move(5, 5, "MMRMMRMRRM");
}
}
/**
* 美国国家航空航天局(NASA)将在火星高原上降落一批机器人漫游车。这个奇异的矩形高原必须由漫游车进行导航,以便其车载摄像机能够获得周围地形的完整视图,然后发送回地球。
* 漫游车的位置由x和y坐标以及代表四个方向(E, S, W, N)之一的字母表示。高原可以用网格来标记以简化导航。例如位置(0、0,N),这表示流动站位于高原的左下角并面向北。
* 为了控制漫游车,NASA发送了一个简单的字符串。其中的字母是“ L”,“ R”和“ M”。 “ L”和“ R”使漫游车分别向左或向右旋转90度,而不会离开当前位置。
* “ M”表示向前移动一个格网点,并保持相同的朝向。
* 假设从(x,y)直接向北的坐标是(x,y + 1)。
*
* 输入:
* 输入的第一行是高原的右上角坐标,假定左下角坐标为0,0。
* 输入的其余部分是与已部署漫游车有关的信息。每个漫游车都有两行输入。第一行给出了漫游车的位置,第二行给出了一系列指令,告诉漫游车如何探索高原。
* 第一行的位置由两个整数和一个由空格分隔的字母组成,分别对应于x和y坐标以及漫游车的方向。
* 每个漫游车将依次完成移动,这意味着第二个漫游车直到第一个漫游车完成移动后才开始移动。
*
* 输出值:
* 每个漫游车的输出应为其最终坐标和方向。
*
* 输入输出例子
* 输入:
* 5 5
*
* 1 2 N
* LMLMLMLMM
*
* 3 3 E
* MMRMMRMRRM
*
* 预期输出:
* 1 3 N
* 5 1 E
*/