Saturday 11 May 2013

ROBOT MEET PROBLEM.


/*
      *  Two robots land with their parachutes on an infinite one-dimensional number line.
      *  They both release their parachutes as soon as they land and start moving.
      *  They are allowed only to make use of the following functions.
      *   I.     moveLeft()         // robot moves to left by 1 unit in 1 unit time
      *   II.    moveRight()        // robot moves to right by 1 unit in 1 unit time
      *   III.   noOperation()      // robot does not move and takes 1 unit time
      *   IV.    onTopOfParachute() // returns true if the robot is standing on top of either of the parachute, else false
      *   V.     didWeMeet()        // returns true if the robot meets to the other robot, else false
      *
      *
      *
      *  Approach -1
      *  __________________________________________________
      *  Move both the robots in oppossite directions.
      *  And at each step check whether 2 robots meet?
      *
      *  WON'T WORK :
      *  Consider robot1 landed at x=0 and robot2 landed at x=100.
      *  case1: move robot1 to right and move robot2 to left then both will meet at x=50.
      *  case2: move robot1 to left and move robot2 to right then they will never meet and will enter into infinite loop.
      *  As we donot have any means of finding out the coordinate of the robots This approach is not reliable.
      *
      *  Approach -2
      *  __________________________________________________
      *  Move both the robot to left.
      *  If robot crosses other's parachute.
      *  Reverse direction of second robot and continue moving first robot in same direction.
      *
      *  WON'T WORK :
      *  Because both robots are executing a separate copy of their program.
      *  A program can only influence robot executing this program and not the other robot as it is executed a copy of same program separately.
      *  And its movements will be governed by that program, variables assigned in one won't work in other.
      *  i.e. it is not possible to change the direction of other robot from program executed by this robot.
      *  
      *  Approach -3
      *  __________________________________________________
      *  follow this pattern, MoveLeft 1 time, MoveRight 2 times, MoveLeft 3 times, MoveRight 4 times and so on.
      *  In each iteration, the robot will go over it's own parachute one time.
      *  But after certain time, one robot will go over its parachute and also the other robots parachute.
      *  At this point, I stop this robot and continue with the other robot.
      *  Finally when this robot go over it's own parachute, it meets the other robot
      *
      *  This will work.
      *
      *  Approach -4
      *  __________________________________________________
      *  Keep a flag whether the robot has reached the other's parachute or not.
      *  If it has reached, call moveLeft() two times, otherwise call moveLeft() and noOperation().
      *  This way one robot will double it's speed as soon as it reaches to the other's parachute and eventually they will meet.
      *
      *  CATCH:
      *  Don't move a robot with twice as speed as other from landing point onwards.
      *  As we donot have any info which robot is behind and which one is in front.
      *  So if front robot is moved with double speed then lagging behind robot then it will never be able to catch it hence, never meet.
*/

      void roboMeet()
      {
            bool reachedParachute = false;
            while( !didWeMeet() )
            {
                  if(reachedParachute)
                  {
                        moveLeft();
                        moveLeft();
                  }
                  else
                  {
                        moveLeft();
                        noOperation();
                  }
                  if( onTopOfParachute() )
                  {
                        reachedParachute = true;
                  }
            }
      }

6 comments:

  1. How does this look like?
    void main{
    do {moveleft();}
    while(!ontopofparachute())

    while(!didwemeet();)
    { ontopofparachute() ? noOperation(); : moveright{}; }
    }

    ReplyDelete
    Replies
    1. Doesn't seem to work well.
      The left most robot will continue left forever at full speed, so it can never be reached.

      Delete
  2. I didn't understand why approach 2 won't work.

    ReplyDelete
    Replies
    1. Because both robots will execute the same copy of the program so if we will not be able to reverse the direction of one robot from the function of the other robot

      Delete
  3. Robots Problems and Solutions If there's one issue that robots completely surpass, over humans, it's the repetitive tasks that demand accuracy. Robots ar precise and quick. They operate while not the chance of human error and once programmed, these machines is productive for astonishing lengths of your time.Robots have in-built sensing devices associated vision systems that enable them to differentiate an assortment of colours and sizes of the half, type elements and product through reading barcodes, conduct machine inspections and loads a lot of.

    ReplyDelete
  4. I didnt understand the code soln. provided above in post.

    ReplyDelete