codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1607E.md (3123B)


      1 Source: https://codeforces.com/problemset/problem/1607/E
      2 
      3 E. Robot on the Board 1
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 The robot is located on a checkered rectangular board of size $n \times m$ ($n$ rows, $m$ columns). The rows in the board are numbered from $1$ to $n$ from top to bottom, and the columns — from $1$ to $m$ from left to right.
     22 
     23 The robot is able to move from the current cell to one of the four cells adjacent by side.
     24 
     25 The sequence of commands $s$ executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively.
     26 
     27 The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in $s$. If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed.
     28 
     29 The robot's task is to execute as many commands as possible without falling off the board. For example, on board $3 \times 3$, if the robot starts a sequence of actions $s=$"RRDLUU" ("right", "right", "down", "left", "up", "up") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell $(2, 1)$ (second row, first column) then all commands will be executed successfully and the robot will stop at the cell $(1, 2)$ (first row, second column).
     30 
     31  ![](https://espresso.codeforces.com/9fe7a04f375f2d9a760fbdc2d302397f39d7a250.png) The robot starts from cell $(2, 1)$ (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell $(1, 2)$ (first row, second column). 
     32 
     33 Determine the cell from which the robot should start its movement in order to execute as many commands as possible.
     34 
     35 Input
     36 
     37 The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
     38 
     39 The next $2t$ lines contain descriptions of the test cases.
     40 
     41 In the description of each test case, the first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 10^6$) — the height and width of the field that the robot is located on. The second line of the description is a string $s$ consisting solely of characters 'L', 'R', 'D' and 'U' — the sequence of commands the robot executes. The string has a length from $1$ to $10^6$ commands.
     42 
     43 It is guaranteed that the total length of $s$ over all test cases does not exceed $10^6$.
     44 
     45 Output
     46 
     47 Print $t$ lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers $r$ ($1 \leq r \leq n$) and $c$ ($1 \leq c \leq m$), separated by a space — the coordinates of the cell (row number and column number) from which the robot should start moving to perform as many commands as possible.
     48 
     49 If there are several such cells, you may output any of them.
     50 
     51 Example
     52 
     53 Input
     54 
     55 ```
     56 4
     57 1 1
     58 L
     59 1 2
     60 L
     61 3 3
     62 RRDLUU
     63 4 3
     64 LUURRDDLLLUU
     65 ```
     66 
     67 Output
     68 
     69 ```
     70 1 1
     71 1 2
     72 2 1
     73 3 2
     74 ```