#C12259. Delivery Robot Navigation

    ID: 41666 Type: Default 1000ms 256MiB

Delivery Robot Navigation

Delivery Robot Navigation

You are required to simulate the navigation system of a delivery robot on a rectangular grid. The grid has width \(W\) and height \(H\) (with coordinates from \(0\) to \(W-1\) horizontally and \(0\) to \(H-1\) vertically). Initially, the robot is at position \((0, 0)\).

The robot processes a series of commands. The commands can be of the following types:

  • MOVE D: Move one unit in direction D, where D can be N (north), S (south), E (east), or W (west). If the target cell lies outside the grid, the move is invalid. If the target cell contains an obstacle, the move is blocked. For a valid move, update the robot's position and record it in the position history.
  • ADD x y: Add an obstacle at position \((x,y)\) if that cell lies within the grid. There is no output for this command.
  • REMOVE x y: Remove an obstacle from the cell \((x,y)\) if it exists. There is no output for this command.
  • RESET: Reset the robot to its initial position \((0,0)\) and clear the position history.
  • RESET x y: Reset the robot to the provided valid position \((x,y)\) (if within the grid) and clear the position history.
  • UNDO: Undo the last successful MOVE command if possible, reverting the robot to its previous position.

For every MOVE command, output a status message on a new line:

  • If the move is successful: output Moved successfully.
  • If the move target has an obstacle: output Hit obstacle and do not change the current position.
  • If the move is outside the grid: output Invalid move and do not change the current position.

After processing all commands, output the final position of the robot in the format x y on a new line.

inputFormat

The first line contains two integers \(W\) and \(H\) representing the grid's width and height.

The second line contains an integer \(Q\), the number of commands.

The following \(Q\) lines each contain one command. The command can be one of:

  • MOVE D where D is one of N, S, E, or W.
  • ADD x y
  • REMOVE x y
  • RESET or RESET x y
  • UNDO

outputFormat

For each MOVE command, output a status message on a new line.

After processing all commands, output the final position of the robot in the format x y on a separate line.

## sample
10 10
4
MOVE N
MOVE E
UNDO
MOVE E
Moved successfully

Moved successfully Moved successfully 1 1

</p>