您好,欢迎来到爱学范文!

当前位置:爱学范文网>>工作范文>>工作经验范文>>sohu搜狐|Thoughtworks公司面试题——MARSROVERS问题

sohu搜狐|Thoughtworks公司面试题——MARSROVERS问题

标签:时间:

说明:

来源于一个很早的帖子,题目如下,有人做过相关的解答。

我也花了1个多小时写了一个,不过这第二个输出和5×5的地图冲突,实际上第二个Rover会走出这个地图。

以下是原题

A squad of robotic rovers are to be landed by NASA on a plateauon Mars.

This plateau, which is curiously rectangular, must be navigatedby the rovers so that their on-board cameras can get a completeview of the surrounding terrain to send back to Earth.

A rover\s position and location is represented by a combinationof x and y co-ordinates and a letter representing one of the fourcardinal compass points. The plateau is divided up into a grid tosimplify navigation. An example position might be 0, 0, N, whichmeans the rover is in the bottom left corner and facing North.

In order to control a rover, NASA sends a simple string ofletters. 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 sameheading.

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 theplateau, the lower-left coordinates are assumed to be 0,0.

The rest of the input is information pertaining to the roversthat have been deployed. Each rover has two lines of input. Thefirst line gives the rover \s position, and the second line is aseries of instructions telling the rover how to explore theplateau.

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

Each rover will be finished sequentially, which means that thesecond rover won \t start to move until the first one has finishedmoving.

OUTpUT

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

INpUT AND OUTpUT

Test Input:

5 5

1 2 N

LMLMLMLMM

3 3 E

MMRMMRMRRM

火星探测器

一小队机器人探测器将由NASA送上火星高原,探测器将在这个奇特的矩形高原上行驶。

用它们携带的照相机将周围的全景地势图发回到地球。每个探测器的方向和位置将由一个x,y系坐标图和一个表示地理方向的字母表示出来。为了方便导航,平原将被划分为网格状。位置坐标示例:0,0,N,表示探测器在坐标图的左下角,且面朝北方。为控制探测器,NASA会传送一串简单的字母。可能传送的字母为:\L \, \R \和 \M \。 \L \,和 \R \分别表示使探测器向左、向右旋转90度,但不离开他所在地点。 \M 表示向前开进一个网格的距离,且保持方向不变。假设以广场(高原)的直北方向为y轴的指向。

输入:首先输入的line是坐标图的右上方,假定左下方顶点的坐标为0,0。剩下的要输入的是被分布好的探测器的信息。每个探测器需要输入wolines。第一条line提供探测器的位置,第二条是关于这个探测器怎样进行高原探测的一系列说明。位置是由两个整数和一个区分方向的字母组成,对应了探测器的(x,y)坐标和方向。每个探测器的移动将按序完成,即后一个探测器不能在前一个探测器完成移动之前开始移动。

输出:每个探测器的输出应该为它行进到的最终位置坐标和方向。输入和输出 测试如下:

期待的输入:

5 5

1 2 N

LMLMLMLMM

3 3 E

MMRMMRMRRM 期待的输出

1 3 N

5 1 E

以下是我的答案

说明:

Java语言,OO思想,JDK1.4环境和4个Class:

Lunch用于程序路口和收入输出Controller用于解释和指挥Rover的行为Area是plateau的环境Rover就是探测器了

写的匆忙,木有注释,尽可能的发挥各位的想象力了,另外打算在东南西北的转换上用循环双向列表的,但是又偷懒了(用4取余),导致这里会有点难以理解,算是美中不足吧。

欢迎大家交流 :-)

Lunch.java

package mars;

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.*;import java.util.ArrayList;

