Moves of the shuttle robot
Statement
The shelves of an automated warehouse are laid out as a grid of l rows and l columns. Row and column numbers both start at 0, so each number runs from 0 to l-1. One shuttle robot moves among the shelves.
Because of the way the rails are built, one move of the robot always goes two cells along one axis and one cell along the other. The cells it passes over on the way are crossed on an overhead rail, so whatever sits on them makes no difference. The eight cells the robot can reach from a cell in one move are therefore the ones below.
| Change in row | -2 | -2 | -1 | -1 | 1 | 1 | 2 | 2 |
|---|---|---|---|---|---|---|---|---|
| Change in column | -1 | 1 | -2 | 2 | -2 | 2 | -1 | 1 |
Of the eight cells, the robot cannot go to one that falls outside the grid. Nothing inside the grid blocks the robot, so it can jump to any cell inside the grid in one move.
Write a program that takes the cell the robot stands on and the cell where it has to drop its load and computes the smallest number of moves it needs. Information about several warehouses is given in order, so the answer has to be found for each warehouse.
For example, suppose that in a warehouse with l=3 the robot stands at (0, 2) and has to drop its load at (2, 0). The cell the robot stands on is written "S", the cell where it has to drop its load is written "T", and the other empty cells are written ".".
| . | . | S |
|---|---|---|
| . | . | . |
| T | . | . |
From (0, 2) only (1, 0) and (2, 1) can be reached in one move, and neither is the target cell. The four moves below reach it.
| Moves made | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Position of the robot | (0, 2) | (1, 0) | (2, 2) | (0, 1) | (2, 0) |
No way reaches it in three moves or fewer. The answer for this example is therefore 4.
Input and Output Examples
Example 1
Input
1 3 0 2 2 0
Output
4
Example 2
Input
1 8 1 6 2 4
Output
1
Example 3
Input
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
Output
5 28 0
Input
First the number of warehouses T is given as a natural number. T is a natural number from 1 to 5.
Then information about the T warehouses is given in order, and the information for each warehouse takes three lines. The first line holds the side length l of the grid. l is a natural number from 3 to 300. The next line holds the row and the column of the cell the robot stands on, separated by a space. The next line holds the row and the column of the cell where it has to drop its load, separated by a space. The rows and the columns are all integers from 0 to l-1, and the two cells may be the same.
Output
For each warehouse print the smallest number of moves the robot needs to reach the cell where it has to drop its load, one per line, in order. If the cell it stands on and the cell where it has to drop its load are the same, print 0. If no sequence of moves can reach it, print -1 on that line.
Requirements
- One move goes two cells along one axis and one cell along the other
- Of the eight cells, the robot cannot go to one that falls outside the grid
- Nothing inside the grid blocks the robot
- Print the smallest number of moves for each warehouse, one per line, in order
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 8 1 6 2 4
1
1 3 0 2 2 0
4
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
5 28 0
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.