Java面試編程題-火星車、火星漫遊車

題目描述如下:

一隊機器人漫遊車將被美國宇航局降落在火星高原上。漫遊車將在這個奇怪的長方形高原上巡遊,以便他們的機載攝像頭可以獲得周圍地形的完整視圖,並將其發送回地球。漫遊者的座標和位置由xy座標的組合以及代表四個方向(E, S, W, N)的字母表示。高原劃分爲網格以簡化導航。比如位置00N,表示漫遊車位於左下角並面向北。爲了控制漫遊車,美國宇航局發送一串簡單的字母。指令字母是'L''R''M' 'L''R'使漫遊車分別向左或向右旋轉90度,而不會從當前地點移動。 'M'表示前進一個網格點,並保持相同的方向。

假設從(xy)直接向北移動,就到了(xy + 1)。

INPUT

第一行輸入是平臺的右上角座標,左下角座標被假定爲0,0

其餘的輸入是有關已部署的漫遊車的信息。每個漫遊車都有兩行輸入。第一行給出了漫遊車的位置,第二行是告訴漫遊車如何探索高原的一系列指令。位置由兩個整數和一個由空格分隔的字母組成,對應於xy座標以及漫遊車當前的方向。

每個漫遊車將按順序完成,這意味着第二個漫遊車在第一個漫遊車完成移動之前不會開始移動。

OUTPUT

每個漫遊車的輸出應該是其最終的座標和位置。

輸入輸出例子

輸入:

5 5

1 2 N

LMLMLMLMM

3 3 E

MMRMMRMRRM

預期產出:

1 3 N

5 1 E

代碼如下:

public class MarsCarTest {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
List<MarsCar> carList = new ArrayList<MarsCar>();
System.out.println("請輸入平臺右上角座標");
MarsCar.Max_x = sc.nextInt();
MarsCar.Max_y = sc.nextInt();
System.out.println("命令輸入完畢後請輸入 EXIT 退出");
while(!sc.hasNext("EXIT")){
int x = sc.nextInt();
int y = sc.nextInt();
String fx = sc.next();
getCoordinate(x, y, fx, carList, sc);
}
if(carList!=null && carList.size()>0){
MarsCar car = null;
for(int i=0; i<carList.size(); i++){//輸出每輛車的座標
car = carList.get(i);
System.out.println(car.getX()+" "+car.getY()+" "+car.getFx());
}
} else {
System.out.println("暫無車輛座標信息!");
}
sc.close();
carList.clear();
}

//得到每輛車的最終座標存入集合中,命令結束後打印輸出
public static void getCoordinate(int x, int y, String fx, List<MarsCar> carList, Scanner sc){
MarsCar car = new MarsCar(x,y,fx);
if(car.inspect()){
String str = sc.next();
if(str!=null && str.length()>0){
boolean isSuccess = true;//標記若是命令錯誤則提示重輸遞歸
for(int i=0; i<str.length(); i++){
if("M".equals(String.valueOf(str.charAt(i)))){
car.move();
} else if("L".equals(String.valueOf(str.charAt(i)))){
car.gotoLeft();
} else if("R".equals(String.valueOf(str.charAt(i)))){
car.gotoRight();
} else {
System.out.println("命令輸入錯誤,請重新輸入!");
isSuccess = false;
break;
}
}
if(isSuccess){
carList.add(car);
} else {
getCoordinate(x, y, fx, carList, sc);
}
}
} else {
System.out.println("座標輸入錯誤,請重新輸入!");
}
}

}

---------class  MarsCar-----------------分割線--------------------

class MarsCar {
private int x;//x座標值
private int y;//y座標值
private String fx;//方向
static int Max_x;//最大x值
static int Max_y;//最大y值

public MarsCar(){

}
public MarsCar(int x, int y, String fx){
this.x = x;
this.y = y;
this.fx = fx.trim();
}

//檢查座標是否合格
public boolean inspect(){
if(this.x<0||this.x>Max_x||this.y<0||this.y>Max_y||!("E".equals(this.fx)||"W".equals(this.fx)||"S".equals(this.fx)||"N".equals(this.fx))){
return false;
}
return true;
}

public int getX() {
return x;
}

//x座標限制
public void setX(int x) {
if(x<=Max_x && x>=0){
this.x = x;
}
}

public int getY() {
return y;
}

//y座標限制
public void setY(int y) {
if(y<=Max_y && y>=0){
this.y = y;
}
}

public String getFx() {
return fx;
}

public void setFx(String fx) {
if("E".equals(fx)||"S".equals(fx)||"W".equals(fx)||"N".equals(fx)){
this.fx = fx.trim();
}
}

//前進一步
public void move(){
if("E".equals(this.fx)){
this.x += 1;
} else if("S".equals(this.fx)){
this.y -= 1;
} else if("W".equals(this.fx)){
this.x -= 1;
} else if("N".equals(this.fx)){
this.y += 1;
}
}

//向左轉
public void gotoLeft(){
if("E".equals(this.fx)){
this.fx = "N";
} else if("S".equals(this.fx)){
this.fx = "E";
} else if("W".equals(this.fx)){
this.fx = "S";
} else if("N".equals(this.fx)){
this.fx = "W";
}
}
//向右轉
public void gotoRight(){
if("E".equals(this.fx)){
this.fx = "S";
} else if("S".equals(this.fx)){
this.fx = "W";
} else if("W".equals(this.fx)){
this.fx = "N";
} else if("N".equals(this.fx)){
this.fx = "E";
}
}

}

如有問題,歡迎指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章