public class Lunch { private static Area area; private static Controller controller = newController(); private static int lineNum = 0; private static ArrayList roverList = newArrayList(); private static ArrayList commandList = newArrayList();

public static void main(String[] args) {

BufferedReader stdin = new BufferedReader(newInputStreamReader(System.in)); while (true){try {String input = stdin.readLine();if (lineNum == 0) {String[] initpoistion = input.split(\" \");area =controller.createArea(Integer.parseInt(initpoistion[0]), Integer.parseInt(initpoistion[1]));

}else {if (lineNum % 2 == 1) {String[] initpoistion = input.split(\" \");Rover rover = controller.createRover(Integer.parseInt(initpoistion[0]),Integer.parseInt(initpoistion[1]), initpoistion[2], area);roverList.add(rover);}else {commandList.add(input);}}if (lineNum == 4)break;lineNum++;}catch (IOException ex) {} } for (int i =0; i < roverList.size(); i++) {Rover rover = (Rover) roverList.get(i);String command = (String) commandList.get(i);for (int j = 0; j < command.length(); j++) {char c = command.charAt(j);if (c == Controller.TURN_LEFT) {controller.turnLeft(rover);}else if (c == Controller.TURN_RIGHT) {

controller.turnRight(rover);}else if (c == Controller.MOVE_FOWARD) {controller.moveFoward(rover, area);}else {System.out.println(\"commond error:\" + c);}}

controller.report(rover, area); }

}}

Controller.java

package mars;

public class Controller { public static final char TURN_LEFT = \L\; public static final char TURN_RIGHT = \R\; public static final char MOVE_FOWARD = \M\;

public Controller() { }

public static void report(Rover rover, Areaarea) {

Rover[][]a = area.getArea(); for (int i =0; i < a.length; i++) {for (int j = 0; j < a[i].length; j++) {if (a[i][j] == rover) {System.out.println(i + \" \" + j + \" \" + rover.getDirection());break;}} } }

public Area createArea(int x, int y) { return newArea(x, y); }

public Rover createRover(int x, int y, Stringdirection, Area area) { Rover rover= new Rover(direction); Rover[][] a= area.getArea(); a[x][y] =rover; returnrover; }

public void turnLeft(Rover rover) {rover.turnLeft(); }

public void turnRight(Rover rover) {rover.turnRight(); }

public void moveFoward(Rover rover, Areaarea) { booleanneedBreak = false; Rover[][] a= area.getArea(); int x; int y; for (int i =0; i < a.length; i++) {for (int j = 0; j < a[i].length; j++) {if (a[i][j] == rover) {needBreak = true;x = i;y = j;a[x][y] = null;String direction = rover.getDirection();//System.out.println(\"X:\" + x + \" Y:\" + y+ \"direction:\"+direction);if (direction.equals(Rover.EAST_DIR)) {a[x + 1][y] = rover;}else if (direction.equals(Rover.SOUTH_DIR)) {a[x][y - 1] = rover;}else if (direction.equals(Rover.WEST_DIR)) {a[x - 1][y] = rover;}else if (direction.equals(Rover.NORTH_DIR)) {a[x][y + 1] = rover;}else {System.out.println(\"error direction:\" + direction);}if (needBreak)break;}if (needBreak)break;} } }

}

Area.java

package mars;

public class Area {

Rover[][] area;

public Area(int x, int y) { area = newRover[x][y]; }

public Rover[][] getArea() { returnarea; }}

Rover.java

package mars;

public class Rover {

public static final String NORTH_DIR =\"N\"; public static final String SOUTH_DIR =\"S\"; public static final String EAST_DIR = \"E\"; public static final String WEST_DIR = \"W\"; private static final String[] DIR = {EAST_DIR, SOUTH_DIR, WEST_DIR, NORTH_DIR}; private String direction;

public Rover(String direction) {this.direction = direction; }

public void turnRight() { for (int i =0; i < DIR.length; i++) {if (direction.equals(DIR[i])) {int tmep = i + 1;direction = DIR[tmep % 4];break;} } }

public void turnLeft() { for (int i =0; i < DIR.length; i++) {if (direction.equals(DIR[i])) {int tmep = i - 1;direction = DIR[ (tmep + 4) % 4];break;} } }

public String getDirection() { returndirection; }

}

想了解更多工作范文的资讯,请访问:工作经验范文
下载文档

看过《sohu搜狐|Thoughtworks公司面试题——MARSROVERS问题》的人还看了以下文章

延伸阅读

毕业前我在广州实习,租住在一室一厅的房子里,虽然逼仄却也舒服精致。一天有个同学突然联系我,说是房东准备把房子卖了,她只好另寻住处,在此之前需要在我这里寄存一些衣物。说实话,我和这个同学并不算熟稔,但她

本文目录2018诵读串词二年级经典诵读串词四年级经典诵读串词经典诵读节目串词  尊敬的领导、各位嘉宾、敬爱的老师、亲爱的同学们:  大家好!源远流长的中华文明如长江黄河流淌千年,生生不息;博大精深的中

端午节二年级作文(精选8篇)端午节二年级作文 篇1早晨,天气阴沉沉的。几日来,家里发生了点事情,我一直也没有走出阴影,时时想起来坐着发呆。走到教室门口,一个孩子看见我“嗖“地钻进了教室。小家伙又想干什

本页是爱学范文网最新发布的《银行员工年终工作总结经典优秀范文五篇》的详细范文参考文章,觉得有用就收藏了,这里给大家转摘到爱学范文网。银行员工年终工作总结经典优秀范文五篇一年来,我始终坚持&ldqu

大学教学工作总结范文大学教学工作总结范文1总结一年来的思想、工作和学习情况,有成绩也有不足,希望在新的一年里更加努力,在各方面取得更大进步。时光匆匆,一个学期已经过去了,总结如下:一、在政治上热爱祖国

大一学生自我鉴定三篇大一自我鉴定(一)珍贵的大一生活已接近尾声,感觉非常有必要总结一下大学第一年的得失,使自己回顾走过的路,也更是为了看清将来要走的路。在刚刚进入大学时,我就怀着激动的心情向党组织递交

微信是腾讯公司于2011年1月21日推出的一个为智能终端提供即时通讯服务的免费应用程序。大家创业网为大家整理的相关的怎么用微信加粉,供大家参考选择。怎么用微信加粉作为微信加粉神器,客缘无忧微信营销机加

导语,大家眼前所欣赏的这篇文章有8237文字共八篇,学校(英语:School),是指教育者有计划、有组织地对受教育者进行系统的教育活动的组织机构。名称起源于民国。如若你对这类

试用员工转正申请书格式(29篇)试用员工转正申请书格式篇1尊敬的领导:我于20X年x月x日成为公司的试用员工,到今天3个月试用期已满,根据公司的规章制度,现申请转为公司正式员工。从来公司的第一天开

人事年度工作计划范文5篇时间的脚步是无声的,它在不经意间流逝,是时候认真思考人事工作计划如何写了。好的工作计划是什么样的呢?下面是小编为大家精心整理的人事年度工作计划范文,希望对大家有所帮助。人事年度