• thoughtworks面试题分析与解答


    题目描述

    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

     

    题目分析:

    这个题其实是thoughtworks里面员工用于娱乐的一道题,漏洞很多,比如出了矩阵的范围怎么办,两个探测器相遇该怎么办之类的问题,这道题都没有给出具体的方案,所以漏洞很多,纯属娱乐,代码以及解决方案如下

     1 #include<iostream>
     2 #include<string>
     3 
     4 using namespace std;
     5 enum dir {N=0,E,S,W};//像友加1向左加3然后整体模上4,就可以实现一圈圈无间断的方向连续不断的变化
     6 class mas
     7 {
     8 private:
     9     int loc_line;
    10     int loc_row;
    11     char direction;
    12     int number;
    13     string duty;
    14 public:
    15     mas(int pos1, int pos2, char dir, string input)
    16         :loc_line(pos1), loc_row(pos2), direction(dir), duty(input){}
    17     ~mas(){}
    18     void change()
    19     {
    20         if (direction == 'N')
    21             number = N;
    22         else
    23         if (direction == 'E')
    24             number = E; 
    25         else
    26         if (direction == 'S')
    27             number = S; 
    28         else
    29         if (direction == 'W')
    30             number = W;
    31     }
    32     void rechange()//这里也可以用switch语句来实现,上面change函数也是一样的;方法不固定
    33     {
    34         if (number == N)
    35             direction = 'N';
    36         else
    37         if (number == W)
    38             direction = 'E';
    39         else
    40         if (number == S)
    41             direction = 'S';
    42         else
    43         if (number == W)
    44             direction = 'W';
    45     }
    46     void move(){
    47         change();
    48         int max = duty.size();
    49         for (int i = 0; i < max; i++)
    50         {
    51             switch (duty[i])
    52             {
    53             case 'L':number = (number + 3) % 4 ; break;
    54             case 'R':number = (number + 1) % 4 ; break;
    55             case 'M':
    56                     switch (number){
    57                     case 0:loc_row++; break;
    58                     case 1:loc_line++; break;
    59                     case 2:loc_row--; break;
    60                     case 3:loc_line--; break;
    61                     }
    62             default:break;
    63             }
    64         }
    65         rechange();
    66         show();
    67     }
    68     void show(){ cout << loc_line << ' ' << loc_row << ' ' << direction << endl; }
    69 };
    70 
    71 int main()
    72 {
    73     int line, row;
    74     int pos_line, pos_row;
    75     char dir;
    76     string input;
    77         cin >> line >> row;
    78     while (cin >> pos_line >> pos_row >> dir)
    79     {
    80         cin >> input;
    81         mas rover(pos_line, pos_row, dir, input);
    82         rover.move();
    83     }
    84     return 0;
    85 }

    测试结果:
    如有问题或错误或者你想说点什么请留言;
  • 相关阅读:
    带有参数的存储过程
    不实用数据库实现保存和查询功能
    http错误代码(快速了解错误所在)
    ListBox的应用(左边中的信息移至右边)
    省市县三级连动(数据在一个表中)
    简单的实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。 .
    省级三连动(二)
    省市选择期三级联动
    百钱百鸡
    MySQL命令简单应用
  • 原文地址:https://www.cnblogs.com/freedom314/p/4499109.html
Copyright © 2020-2023  润新知