15009 - That Time I Got Reincarnated as a Roomba II   

Description

Hurray! Your first shift went well and you are still alive. Your owner was satisfied with the clean room, but all that dirt clogged your brush. Until he buys a replacement, he just wants you to patrol the living room and report back.

Your bumper still works the same way. A command that would take you out of the room is refused outright, and you wait for the next one. You no longer clean anything; you only move. The rest of the rules remain the same:

  • If a command would take you outside of the room, you do not move at all, you skip the command and you go on to read the next command.
  • If you receive the command STOP, it ends your shift immediately. Any commands that come afterwards are skipped.
  • If the commands run out, your shift has ended.
  • By the end of your shift, you need to output your location in the living room. You also need to report to your owner how many commands you skipped.

Note: you can try using while-loop when solving this problem.

Input

The first line is five integers. The first one is the number of command you will received com . The next two are R rows and C columns. Rows are numbered 0 to R-1 from top to bottom, columns 0 to C-1 from left to right; basically 0-indexed. The last two are your initial position in the living room sr -th row and sc -th column.

The next com lines are either:

  • <dir> <steps> where <dir> is one of N , S , E , W , which mean north, south, east, and west correspondingly. <steps> is a positive integer of how many steps you will take within the grid.
  • STOP which means you need to discard all the next commands.

Constraints

  • 1 ≤ com ≤ 100
  • 1 ≤ R, C ≤ 100
  • 0 ≤ sr < R, 0 ≤ sc < C
  • <dir> is one of N, S, E, W
  • 1 ≤ <steps> ≤ 100

Output

First line of output is your fr final row and fc column in the grid. The next line is the number of how many commands you have skipped (including the commands that come after the command STOP if applicable).

Sample Input  Download

Sample Output  Download

Tags




Discuss