#P6233. [eJOI 2019] Awesome Arrowland Adventure
[eJOI 2019] Awesome Arrowland Adventure
Description
You are now in a matrix of size rows (rows are numbered from , from top to bottom up to ) and columns (columns are numbered from , from left to right up to ). Your initial position is . ( denotes the cell in row and column of the matrix.)
You need to reach position . This matrix is very magical: some cells contain an arrow. An arrow can only point in one of four directions: North (up), East (right), South (down), or West (left). These arrows are spread across the whole matrix, forming an arrow matrix.
When you are at some position, if this position is outside the rectangle (top-left corner , bottom-right corner ) or there is no arrow in this cell, then you will stay there forever and will never reach the destination. If there is an arrow here, then you will move one cell in the direction of the arrow.
However, it is obvious that you may not be able to find a path from to in the initial arrow matrix. To find such a path, each time you may rotate one arrow in the arrow matrix by degrees clockwise. After several rotations, you might be able to find a path.
Determine whether it is possible to obtain a path from to by rotations. If it is possible, output the minimum number of rotations needed.
Input Format
The first line contains two integers , representing the size of the matrix.
The next lines each contain a string of length , describing the arrows in that row of the arrow matrix. The string contains only kinds of characters: W, E, N, S, X. They represent: West, East, North, South, and no arrow, respectively.
Output Format
If there is no rotation plan that can produce a valid path, output -1.
Otherwise, output one integer, the minimum number of rotations required.
3 3
EES
SSW
ESX
3
3 3
EES
SSW
EEX
0
3 4
EXES
WSNS
XNNX
4
Hint
Sample Explanation
[Sample 1 Explanation]
- One feasible solution is to rotate the
Wat position three times to make it becomeS.
[Sample 2 Explanation]
- No rotation is needed.
[Sample 3 Explanation]
- Rotate once at , rotate twice at , and rotate once at .
Constraints
This problem uses bundled testdata, with a total of subtasks.
- Subtask 1 (10 points): , and the input character matrix contains only
EorX. - Subtask 2 (12 points): .
- Subtask 3 (12 points): .
- Subtask 4 (16 points): .
- Subtask 5 (50 points): no special restrictions.
For all test cases, it is guaranteed that .
Notes
The original problem is from: eJOI2019 Problem F Awesome Arrowland Adventure.
Statement translation: @_Wallace_ (if you find this translation confusing, feel free to provide a new one).
Translated by ChatGPT 5
京公网安备 11011102002149号