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
Your information is very useful! Thank you!
Where can I find more infomration abut tihs theme, except http://www.yjanboo.cn?
This is a modified theme form 『FacebookWP』http://wplight.com/2008/12/07/facebook-theme-for-wordpress.html.
You can also download it at wordpress.org.
It is much interesting =)
Julie warner…
… You may want to take jobs new mexico activities association will not fully answer your current needs and warren buffet letter 2009 words you use corey smith nfl article. just treat each article is perfect for making an article should be long in ord…
Casino 1240948325…
Casino 1240948325…
rvkqhuta…
rvkqhuta…
Ñîâðåìåííàÿ ÑÒÎ ýôôåêòèâíî è êà÷åñòâåííî âûïîëíÿåò ñâîè ôóíêöèè ïðè ïîìîùè ðàçëè÷íîãî âèäà îáîðóäîâàíèÿ…
àâòîðåìîíò…
Øèðîêèé ñïåêòð ñîâðåìåííîãî øàõòíîãî è ãîðíîîáîãàòèòåëüíîãî îáîðóäîâàíèÿ íàñ÷èòûâàåò áîëåå 100 íàèìåíîâàíèé…
âèáðàòîðû, ãðîõîòû, æåëåçîîòäåëèòåëè…
àñôàëüòîóêëàä÷èêè, àâòîêðàíû, áóëüäîçåðû, ãðåéäåðû, êàòêè, ïîãðóç÷èêè, ýêñêàâàòîðû…
Ïîãðóç÷èêè èñïîëüçóþòñÿ äëÿ çàñûïêè, ðàñêîïêè, âûðàâíèâàíèè è äðóãèõ ðàáîò ñ ðàçëè÷íûìè ìàòåðèàëàìè…
Êàê çàðàáîòàòü â èíòåðíåòå…
èíñòðóêöèè ïî çàðàáîòêó â ñåòè…
Ïÿòèçâåçäî÷íûå îòåëè Åãèïòà…
Åãèïåò - æåì÷óæèíà ìèðîâîé êóëüòóðû…
Âèäû âåíòèëÿöèîííîãî îáîðóäîâàíèÿ…
Ñèñòåìû âåíòèëÿöèè…
nzeshihx…
nzeshihx…
Ãîðîä Ñèäå ðàñïîëîæåí íà ïîëóîñòðîâå â 75 êì îò Àíòàëüè…
Îòåëè ãîðîäà Ñèäå…
Ãîà íàõîäèòñÿ íà çàïàäå Èíäèè…
Ìóçûêà, òàíöû, ïðèêëàäíîå è óñòíîå òâîð÷åñòâî ãëóáîêèìè êîðíÿìè óõîäÿò â äóøó Ãîà, à ðèòìû ïóëüñèðóþò â ñåðäöàõ åãî æèòåëåé…
Êëàññèôèêàöèÿ è íàçíà÷åíèå êàáåëåé…
Ýëåêòðîêàáåëè…
Îòåëè Àëàíèè…
Êóðîðòû Òóðöèè…
Ïèëîðàìû…
Áð¸âíîïèëüíûå äèñêîâûå ñòàíêè…
Ïàðèæ - ñòîëèöà Ôðàíöèè…
Ïðîâåäèòå îòïóñê âî Ôðàíöèè…
Ïðàãà - Ãîðîä ñîòíè øïèëåé…
Ïðàãà - ñòîëèöà ×åõèè…
Êàíàðñêèå îñòðîâà…
Âàëåíñèÿ…
ïíåâìîàâòîìàòèêà…
Ïðîìûøëåííîå èñïîëüçîâàíèå ïíåâìàòèêè íà÷àëîñü áîëåå ñòà ëåò íàçàä…
Ìåêñèêà âîïëîùåíèå èäåàëüíîãî òóðèñòè÷åñêîãî íàïðàâëåíèÿ…
Ìåêñèêà ðàñïîëàãàåò ïî-íàñòîÿùåìó óíèêàëüíûìè âîçìîæíîñòÿìè äëÿ âñåõ âèäîâ òóðèçìà…
Ìåäèöèíñêèå èçäåëèÿ…
Ìåäèöèíñêàÿ òåõíèêà…
×åì òóðèñòîâ ïðèâëåêàåò Èñïàíèÿ…
5 äîâîäîâ çà îòäûõ â Èñïàíèè…
Îáùàÿ èíôîðìàöèÿ î Òóíèñå…
Äîñòîïðèìå÷àòåëüíîñòè Òóíèñà…
Îáùàÿ èíôîðìàöèÿ î ×åðíîãîðèè…
Äîñòîïðèìå÷àòåëüíîñòè ×åðíîãîðèè…
Êëàññèôèêàöèÿ ñòàíêîâ è îáîðóäîâàíèÿ ïî ìåòàëëó…
Ìåòàëëîðåæóùèé ñòàíîê…
Ïóòåøåñòâèå â Òàéëàíä…
Âåëèêîëåïíûé îòäûõ â Òàéëàíäå…
Óëüòðàçâóêîâîé íåðàçðóøàþùèé êîíòðîëü…
Óëüòðàçâóêîâûå ïðèáîðû…
Òàõåîìåòðû ýëåêòðîííûå…
Òåîäîëèòû ýëåêòðîííûå…
Êîìïüþòåðû äëÿ ìîíòàæà â ïàíåëè óïðàâëåíèÿ…
Îäíîïëàòíûå ïðîìûøëåííûå êîìïüþòåðû…
Ëó÷øèå ïëÿæè Þæíî-Êèòàéñêîãî ìîðÿ…
Âüåòíàì - ñòðàíà ñ äðåâíåéøåé èñòîðèåé è êóëüòóðîé…
Îáîðóäîâàíèå ìåëüíè÷íî-ýëåâàòîðíîå è êðóïÿíîå…
Îáîðóäîâàíèå äëÿ ìàñëîæèðîâîé ïðîìûøëåííîñòè…
Âûäóâíîå îáîðóäîâàíèå…
Ýòèêåòèðîâî÷íîå îáîðóäîâàíèå…
Èíôîðìàöèÿ î Êèòàå…
Èíôîðìàöèÿ î ßïîíèè…
Êðèò - ñàìûé áîëüøîé îñòðîâ Ãðåöèè…
Îñòðîâ Êðèò - ýòî äðàãîöåííàÿ ÷àñòü èñòèííî ãðå÷åñêîé çåìëè…
Âåñû ïðîñòîãî âçâåøèâàíèÿ…
Òîðãîâûå âåñû…
Òîêàðíûå, ñâåðëèëüíûå, øëèôîâàëüíûå è çàòî÷íûå ñòàíêèü…
Îñîáåííîñòè òîêàðíûõ ñòàíêîâ ñ ×ÏÓ…
Îòåëè Àâñòðèè: Âåíà, Çààëüáàõ, Çàëüöáóðã, Çîëüäåí, Èøãëü…
Âåíà - ãîðîä ðîñêîøíûõ äâîðöîâ, âåëè÷åñòâåííûõ ïëîùàäåé, æèâîïèñíûõ óëî÷åê…
Îáîðóäîâàíèå äëÿ ìîíòàæà ñòàëüíûõ òðóá…
Èíñòðóìåíò äëÿ ïðåññ-ôèòèíãà…
Êîíòðîëüíî-èçìåðèòåëüíûå ïðèáîðû Yokogawa…
Àíàëèçàòîðû Yokogawa…
Leave a reply
You must be logged in to post a comment.