Labyrinth Adventures Codeforces Solution | Codeforces Problem Solution 2022
You found a map of a weirdly shaped labyrinth. The map is a grid, consisting of rows and columns. The rows of the grid are numbered from to from bottom to top. The columns of the grid are numbered from to from left to right.
The labyrinth has layers. The first layer is the bottom left corner (cell ). The second layer consists of all cells that are in the grid and adjacent to the first layer by a side or a corner.
The third layer consists of all cells that are in the grid and adjacent to the second layer by a side or a corner. And so on.
The labyrinth with layers, for example, is shaped as follows:
The layers are separated from one another with walls. However, there are doors in these walls.
Each layer (except for layer ) has exactly two doors to the next layer. One door is placed on the top wall of the layer and another door is placed on the right wall of the layer. For each layer from to you are given positions of these two doors. The doors can be passed in both directions: either from layer to layer or from layer to layer .
If you are standing in some cell, you can move to an adjacent by a side cell if a wall doesn't block your move (e.g. you can't move to a cell in another layer if there is no door between the cells).
Now you have queries of sort: what's the minimum number of moves one has to make to go from cell to cell .
The first line contains a single integer () — the number of layers in the labyrinth.
The -th of the next lines contains four integers and () — the coordinates of the doors. Both cells are on the -th layer. The first cell is adjacent to the top wall of the -th layer by a side — that side is where the door is. The second cell is adjacent to the right wall of the -th layer by a side — that side is where the door is.
The next line contains a single integer () — the number of queries.
The -th of the next lines contains four integers and () — the coordinates of the cells in the -th query.
For each query, print a single integer — the minimum number of moves one has to make to go from cell to cell .
No comments:
Post a Comment