#include #include using namespace std; class OneDimensionalRobotEasy { public: int finalPosition(string commands, int A, int B); }; int OneDimensionalRobotEasy::finalPosition(string commands, int A, int B) { /* The only thing that we need to do is keep track of the current position */ int i; int position; position = 0; /* Look at each command, and if the command won't put the robot past its constraints, move it in the proper direction. */ for (i = 0; i < commands.size(); i++) { if (commands[i] == 'L') { if (position > -A) position--; } else { if (position < B) position++; } } /* Return the final position */ return position; }