MARS ROVERS问题(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 OUTPUTTest 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 

解决方案 »

  1.   

    下面是我的答案: 反正后来给我了一封拒信。说我没过
    我也不知道为啥没过 请各位大大帮忙看看
    class Rover
    {
    public int x;
    public int y;
    public char heading;
    }class Control
    {
    public static int xx=0;
    public static int yy=0;
    public static char heading='N'; public static char TurnLeft(char heading)
    {
    if(heading == 'N')
    heading = 'W';
    else if(heading == 'S')
    heading = 'E';
    else if(heading == 'W')
    heading = 'S';
    else if(heading == 'E')
    heading = 'N';
        return heading;
    } public static char TurnRight(char heading)
    {
    if(heading == 'N')
    heading = 'E';
    else if(heading == 'S')
    heading = 'W';
    else if(heading == 'W')
    heading = 'N';
    else if(heading == 'E')
    heading = 'S';
    return heading;
    } public static void Move(int x,int y,char heading)
    {

    if(heading == 'N')
    {
    yy++;
            if (yy>5)
    yy=5;
    }    
    else if(heading == 'S')
    {
    yy--;
    if (yy<0)
    yy=0;
    }
    else if(heading == 'W')
    {
    xx--;
    if (xx<0)
    xx=0;
    }
    else if(heading == 'E')
    {
    xx++;
    if (xx>5)
    xx=5;
    }

    } public static void Input (Rover r1,string pp)
    {
    yy=r1.y; xx=r1.x;
    heading=r1.heading;

    int i =0;
    int j=pp.Length;
    while(i<j)
    {

    if(pp[i]=='L')
    heading=TurnLeft(heading);
    else if(pp[i]=='R')
    heading=TurnRight(heading);
    else if(pp[i]=='M')
    Move(xx,yy,heading);
    i++;
    }


    }
      public static int x1()
    {
            return xx;
    } public static int y1()
    {
    return yy;
    } public static char heading1()
    {
    return heading;
    }
    }
    class Test
    {
    public static void Main()
    { Rover r1 = new Rover();
    r1.x = 1;
    r1.y = 2;
    r1.heading = 'N';
            System.Console.WriteLine("\nThe position of the robot is:"); 
    System.Console.WriteLine("{0},{1},{2}",r1.x,r1.y,r1.heading);  System.Console.WriteLine("Please input the dictation\n(M means move one temp L means turn left R means turn right)"); 
    string ss = System.Console.ReadLine();
    Control.Input(r1,ss);
    r1.heading = Control.heading1();
    r1.x = Control.x1();
    r1.y = Control.y1();
    System.Console.WriteLine("{0},{1},{2}",r1.x,r1.y,r1.heading);  Rover r2 = new Rover();
    r2.x = 3;
    r2.y = 3;
    r2.heading = 'E';
    System.Console.WriteLine("\nThe position of the robot is:"); 
    System.Console.WriteLine("{0},{1},{2}",r2.x,r2.y,r2.heading);  System.Console.WriteLine("Please input the dictation\n(M means move one temp L means turn left R means turn right)"); 
    string ss2 = System.Console.ReadLine();
    Control.Input(r2,ss2);
    r2.heading = Control.heading1();
    r2.x = Control.x1();
    r2.y = Control.y1();
    System.Console.WriteLine("{0},{1},{2}",r2.x,r2.y,r2.heading); 
    }
    }
      

  2.   

    我也寫了一個,請多指教~~~輸入:
    5 5
    1 2 N
    LMLMLMLMM
    3 3 E
    MMRMMRMRRM
    STATE
    輸出:
    1 3 N 5 1 E
    using System;
    using System.Collections;namespace ConsoleApplication5
    {
    enum Heading {E,S,W,N};
    enum X_way {E = 1,S = 0,W = -1,N = 0};
    enum Y_way {E = 0,S = -1,W = 0,N = 1};
    class Area
    {
    private static int area_X;
    private static int area_Y;
    private static ArrayList aryRovers = new ArrayList();
    public Area()
    {}
    protected void AddRover(Rover aRover)
    {
    aryRovers.Add(aRover);
    }
    public static bool SetArea(int x, int y)
    {
    if(area_X == 0 && area_Y == 0)
    {
    area_X = x;
    area_Y = y;
    return true;
    }
    else
    return false;
    }
    public static string GetState()
    {
    string strTemp = "";
    foreach(Rover aobj in aryRovers)
    {
    strTemp += String.Format("{0} {1} {2} ",aobj.X,aobj.Y,aobj.heading);
    }
    return strTemp;
    } public static bool CheckArea(int x,int y)
    {
    if(x >= 0 && y >= 0 && x <= area_X && y <= area_Y)
    return true;
    else
    return false;
    }
    } class Rover:Area
    {
    protected int m_X;
    private int m_Y;
    private Heading m_heading;
    public int X
    {
    get
    {
    return m_X;
    }
    }
    public int Y
    {
    get
    {
    return m_Y;
    }
    }
    public Heading heading
    {
    get
    {
    return m_heading;
    }
    } public Rover(int X_in, int Y_in, Heading heading_in)
    {
    m_X = X_in;
    m_Y = Y_in;
    m_heading = heading_in;
    AddRover(this);
    } public bool Turnning(bool isLeftTurnning)
    {
    try
    {
    m_heading = (Heading)(((int)heading + (isLeftTurnning?3:1))%4);
    return true;
    }
    catch
    {
    return false;
    }
    } public bool Move()
    {
    try
    {
    int x_move = (int)(X_way)(Enum.Parse(typeof(X_way),m_heading.ToString()));
    int y_move = (int)(Y_way)(Enum.Parse(typeof(Y_way),m_heading.ToString()));
    if(CheckArea(m_X + x_move,m_Y + y_move))
    {
    m_X += x_move;
    m_Y += y_move;
    }
    return true;
    }
    catch
    {
    return false;
    }
    }
    } class Class1
    {
    /// <summary>
    /// 應用程式的主進入點。
    /// </summary> [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: 在此加入啟動應用程式的程式碼
    // string strTemp = Console.ReadLine();
    try
    {
    string[] cTemp = strTemp.Trim().Split(new char[]{' '});
    Area.SetArea(Convert.ToInt32(cTemp[0]),Convert.ToInt32(cTemp[1]));
    }
    catch
    {
    Console.WriteLine("Error...");
    }

    while((strTemp = Console.ReadLine()).ToUpper() != "State".ToUpper())
    {
    try
    {
    string[] cTemp = strTemp.Trim().ToUpper().Split(new char[]{' '});
    Rover aRover = new Rover(Convert.ToInt32(cTemp[0]),Convert.ToInt32(cTemp[1]),(Heading)Enum.Parse(typeof(Heading),cTemp[2]));
    strTemp = Console.ReadLine().ToUpper();
    foreach(char active in strTemp.ToCharArray())
    {
    switch(active)
    {
    case 'L':
    aRover.Turnning(true);
    break;
    case 'R':
    aRover.Turnning(false);
    break;
    case 'M':
    aRover.Move();
    break;
    }
    }

    }
    catch(Exception ex)
    {
    Console.WriteLine("Error...");
    }
    }
    Console.WriteLine(Area.GetState());
    Console.Read();
    }
    }
    }