A width x height grid is on an XY-plane with the bottom-left cell at (0, 0) and the top-right cell at (width - 1, height - 1). The grid is aligned with the four cardinal directions ("North", "East", "South", and "West"). A robot is initially at cell (0, 0) facing direction "East".

The robot can be instructed to move for a specific number of steps. For each step, it does the following.


	Attempts to move forward one cell in the direction it is facing.
	If the cell the robot is moving to is out of bounds, the robot instead turns 90 degrees counterclockwise and retries the step.


After the robot finishes moving the number of steps required, it stops and awaits the next instruction.

Implement the Robot class:


	Robot(int width, int height) Initializes the width x height grid with the robot at (0, 0) facing "East".
	void step(int num) Instructs the robot to move forward num steps.
	int[] getPos() Returns the current cell the robot is at, as an array of length 2, [x, y].
	String getDir() Returns the current direction of the robot, "North", "East", "South", or "West".


&nbsp;
Example 1:

Input
["Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir"]
[[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
Output
[null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]

Explanation
Robot robot = new Robot(6, 3); // Initialize the grid and the robot at (0, 0) facing East.
robot.step(2);  // It moves two steps East to (2, 0), and faces East.
robot.step(2);  // It moves two steps East to (4, 0), and faces East.
robot.getPos(); // return [4, 0]
robot.getDir(); // return "East"
robot.step(2);  // It moves one step East to (5, 0), and faces East.
                // Moving the next step East would be out of bounds, so it turns and faces North.
                // Then, it moves one step North to (5, 1), and faces North.
robot.step(1);  // It moves one step North to (5, 2), and faces North (not West).
robot.step(4);  // Moving the next step North would be out of bounds, so it turns and faces West.
                // Then, it moves four steps West to (1, 2), and faces West.
robot.getPos(); // return [1, 2]
robot.getDir(); // return "West"



&nbsp;
Constraints:


	2 &lt;= width, height &lt;= 100
	1 &lt;= num &lt;= 105
	At most 104 calls in total will be made to step, getPos, and getDir.


class Robot:

    def __init__(self, width: int, height: int):
        self.perimeter = 2*width + 2*(height - 2)
        self.pos = 0
        self.atStart = True

        self.bottomRight = width - 1
        self.topRight = self.bottomRight + (height - 1)
        self.topLeft = self.topRight + (width - 1)

    def step(self, num: int) -> None:
        self.atStart = False
        self.pos = (self.pos + num) % self.perimeter

    def getPos(self) -> List[int]:
        if 0 <= self.pos <= self.bottomRight:
            return [self.pos, 0]

        if self.bottomRight < self.pos <= self.topRight:
            return [self.bottomRight, self.pos - self.bottomRight]

        if self.topRight < self.pos <= self.topLeft:
            return [self.bottomRight - (self.pos - self.topRight), self.topRight - self.bottomRight]
        
        return [0, self.topRight - self.bottomRight - (self.pos - self.topLeft)]

    def getDir(self) -> str:
        if self.atStart or 0 < self.pos <= self.bottomRight:
            return 'East'

        if self.bottomRight < self.pos <= self.topRight:
            return 'North'

        if self.topRight < self.pos <= self.topLeft:
            return 'West'
        
        return 'Sout
class Robot {

    int p;
    int w;
    int h;
    public Robot(int width, int height) {
        w = width - 1;
        h = height - 1;
        p = 0;
    }

    public void step(int num) {
        p += num;
    }

    public int[] getPos() {
        int remain = p % (2 * (w + h));
        if (remain <= w)
            return new int[]{remain, 0};
        remain -= w;
        if (remain <= h)
            return new int[]{w, remain};
        remain -= h;
        if (remain <= w)
            return new int[]{w - remain, h};
        remain -= w;
        return new int[]{0, h - remain};
    }

    public String getDir() {
        int[] pos = getPos();
        if (p == 0 || pos[1] == 0 && pos[0] > 0)
            return "East";
        else if (pos[0] == w && pos[1] > 0)
            return "North";
        else if (pos[1] == h && pos[0] < w)
            return "West";
        else
            return "South";
    }
}
class Robot {
private:
    int width, height, perimeter;
    int dir = 0;
    int x = 0, y = 0;
    map<int, string> mp = {{0, "East"}, {1, "North"}, {2, "West"}, {3, "South"}};

public:
    Robot(int width, int height) {
        this->width = width;
        this->height = height;
        this->perimeter = 2 * (height + width) - 4;
    }
    
    void step(int num) {
		if(!num) return;
        num = num % perimeter;
        if(x == 0 && y == 0 && num == 0) dir = 3;
        while(num--){
            if(x == 0 && y == 0) dir = 0;
            else if(x == width-1 && y == 0) dir = 1;
            else if(x == width-1 && y == height-1) dir = 2;
            else if(x == 0 && y == height-1) dir = 3;
            
            if(dir == 0) x += 1; 
            if(dir == 1) y += 1;
            if(dir == 2) x -= 1;
            if(dir == 3) y -= 1;
        }
        
    }
    
    vector<int> getPos() {
        return {x, y};
    }
    
    string getDir() {
        return mp[dir];
    }
};
const EAST = "East";
const NORTH = "North";
const WEST = "West";
const SOUTH = "South";

const DIRECTIONS = [EAST, NORTH, WEST, SOUTH];
const ORIGIN = [0,0];

/**
 * @param {number} width
 * @param {number} height
 */
var Robot = function(width, height) {
    this.width = width;
    this.height = height;

    this.path = []; // circular buffer of entire path of robot
    this.dirAtSquare = []; // dir robot is facing at each square
    this.currentPos = -1;

    this.preprocess();
};

/**
 * Loop around the map once and store the path into a circular buffer.
 *
 * (0,0) is set to face SOUTH. The only time it is EAST is at the beginning
 * if the robot didn't move at all.
 *
 * @return {void}
 */
Robot.prototype.preprocess = function() {
    // go right
    for (let i = 1; i < this.width; i++) {
        this.path.push([i, 0]);
        this.dirAtSquare.push(0);
    }

    // go up
    const xMax = this.width - 1;
    for (let i = 1; i < this.height; i++) {
        this.path.push([xMax, i]);
        this.dirAtSquare.push(1);
    }

    // go left
    const yMax = this.height - 1;
    for (let i = xMax - 1; i >= 0; i--) {
        this.path.push([i, yMax]);
        this.dirAtSquare.push(2);
    }

    // go down
    const xMin = 0;
    for (let i = yMax - 1; i >= 0; i--) {
        this.path.push([xMin, i]);
        this.dirAtSquare.push(3);
    }
}

/**
 * @param {number} num
 * @return {void}
 */
Robot.prototype.step = function(num) {
    this.currentPos += num;
};

/**
 * @return {number[]}
 */
Robot.prototype.getPos = function() {
    return this.currentPos === -1
        ? ORIGIN // we haven't moved
        : this.path[this.currentPos % this.path.length];
};

/**
 * @return {string}
 */
Robot.prototype.getDir = function() {
    return this.currentPos === -1
        ? DIRECTIONS[0] // we haven't moved
        : DIRECTIONS[this.dirAtSquare[this.currentPos % this.dirAtSquare.length]];
};

/**
 * Your Robot object will be instantiated and called as such:
 * var obj = new Robot(width, height)
 * obj.step(num)
 * var param_2 = obj.getPos()
 * var param_3 = obj.getDir()
 */
Walking Robot Simulation II