No.340 Footprints in the Snow
Note
Please note that, because of the constraints of this problem, you cannot pass all the test cases unless you use a fast language such as C++/Java/C#.
Test cases 00,01,02 can be passed even with LL (lightweight languages).
[For reference]
The approximate running times when the writer solved this problem with the intended solution are as follows.
- C++: 0.45 sec
- Python2: 6 sec (TLE)
- PyPy2: 1.5 sec (TLE)
Perhaps because of input/output, C# also seems to be quite tough. So far only one person has gotten AC with C# (758 ms).
Problem Statement
When Mana woke up in the morning, snow taller than she is had piled up all over the town.
She had planned to visit Mitsuru's house today, but as things stand, she cannot even go outside, let alone visit.
So Mana came up with the idea of going out by following the trails left by the adults who had dug through the snow on their way out.
However, Mana is physically frail and quickly catches a cold if she stays outside for a long time, so she cannot be made to walk a long distance.
Her worried father decided to find out how far she would have to walk to get to Mitsuru's house.
The town where Mana lives is a rectangle of width $W$ and height $H$, consisting of $W \times H$ square blocks.
The blocks are numbered with the southwesternmost block as block $0$, and the block located $w$ to the east and $h$ to the north of block $0$ has the number $w + h \times W$ $(0 \le w \lt W,\ 0 \le h \lt H)$.
From a block, one can move in the four directions east, west, south, and north, and the distance between adjacent blocks is always 1.
Mana's house is in block $0$, and Mitsuru's house is in block $ W\times H-1$.
Each adult's outing starts from the block containing their own house and consists of repeating the action of going straight from the current block and stopping at another block.
If there is an adult who passed between two adjacent blocks $B_1$, $B_2$ during such straight movement, Mana can also travel back and forth between $B_1$ and $B_2$.
For example, if the routes taken by the adults are the black arrows in the figure below, the routes by which Mana can reach Mitsuru are the red and blue arrows.
Note that Mana can move in the direction opposite to the one the adults moved in.
Given the movement routes of the adults, find the shortest distance from the block containing Mana's house to the block containing Mitsuru's house.
Input
$W \ H \ N \\
M_1 \\
B_{1 \ 0} \ B_{1 \ 1} \ ... \ B_{1 \ M_1} \\
. \\
. \\
M_N \\
B_{N \ 0} \ B_{N \ 1} \ ... \ B_{N \ M_N}
$
The elements of each line are separated by spaces.
Line $1$ gives the width $W$ of the town, the height $H$ of the town, and the number $N$ of adults who went out.
Line $2$ onward give the information on the routes of the adults' outings.
$M_i$ is the number of times adult $i$ stopped during the outing, $B_{i \ 0}$ is the block containing the house of adult $i$, and $B_{i \ j}$ $(j = 1,..,M_i)$ is the number of the block where adult $i$ stopped for the $j$-th time.
The movement from $B_{i \ j}$ to $B_{i \ j+1}$ is made by going straight in one of the directions east, west, south, or north, and the direction of the straight movement changes at each stop.
All inputs are integers and satisfy the following constraints.
$ 1 \le W,H \le 10^3 $
$ 0 \le N \le 10^3 $
$1 \le M_i \le 10^3 $
$0 \le B_{i \ j} \lt W \times H $
For $j = 0,1,..,M_i-1$,
$B_{i \ j} \neq B_{i \ j+1} \land (\ B_{i \ j} \equiv B_{i \ j+1} \pmod W \lor \lfloor B_{i \ j} \div W \rfloor = \lfloor B_{i \ j+1} \div W \rfloor \ )$
is guaranteed to hold. Note: $\lfloor x \rfloor$ is the floor function.
<Additional Constraints>
Test cases 00,01,02 satisfy
$ W,H,N,M_i \le 3.0 \times 10^2 $
.
Output
Output the shortest distance for Mana to reach the block containing Mitsuru's house by following the trails left by the adults. If she cannot reach it, output "Odekakedekinai..".
Print a newline at the end.
Sample
Sample 1
Input
3 3 2 2 0 1 4 2 4 7 8
Output
4
She can reach it by the route
$0→1→4→7→8$.
Sample 2
Input
4 4 3 3 6 2 0 12 3 3 11 8 4 3 15 13 5 6
Output
6
This is the case given as the example in the problem statement.
Sample 3
Input
5 3 0
Output
Odekakedekinai..
It seems that not a single adult went out.
Sample 4
Input
3 3 1 5 8 6 3 5 2 0
Output
8
Note that even if adults have passed through each of two adjacent blocks, Mana cannot pass unless a passage has occurred between those two blocks.
For example, adults have passed through both $1$ and $4$, but since no passage has occurred between these two blocks, she cannot go $1→4$.
Sample 5
Input
3 3 1 9 1 2 5 3 6 7 1 2 8 7
Output
Odekakedekinai..
Only the block where Mana lives is isolated, so she cannot go out.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
Tawara