AncientGameRecord
SRM 707 by Blizzard · 2016-12-07 · by cgy4ever
Problem Statement
At any point during the game each cell is either empty or it contains exactly one token. A move in the game looks as follows: The player selects a cell and a direction: up, down, left, or right. The selected cell must contain a token. There must be a next cell in the chosen direction, and that cell must be empty. The actual move then consists of taking the token and moving it into the next cell in the chosen direction.
You are given a list of moves: two
A list of moves is called valid if there is some initial configuration of arbitrarily many tokens on the board (with at most one token in each cell) such that it is possible to perform all the moves on the list in the given order. I.e., when executing the moves one after another, the chosen cell must always contain a token and the chosen direction must always point to an empty adjacent cell.
We want to change the given list of moves into a valid list. We can erase some moves from the given list, without changing the relative order of the other moves on the list. Compute and return the smallest number of moves we have to erase.
Constraints
- n, m will be between 2 and 50, inclusive.
- x will contain between 1 and 1,000 elements, inclusive.
- x, y and d will contain the same number of elements.
- Each element in x will be between 0 and n-1, inclusive.
- Each element in y will be between 0 and m-1, inclusive.
- Each character in d will be one of {'U', 'D', 'L', 'R'}.
2
2
{0,1,1,0}
{0,0,1,1}
"DRUL"
Returns: 0
We don't need to remove any moves. Suppose the board initially contains one token at (0,0), we will move it 4 times: (0,0) -> (1,0) -> (1,1) -> (0,1).
2
2
{0,1}
{1,0}
"LU"
Returns: 1
This is not a valid list of moves. Move 0 wants us to move a token from (0, 1) leftwards - to (0, 0). Move 1 wants us to move a token from (1, 0) upwards - again to (0, 0). Regardless of the original configuration of tokens, if we can perform move 0, it means that the cell (0, 0) now contains a token and therefore we won't be able to perform move 1. Erasing either move from the list produces a valid list.
3
3
{0,0,0,1,2,1}
{0,1,1,1,1,2}
"RLDDRL"
Returns: 1
The list is not valid because in moves 1 and 2 we try to move a token away from the cell (0, 1) twice in a row. One optimal solution is to erase move 1. One possible initial configuration of tokens and the execution of the remaining moves is shown below. o.. .o. ... ... ... ... ..o -> ..o -> .oo -> ..o -> ..o -> .o. ... ... ... .o. ..o ..o
3
3
{0,0,2,2}
{0,0,2,2}
"ULDR"
Returns: 4
We need to erase all four moves from this list. None of these moves can be on a valid list because in each case the cell is in the corner of the grid and the chosen direction points outside the grid. We cannot move a token off the grid.
6
6
{0,5,4,1,3,2,5,4,2,2,5,4,1,3,2,5}
{5,4,1,1,1,2,3,5,4,2,0,2,0,0,2,1}
"DRULLLUDRUDLRUDL"
Returns: 4
Submissions are judged against all 116 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class AncientGameRecord with a public method int minimalRemove(int n, int m, vector<int> x, vector<int> y, string d) · 116 test cases · 2 s / 256 MB per case