Thoughtworks公司面试题——MARSROVERS问题


试题 2018-03-07 07:51:43 试题
[摘要]说明:来源于一个很早的帖子,题目如下,有人做过相关的解答。我也花了1个多小时写了一个,不过这第二个输出和5×5的地图冲突,实际上第二个Rover会走出这个地图。以下是原题A squad of robotic rovers are to be landed by NASA on a职场指南

【www.cnmmxh.com--试题】

说明:

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

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

以下是原题

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

火星探测器

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

以下是我的答案

说明:

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 = new Controller();
private static int lineNum = 0;
private static ArrayList roverList = new ArrayList();
private static ArrayList commandList = new ArrayList();

public static void main(String[] args) {

BufferedReader stdin = new BufferedReader(new InputStreamReader(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, Area area) {

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 new Area(x, y);
}

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

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

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

public void moveFoward(Rover rover, Area area) {
boolean needBreak = 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 = new Rover[x][y];
}

public Rover[][] getArea() {
return area;
}
}

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() {
return direction;
}

}

本文来源:http://www.cnmmxh.com/xuexiziliao/3877.html

相关阅读
  • 10以内的加减法题库 10以内的加减法题库
  • 2019年6月英语四级真题 2019年6月英语四级真题
  • 2019年会计继续教育 2019年会计继续教育截止时间 2019年会计继续教育 2019年会计继续教育截止时间
  • 道路危险货物运输管理规定危险品运输管理规定 道路危险货物运输管理规定危险品运输管理规定
  • 不忘初心牢记使命试题3篇 不忘初心牢记使命试题3篇
  • 不忘初心牢记使命主题教育试题 不忘初心主题教育试卷 不忘初心牢记使命主题教育试题 不忘初心主题教育试卷
  • 不忘初心牢记使命检测题100题 不忘初心牢记使命检测题100题
  • 不忘初心牢记使命试题大全 不忘初心牢记使命试题大全
为您推荐
  • 歪脑袋木头桩阅读测试题和答案
    歪脑袋木头桩阅读测试题和答案
    歪脑袋木头桩故事讲的是有一个歪着尖尖的脑袋木头桩,他瞧不起周围的小草,觉得自己比谁都高明。后来,他把自己当成了一个美丽的雕像,他希望能被大家重视……以下是本站分享的歪脑袋木[db:cate]
  • 不忘初心牢记使命答题答案
    不忘初心牢记使命答题答案
    为深化“不忘初心、牢记使命”主题教育。以下是本站分享的不忘初心牢记使命答题答案,希望能帮助到大家!不忘初心牢记使命答题答案1、中国共产党第十九次全国代表大会,把 确立为党必须长[db:cate]
  • 党史知识竞赛100题
    党史知识竞赛100题
    中国共产党历史,是中国共产党从1921年7月23日成立以来整个发展过程的全部历史。以下是本站分享的党史知识竞赛100题,希望能帮助到大家!党史知识竞赛100题一、单项选择题(共100题):1、中国共产[db:cate]
  • 2019保密观知识竞赛试题及答案108个 保密观五法普法知识竞赛答案
    2019保密观知识竞赛试题及答案108个 保密观五法普法知识竞赛答案
    想知道保密观知识竞赛试题及答案的资料吗?那就快来吧!以下是本站分享的2019保密观知识竞赛试题及答案108个 保密观五法普法知识竞赛答案,希望能帮助到大家!2019保密观知识竞赛试题及答案108个 保[db:cate]
  • 不忘初心牢记使命知识竞赛题库大全
    不忘初心牢记使命知识竞赛题库大全
    开展不忘初心牢记使命知识竞赛答题可以以考促学,检测成效,以下是本站分享的不忘初心牢记使命知识竞赛题库大全,希望能帮助到大家!不忘初心牢记使命知识竞赛题库单选题(150题)1、2018年10月29日,中[db:cate]
  • 不忘初心牢记使命主题教育试题
    不忘初心牢记使命主题教育试题
    弘扬马克思主义学风,推进“两学一做”学习教育常态化制度化,以县处级以上领导干部为重点,在全党开展“不忘初心、牢记使命”主题教育,用党的创新理论武装头脑,[db:cate]
  • 管理心理学试题
    管理心理学试题
    管理心理学在西方又称为工业与组织心理学,是研究组织管理活动中人的行为规律及其潜在心理机制的一门学科。以下是本站分享的管理心理学试题,希望能帮助到大家!管理心理学试题第一部分选择题一、单项选择题:本大题[db:cate]
  • 不忘初心牢记使命试题
    不忘初心牢记使命试题
    不忘初心,就是要严于律己。“打铁必须自身硬”,作为一名党员,在任何情况下我们都要以党员的标准来严格要求自己,要以身作则,不断加强作风建设,增强群众感情;加强纪律教育,本站精心为[db:cate]
  • 不忘初心牢记使命个人对照检查材料范文
    不忘初心牢记使命个人对照检查材料范文
    坚持把“不忘初心牢记使命”作为自己的修身之本、为政之道、成事之要。对照高标准、严要求,以下是本站分享的不忘初心牢记使命个人对照检查材料范文,希望能帮助到大家!不忘初心牢记使命个[db:cate]
  • 不忘初心牢记使命题目答案
    不忘初心牢记使命题目答案
    党的十九大报告指出,在全党开展“不忘初心、牢记使命”主题教育。本站精心为大家整理了不忘初心牢记使命题目答案,希望对你有帮助。不忘初心牢记使命题目答案01党的十九大报告指出,中国[db:cate]