java面试题:火星探测器。

前几天收到某公司的面试测试题,自己是做完了。。。但是,。。。。没有得到回复。。。估计是被刷了。。。不过没啥,自己也算是经历过,主要考察面向对象的思想吧;如果有人有更好的方法的话希望可以讨论一下,~~~我发现javaeye上面居然已经有了。。。。。。
[quote]
一小队机器人探测器将由NASA送上火星高原,探测器将在这个奇特的矩形高原上行驶。 用它们携带的照相机将周围的全景地势图发回到地球。每个探测器的方向和位置将由一个x,y系坐标图和一个表示地理方向的字母表示出来。为了方便导航,平原将被划分为网格状。位置坐标示例:0,0,N,表示探测器在坐标图的左下角,且面朝北方。为控制探测器,NASA会传送一串简单的字母。可能传送的字母为:'L','R'和'M'。 'L',和'R'分别表示使探测器向左、向右旋转90度,但不离开他所在地点。'M' 表示向前开进一个网格的距离,且保持方向不变。假设以广场(高原)的直北方向为y轴的指向。 输入:首先输入的line是坐标图的右上方,假定左下方顶点的坐标为0,0。剩下的要输入的是被分布好的探测器的信息。每个探测器需要输入wo lines。第一条line 提供探测器的位置,第二条是关于这个探测器怎样进行高原探测的一系列说明。位置是由两个整数和一个区分方向的字母组成,对应了探测器的(x,y)坐标和方向。每个探测器的移动将按序完成,即后一个探测器不能在前一个探测器完成移动之前开始移动。 输出:每个探测器的输出应该为它行进到的最终位置坐标和方向。输入和输出 测试如下:
期待的输入:
5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM
期待的输出
1 3 N
5 1 E
[color=red]尽量少用if...else和switch.[/color]
[/quote]


坐标:Position.java

/*
* To change this template, choose Tools | Templates

* and open the template in the editor.
*/
//���
package mars;

/**
*
* @author Allen
*/
public class Position {

private int x;
private int y;


public Position(String x,String y){
this.x = Integer.parseInt(x);
this.y = Integer.parseInt(y);
}

public void setX(int _x) {
this.x = _x;
}

public int getX() {
return this.x;
}

public void setY(int _y) {
this.y = _y;
}

public int getY() {
return this.y;
}

@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}



方向Direction.java,之所以把移动和转向单独拿出来,是觉得这个东西是共性,比如你面向东方,那你左转一定是北方等等,之类的共性拿出来当一个小类。对于面向对象来说呢,这个就可以理解成探测器上面的方向控制器,喔~只要一个命令,就会计算好移动或者转向之后的位置,然后机器人在移动过去。。。有没有不同理解的?

//方向控制器,控制转向和移动。
package mars;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Direction {

private static Direction instance;

private static ArrayList<Character> index = new ArrayList<Character>();

private static Map<Character, Integer> direction = new HashMap<Character, Integer>();

public Direction(){
index.add('N');
index.add('E');
index.add('S');
index.add('W');
direction.put('N', 1);
direction.put('E', 1);
direction.put('S', -1);
direction.put('W', -1);
}

public static Direction getInstance(){
if (instance == null){
instance = new Direction();
}
return instance;
}

public char turnTo(char faceTo,String LR){
String lr = LR.toUpperCase();
int count = (lr.equals("L") ? -1 : 1 );
char new_faceTo = index.get((index.indexOf(faceTo) + count + 4)%4);
return new_faceTo;
}

public Position moveXY(char faceTo,Position pos){
if(index.indexOf(faceTo)%2 == 1){
pos.setX(pos.getX()+direction.get(faceTo)<=0 ? 0 : pos.getX()+direction.get(faceTo) );
}else{
pos.setY(pos.getY()+direction.get(faceTo)<=0 ? 0 : pos.getY()+direction.get(faceTo));
}
return pos;
}

}



