Multiplayer Snake
Time limit2sMemory limit1024 MB
On a wrap-around grid, two snakes move alternately; find the first move on which one snake can step onto the other snake's cell.
- Level
Medium6 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Statement
Konia is a company that makes budget mobile devices, and it has been losing market share recently. To save the company, the board of directors decided to change Snake, the most famous game preinstalled on every Konia device.
The rules are simple. A snake of length 1 (a single 1 × 1 cell) sits on a grid of rows and columns. On each move, the snake moves one cell up, down, left, or right. If it crosses a border of the screen, it appears on the opposite side. For example, stepping left from cell on a board with 10 rows and 8 columns puts the snake in cell . Food sometimes appears on the board and can make the snake longer, but that does not matter for this problem.
The board gets a two-player mode. At the start, the snakes of the two players appear in different cells, each of length 1, so each snake consists only of its starting cell. Each turn, the first player's snake moves first, then the second player's snake moves. Movement follows the same rules as in single-player mode. Unlike single-player mode, the snakes can run into each other, and then the game ends. Market research shows that users prefer games that last as long as possible, so the developers must study how the game length depends on the starting coordinates and . The two-player mode only supports movement for now, so no food appears, and the game reduces to controlling two single-cell snakes.
As the lead engineer, you are tasked with overseeing your colleagues' work. Write a program that takes the board size and and two different cells and , and computes the number of the first move on which the snakes can collide, that is, one snake moves into the cell where the other snake is.
Input
The first line contains two integers and (), the number of rows and columns of the board. Rows are numbered from 1 to from bottom to top, and columns are numbered from 1 to from left to right.
The second line contains and (, ), the starting row and column of the first player's snake.
The third line contains and in the same format for the second player's snake.
It is guaranteed that the two snakes start in different cells.
Output
Print the number of the first move on which the snakes can collide, that is, one snake moves into the cell where the other snake is.
Hint
In the first test, there is exactly one cell where the snakes can meet after one move: cell .