`
42087743
  • 浏览: 232083 次
  • 性别: Icon_minigender_1
  • 来自: 合肥&上海
社区版块
存档分类
最新评论

关于火星探测器的试题

 
阅读更多
引用

火星探测器     一小队机器人探测器将由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


Action.java:
public interface Action {
	public String getOutput(InputStream input);
}


ActionImpl.java:
public class ActionImpl implements Action {
	//the width and the height of each grid
	private int grid_width = 1;
	private int grid_height = 1;
	
	//the max width and the max height of the whole grids
	private int max_grid_width;
	private int max_grid_height;
	
	//the min width and the max height of the whole grids
	private int min_grid_width;
	private int min_grid_height;
	
	private String output;
	
	public ActionImpl(){
	}
	
	public ActionImpl(int min_grid_width, int min_grid_height){
		this.min_grid_height = min_grid_height;
		this.min_grid_width = min_grid_width;
	}
	
	public ActionImpl(int... args) throws Exception{
		if(args.length == 4){
			this.max_grid_width = args[0];
			this.max_grid_height = args[1];
			this.min_grid_width = args[2];
			this.min_grid_height = args[3];
		}else{
			throw new Exception("There is not enough args");
		}
	}
	
	public String getOutput(InputStream input){
		List<String> input_divided = devideInput(input);
		
		if(Integer.parseInt(input_divided.get(0).toString())<=this.min_grid_height 
				|| Integer.parseInt(input_divided.get(1).toString())<=this.min_grid_width){
			try {
				throw new Exception("you entered the uncorrect args");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		this.max_grid_height = Integer.parseInt(input_divided.get(0).toString());
		this.max_grid_width = Integer.parseInt(input_divided.get(1).toString());
		
		List<Robot> robots = new ArrayList<Robot>();
		try {
			robots = generateRobots(robots, input_divided);
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.output = "";
		for(int i=0;i<robots.size();i++){
			try {
				this.output += explore((Robot) robots.get(i)) + " ";
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return this.output.trim();
	}
	
	private List<Robot> generateRobots(List<Robot> robots, List<String> input_divided) throws Exception{
		for(int i=2;i<input_divided.size();i=i+4){
			Robot robot = new Robot();
			robot.setX(Integer.parseInt(input_divided.get(i).toString()));
			robot.setY(Integer.parseInt(input_divided.get(i+1).toString()));
			isBeyondTheMark(robot);
			robot.setDirection(input_divided.get(i+2).toString().charAt(0));
			robot.setAction(input_divided.get(i+3).toString());
			robots.add(robot);
		}
		return robots;
	}
	
	private List<String> devideInput(InputStream input){
		Scanner scan = new Scanner(input);
		List<String> input_divided = new ArrayList<String>();
		
		while(scan.hasNext()){
			String each_input = scan.next();
			if(each_input=="stop"||each_input.equals("stop")){
				break;
			}
			input_divided.add(each_input);
		}
		
		return input_divided;
	}
	
	private String explore(Robot robot) throws Exception{
		char[] action = robot.getAction().toCharArray();
		
		for(int i=0;i<action.length;i++){
			switch(action[i]){
				case 'L': 
					turn(robot, 'L');
					break;
				case 'R':
					turn(robot, 'R');
					break;
				case 'M':
					turn(robot, 'M');
					break;
			}
		}
		return robot.getX() + " " + robot.getY() + " " + robot.getDirection();
	}
	
	private void turn(Robot robot, char direction) throws Exception{
		if(direction == 'L'){
			for(int j=0;j<Robot.directions.length;j++){
				if(robot.getDirection()==Robot.directions[j]){
					if(j==0){
						robot.setDirection(Robot.directions[3]);
					}else{
						robot.setDirection(Robot.directions[j-1]);
					}
					break;
				}
			}
		}else if(direction == 'R'){
			for(int j=0;j<Robot.directions.length;j++){
				if(robot.getDirection()==Robot.directions[j]){
					if(j==3){
						robot.setDirection(Robot.directions[0]);
					}else{
						robot.setDirection(Robot.directions[j+1]);
					}
					break;
				}
			}
		}else if(direction == 'M'){
			if(robot.getDirection()==Robot.directions[0]){
				robot.setY(robot.getY()+1);
			}else if(robot.getDirection()==Robot.directions[1]){
				robot.setX(robot.getX()+1);
			}else if(robot.getDirection()==Robot.directions[2]){
				robot.setY(robot.getY()-1);
			}else if(robot.getDirection()==Robot.directions[3]){
				robot.setX(robot.getX()-1);
			}
		}
		isBeyondTheMark(robot);
	}
	
	private void isBeyondTheMark(Robot robot) throws Exception{
		if(robot.getX()>this.max_grid_height||robot.getY()>this.max_grid_width
				||robot.getX()<this.min_grid_height||robot.getY()<this.min_grid_width){
			throw new Exception("beyond the mark");
		}
	}

	public String getOutput() {
		return output;
	}

	public void setOutput(String output) {
		this.output = output;
	}
}


Robot.java:
public class Robot {
	public static final char[] directions = {'N', 'E', 'S', 'W'};
	
	//	the position of the robotic rover
	private int x;
	private int y;
	private char direction;

	private String action;
}


Main.java:
public class Main {
	public static void main(String[] args) {
		Action action = new ActionImpl();
		String output = action.getOutput(System.in);
		System.out.println(output);
	}
}
  • src.rar (5.8 KB)
  • 下载次数: 32
分享到:
评论
1 楼 insiku 2008-12-04  
那个谁谁谁 不是ThoughtWorks的?
LZ要直接pass了~

相关推荐

Global site tag (gtag.js) - Google Analytics