[Solution] Robot in a Hallway Codeforces Solution
There is a grid, consisting of rows and columns. The rows are numbered from to from top to bottom. The columns are numbered from to from left to right.
The robot starts in a cell . In one second, it can perform either of two actions:
- move into a cell adjacent by a side: up, right, down or left;
- remain in the same cell.
The robot is not allowed to move outside the grid.
Initially, all cells, except for the cell , are locked. Each cell contains a value — the moment that this cell gets unlocked. The robot can only move into a cell if at least seconds have passed before the move.
The robot should visit all cells without entering any cell twice or more (cell is considered entered at the start). It can finish in any cell.
occupied
What is the fastest the robot can achieve that?
The first line contains a single integer () — the number of testcases.
The first line of each testcase contains a single integer () — the number of columns of the grid.
The -th of the next lines contains integers () — the moment of time each cell gets unlocked. . If , then cell is unlocked from the start.
The sum of over all testcases doesn't exceed .
For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.
No comments:
Post a Comment