`
iseesea
  • 浏览: 45568 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

java面试题:火星探测器。

阅读更多
前几天收到某公司的面试测试题,自己是做完了。。。但是,。。。。没有得到回复。。。估计是被刷了。。。不过没啥,自己也算是经历过,主要考察面向对象的思想吧;如果有人有更好的方法的话希望可以讨论一下,~~~我发现javaeye上面居然已经有了。。。。。。
引用

一小队机器人探测器将由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
尽量少用if...else和switch.



坐标: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));   
       }
    }
}



呵呵,第一次接触到比较正规的测试题,做这种题目才有兴趣~~~





2
0
分享到:
评论
2 楼 iseesea 2011-01-07  
是啊~~角度控制方向更好理解~~~~谢谢~!

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

我是为了在代码中不是用switch,case的代码
1 楼 CoderDream 2011-01-07  
改造了一下Direction,用List意思不是很明确,用角度表示方向应该更容易理解!

package mars;

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

public class Direction {
	private static Direction instance;
	private static Map<Character, Integer> directions = new HashMap<Character, Integer>();
	private static Map<Integer, Character> degrees = new HashMap<Integer, Character>();

	public Direction() {
		directions.put('E', 0);
		directions.put('N', 90);
		directions.put('W', 180);
		directions.put('S', 270);
		degrees.put(0, 'E');
		degrees.put(90, 'N');
		degrees.put(180, 'W');
		degrees.put(270, 'S');
	}

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

	public char turnTo(char faceTo, String direction) {
		String lr = direction.toUpperCase();
		int count = (lr.equals("L") ? -1 : 1);

		int faceToDegree = directions.get(faceTo);
		switch (count) {
		case -1:
			faceToDegree += 90;
			break;
		case 1:
			faceToDegree -= 90;
			break;
		}

		faceToDegree = faceToDegree >= 0 ? faceToDegree : faceToDegree + 360;
		faceToDegree = faceToDegree >= 360 ? faceToDegree - 360 : faceToDegree;

		return degrees.get(faceToDegree);
	}

	public Position moveXY(char faceTo, Position position) {

		int degree = directions.get(faceTo);
		int x = position.getX();
		int y = position.getY();
		switch (degree) {
		case 0:
			x += 1;
			break;
		case 90:
			y += 1;
			break;
		case 180:
			x -= 1;
			break;
		case 270:
			y -= 1;
			break;
		default:
			break;
		}
		position.setX(x);
		position.setY(y);

		return position;
	}

	public static void main(String[] args) {
		Direction d = Direction.getInstance();
		System.out.println(d.turnTo('W', "L"));
	}

}

相关推荐

Global site tag (gtag.js) - Google Analytics