InfiniteLab
SRM 490 · 2010-03-12 · by Chmel_Tolstiy
Problem Statement
The rows and columns of the labyrinth are numbered using integers. The rows are infinite in both directions, so for every integer i (including negative integers) there's a row numbered i. The columns are numbered 0 to W-1, inclusive. A cell in row i and column j is denoted as (i,j).
If you are located in a free cell (i,j), you can perform one of the following actions:
- Walk to another free cell (x,y) adjacent by side to (i,j). In other words, (x,y) must be such that |i-x| + |j-y| = 1. It is impossible to walk outside of the labyrinth.
- If cell (i,j) contains a teleport, you can use it to be transferred to another free cell from the same row that contains a teleport (there's always exactly one such cell). Note that when you are located in a cell with a teleport, it isn't necessary to use the teleport.
You are given a
Return the minimum number of moves needed to get from cell (r1,c1) to cell (r2,c2). If it is impossible, return -1.
Notes
- The start and finish cells (r1,c1) and (r2,c2) are guaranteed to be distinct free cells.
Constraints
- map will contain between 1 and 20 elements, inclusive.
- Each element of map will contain between 1 and 20 characters, inclusive.
- All elements of map will contain the same number of characters.
- Each character in map will be either '#', '.' or 'T'.
- Each element of map will contain either 0 or 2 'T' characters.
- r1 and r2 will each be between -10^15 and 10^15, inclusive.
- c1 and c2 will each be between 0 and W-1, inclusive, where W is the number of characters in each element of map.
- Cells (r1,c1) and (r2,c2) will both be free cells.
- Cells (r1,c1) and (r2,c2) will be distinct.
{"#...##",
".##...",
"..#.##",
"#.#.##"}
1
0
5
3
Returns: 7
The optimal path is drawn below. Here 'S' means the start cell, 'F' means the finish cell and 'P' means an intermediate cell. #...## S##... PP#.## #P#.## #PPP## .##F.. ..#.## #.#.## Note that the labyrinth is infinite, so only its part with rows 0 to 7, inclusive, is shown here and in subsequent examples.
{"##.#.",
".#T#T",
"...#.",
"##.#."}
7
4
1
0
Returns: 9
##.#. F#T#T PPP#. ##P#. ##P#. .#T#T ...#P ##.#S Here we need to use a teleport once.
{"..######.#",
".###T###.T",
"..T#.##T##",
".######..#"}
1
0
6
4
Returns: 11
..######.# S###T###.T PPT#.##T## .######PP# ..######P# .###T###PT ..T#F##T## .######..# Here we need to use a teleport twice.
{"..#..",
".#.#.",
"....."}
-29
2
19
2
Returns: 54
{".#.#.",
"..#..",
".....",
".....",
"..#.."}
-999
3
100
2
Returns: -1
Submissions are judged against all 106 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class InfiniteLab with a public method long long getDistance(vector<string> map, long long r1, int c1, long long r2, int c2) · 106 test cases · 2 s / 256 MB per case