探测器Detector.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mars;
/**
*
* @author Allen
*/
public class Detector {
private Position pos;
private char faceTo;
private String moveCmd;

public Detector(char faceTo,Position pos,String cmd){
this.pos = pos;
this.faceTo = faceTo;
this.moveCmd = cmd;
}


public void excuteCMD(Direction d){
String cmd = this.getMoveCmd();
char[] cmd_array = cmd.toUpperCase().toCharArray();

for(int i=0;i<cmd_array.length;i++){
System.out.print("\n");

if(cmd_array[i]=='M'){
this.pos = d.moveXY(this.faceTo, this.pos);
}else{
this.faceTo = d.turnTo(this.faceTo, String.valueOf(cmd_array[i]));
}
}
}

@Override
public String toString() {
return "Detector [Position: " + pos + ", faceTo: " + faceTo + ", Moving CMD: "
+ moveCmd + "]\n";
}


public String getMoveCmd() {
return moveCmd;
}


public void setMoveCmd(String moveCmd) {
this.moveCmd = moveCmd;
}


public char getFaceTo() {
return faceTo;
}

public void setFaceTo(char faceTo) {
this.faceTo = faceTo;
}

public void setPos(Position _pos){
this.pos = _pos;
}

public Position getPos(){
return this.pos;
}



}



程序入口Main.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mars;

import java.util.*;

/**
*
* @author Allen
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

//第一次使用Juint
//获取方向控制器,单例
Direction d = Direction.getInstance();
//命令接收
List<String> cmd = new ArrayList<String>();
//探测器组
List<Detector> det = new ArrayList<Detector>();

Scanner sc = new Scanner(System.in);
System.out.print("输入‘exit’退出:\n");
String str = sc.nextLine();
while (!str.toUpperCase().equals("EXIT")) {
cmd.add(str);
str = sc.nextLine();
}
//验证命令,看是否数目准确;
if(cmd.size()%2!=1){
System.out.print("请输入正确的命令数量。");
}
String[] maxXY = cmd.get(0).split(" ");
Position maxPos = new Position(maxXY[0].trim(),maxXY[1].trim());
//初始化探测器,并让他执行命令
for(int i=1;i<cmd.size();i+=2){
String[] tmp = cmd.get(i).split(" ");
Detector det_tmp = new Detector(tmp[2].toUpperCase().charAt(0),new Position(tmp[0],tmp[1]),cmd.get(i+1));
//控制器初始化之后运行。
det_tmp.excuteCMD(d);
//防止越界,理论上是应该放在方向类中,但是这是后面添加的,所以没有去该,这个不符合面向对象的思想。。。
det_tmp.getPos().setX(det_tmp.getPos().getX() <= maxPos.getX() ? det_tmp.getPos().getX() : maxPos.getX());
det_tmp.getPos().setY(det_tmp.getPos().getY() <= maxPos.getY() ? det_tmp.getPos().getY() : maxPos.getY());
det.add(det_tmp);
}

// 输出探测器信息;
for(int i=0;i<det.size();i++){
System.out.print(det.get(i));
}
}
}



呵呵,第一次接触到比较正规的测试题,做这种题目才有兴趣~~~
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thoughtworks公司面试题——MARS ROVERS问题火星探测器 C# 实现 VS2010工程,带界面展示! 一小队机器探测器将由NASA送上火星高原,探测器将在这个奇特的矩形高原上行驶。 用它们携带的照相机将周围的全景地势图发回到地球。每个探测器的方向和位置将由一个x,y系坐标图和一个表示地理方向的字母表示出来。为了方便导航,平原将被划分为网格状。位置坐标示例:0,0,N,表示探测器在坐标图的左下角,且面朝北方。为控制探测器NASA会传送一串简单的字母。可能传送的字母为: 'L ', 'R '和 'M '。 'L ',和 'R '分别表示使探测器向左、向右旋转90度,但不离开他所在地点。 'M ' 表示向前开进一个网格的距离,且保持方向不变。假设以广场(高原)的直北方向为y轴的指向。 输入:首先输入的line是坐标图的右上方,假定左下方顶点的坐标为0,0。剩下的要输入的是被分布好的探测器的信息。每个探测器需要输入wo lines。第一条line 提供探测器的位置,第二条是关于这个探测器怎样进行高原探测的一系列说明。位置是由两个整数和一个区分方向的字母组成,对应了探测器的(x,y)坐标和方向。每个探测器的移动将按序完成,即后一个探测器不能在前一个探测器完成移动之前开始移动。 输出:每个探测器的输出应该为它行进到的最终位置坐标和方向。输入和输出 测试如下: 期待的输入: 5 5 1 2 N LMLMLMLMM 3 3 E MMRMMRMRRM 期待的输出 1 3 N 5 1 E

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值