DMOPC '14 Contest 1 P5 - Surprise Teleport

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

However, you quickly realize that Mr. Sidhu can't walk through walls! Therefore, you must now somehow include these obstacles into your program. The input will include a grid that will consist of O (representing an empty space) and X (representing an obstacle or otherwise unavailable space). You should note that each grid space is equal to one metre in length. It is guaranteed that the starting and destination points will be available (O, that is) and that the Main Office can be reached by walking.

What he does not know, however, is that the school's former physics teacher installed invisible teleportation devices over the summer as to provide a more efficient means for teachers to move around. She also configured it such that it would only work for Mr. Sidhu and no one else.

Unfortunately, she forgot about the devices — and she retired. As Mr. Sidhu walks down the hall, he is instantly teleported to the Main Office, his destination. Since he doesn't particularly enjoy being randomly teleported, he contacted the teacher and convinced her to remove the devices.

Being the curious student you are, you happened to stumble over a copy of the map of the teleportation devices on the teacher's desk of the classroom you have physics in, which contains the exact location of the devices as well as mentions that it would only affect Mr. Sidhu.

Since you are also a very compassionate student, you decide to show him the map but are disappointed when he tells you that the devices no longer exist. Therefore, to make him regret his decision, you decide to write a program that calculates exactly how much time Mr. Sidhu could have saved had he known the exact locations of the devices.

Input Specification

The first line of input will consist of the number of rows and columns R, C (1 \le R, C \le 1000), separated by one space. The next line represents his starting point and the following line is the Main Office. Note that rows and columns in this problems are zero-indexed. This is followed by R lines containing the grid as described above.

The next line of input will be T (0 \le T \le R\times C), the number of teleportation devices. This will be followed by T lines, consisting of two positive integers each separated by one space representing the coordinates of that device, the former being the row number and the latter the column number. You can assume that teleportation devices will not be on walls.

Output Specification

Your output should be the time he could have gained, in seconds.

Sample Input

5 5
4 4
1 2
OOXOO
OXOXO
OOOXX
XOXOX
OOOOO
1
3 3

Sample Output

5

Explanation for Sample Input

There is one device at 3, 3. He starts at 4, 4, the bottom-right of the grid. The length of the shortest path ignoring (walking over the tile) the device is 7 metres. Walking at 1 m/s means it will take him 7 seconds. But a teleportation device is only 2 metres away, thus allowing him to complete his journey in 2 seconds. The output should be 5, as he would have saved 5 seconds.


Comments


  • 2
    kevze  commented on June 17, 2020, 11:11 a.m. edited

    It should be noted that one of the cases includes a path where Mr. Sidhu's only path to the main office crosses a teleporter.

    Edit: for example

    5 5 4 4 1 2 OOXOO OXOXO OOOXX XOXOX OOOOO 1 3 1

    And correct output is 3.


  • 4
    RyanLi  commented on Jan. 10, 2020, 12:54 p.m.

    Note: He can start directly on a teleporter


  • 8
    Firebrand  commented on July 26, 2019, 5:32 p.m.

    Could be worth mentioning that the coordinates are from index 0 and not 1 - that tripped me up a little bit.


  • -1
    AlanCCCL2S18  commented on March 30, 2018, 3:36 p.m.

    Can he walk diagonally or not?


    • 3
      wleung_bvg  commented on March 30, 2018, 8:24 p.m.

      No, he cannot walk diagonally. Only up, down, left, and right (if there is no wall).


  • 17
    hezeyu2007  commented on March 25, 2018, 1:36 p.m.

    So is saving 5 seconds of his life so important for Mr.Sidhu? :)


  • 8
    max  commented on Dec. 13, 2017, 7:35 p.m.

    Clarification

    Will the amount of time Mr. Sidhu could have saved always be a positive integer?

    ie. If there was only one portal farther away from the starting point than the main office, would we output a negative integer?

    thanks


    • 12
      aeternalis1  commented on Dec. 13, 2017, 8:46 p.m.

      If the portal is further away than the main office, he would simply go straight into the office and never take the portal, saving 0 time. Thus you would output 0.