yjanboo’s logs | 低处生活

Asking costs nothing.

3 Coding test

  • 星期三 一 21,2009 03:17 下午
  • By yjanboo
  • In tec
  • 503 views
There are three small coding tests; I received these from a famous IT company. And I think it some challenges for not implement but optimist, so I am very glad to share this to you guys for a deeper communication.

PROBLEM THREE: MARS ROVERS

A squad of robotic rovers are to be landed by NASA on a plateau on Mars. This plateau, which is curiously rectangular, must be navigated by the rovers so that their on-board cameras can get a complete view of the surrounding terrain to send back to Earth.

A rover’s position and location is represented by a combination of x and y co-ordinates and a letter representing one of the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner and facing North.

In order to control a rover, NASA sends a simple string of letters. The possible letters are ‘L’, ‘R’ and ‘M’. ‘L’ and ‘R’ makes the rover spin 90 degrees left or right respectively, without moving from its current spot. ‘M’ means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+1).

INPUT:

The first line of input is the upper-right coordinates of the plateau, the lower-left coordinates are assumed to be 0,0.

The rest of the input is information pertaining to the rovers that have been deployed. Each rover has two lines of input. The first line gives the rover’s position, and the second line is a series of instructions telling the rover how to explore the plateau.

The position is made up of two integers and a letter separated by spaces, corresponding to the x and y co-ordinates and the rover’s orientation.

Each rover will be finished sequentially, which means that the second rover won’t start to move until the first one has finished moving.

OUTPUT

The output for each rover should be its final co-ordinates and heading.

INPUT AND OUTPUT

Test Input:

5 5

1 2 N

LMLMLMLMM

3 3 E

MMRMMRMRRM

Expected Output:

1 3 N

5 1 E

The first java file is “NASAPlateauControl.java”, Included loading map and Plateaus building.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
 
/**
 * The NASA Plateaus Control
 * Explanation:
 * I had use a file called roadmap(in the ./src dictionary)to store the input data.
 * The NASAPlateauControl.java takes charge of input and store the plateaus.
 * and the Plateau.java is with the responsibility for truning , moving and facing.
 * I use some switch and case  and to the continuity of trunning, i store the 
 * converted x-y operation in a array and use a int to point the facing out from the
 * second array.
 * @author yjanboo
 * 
 */
public class NASAPlateauControl {
	public static void main(String[] args) {
		//a container to hold more than one plateau.
		ArrayList<Plateau> plateaus = new ArrayList();
		//the maxX and maxY ,if a plateau want go over the field,nothing will happen.
		int maxX = 0, maxY = 0, counter = 0;
		//read for ./src/roadmap for route data
		BufferedReader cin = null;
		try {
			cin = new BufferedReader(new InputStreamReader(new FileInputStream(
					new File("./src/roadmap"))));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			Plateau p = null;
			while (true) {
				String s = cin.readLine();
				if (s == null || s.length() == 0)
					break;
				counter++;
				System.out.println(s);
				if (counter == 1) {
					maxX = Integer.parseInt(s.charAt(0) + "");
					maxY = Integer.parseInt(s.charAt(2) + "");
				} else if (counter % 2 == 0) {
					// a new plateau;
					p = new Plateau(Integer.parseInt(s.charAt(0) + ""), Integer
							.parseInt(s.charAt(2) + ""), s.charAt(4), maxX,
							maxY);
					plateaus.add(p);
				} else {
					//set the road way.
					p.setMway(s);
				}
 
			}
			//use iterator to do a iterate operation for out put.
			Iterator iter = plateaus.iterator();
			while (iter.hasNext()) {
				p = (Plateau) iter.next();
				p.goontheway();
				p.report();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
	}
 
}

The second file “Plateau.java” is a OO implement of Plateau.

import java.util.HashMap;
 
/**
 * A NASA Plateau.
 * @author yjanboo
 *
 */
public class Plateau {
	private int x=0,y=0;
	private static final int[][] getConvertMap=new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
	private static final char[] directions=new char[]{'N','E','S','W'};
	private int p,add;
	private int maxX,maxY;
	private String mway="";
	public Plateau(){
		x=0;
		y=0;
		p=0;
	}
 
	public Plateau(int x,int y,char direction,int maxX,int maxY){
		this.x=x;
		this.y=y;
		switch(direction){
		case 'N':p=0;break;
		case 'E':p=1;break;
		case 'S':p=2;break;
		case 'W':p=3;break;
		default:p=0;
		}
		this.maxX=maxX;
		this.maxY=maxY;
	}
 
	public char move(char mchar){
		switch(mchar){
		case 'R':p=(p==3)?0:p+1;break;
		case 'L':p=(p==0)?3:p-1;break;
		default:
			if(x+getConvertMap[p][0]<=maxX) x+=getConvertMap[p][0];
			if(y+getConvertMap[p][1]<=maxY) y+=getConvertMap[p][1];
		}
		return directions[p];
	}
	public void goontheway(){
		int lengthofway=mway.length();
		for(int i=0;i<lengthofway;i++){
			char c=mway.charAt(i);
			this.move(c);
		}
	}
	public void report(){
		System.out.println(x+" "+y+" "+directions[p]);
	}
	public String getMway() {
		return mway;
	}
	public void setMway(String mway) {
		this.mway = mway;
	}
 
}

相关文章

,


43 Comments


Leave a reply

You must be logged in to post a comment.

倒计时:)

  • 2010YBD: 170 日 18 时 26 分 24 秒 之前

天气预报

friends

标签

Series



Other posts belonging to this series

    RSS Feeds

    Ads Here