CCC '18 S3 - RoboThieves

View as PDF

Submit solution

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

Problem type
Canadian Computing Competition: 2018 Stage 1, Senior #3

A robot has stolen treasure from a factory and needs to escape without getting caught. The factory can be modelled by an N by M grid, where the robot can move up, down, left, or right.

Each cell of the grid is either empty, a wall, a camera, a conveyor, or the robot’s initial position. The robot can only walk on empty cells (denoted by .) or conveyors. The first row, last row, first column and last column of the grid consists of walls (denoted by W), and there may be walls in other cells.

Conveyors cause the robot to move in a specific direction, denoted by L, R, U, D for left, right, up, down respectively. The robot is unable to move on its own while on a conveyor. It is possible that the robot can become stuck forever on conveyors.

Cameras (denoted by C) can see in all four directions up, down, left, and right, but cannot see through walls. The robot will be caught if it is in the same cell as a camera or is seen by a camera while not on a conveyor. Conveyors are slightly elevated, so the robot cannot be caught while on a conveyor, but cameras can see empty cells on the other side of conveyors.

The robot is initially at the cell denoted by S. The exit could be at any of the empty cells. For each empty cell, determine the minimum number of steps needed for the robot to move there without being caught, or determine that it is impossible to move there. A step consists of moving once up, down, left or right. Being moved by a conveyor does not count as a step.

Input Specification

The first line of input contains two integers N and M (4 \le N, M \le 100). The next N lines of input will each contain M characters, each of which is one of the eight characters W, ., C, S, L, R, U, or D.

There will be exactly one S character and at least one . character. The first and last character of every row and column will be W.

For 5 of the 15 marks available, there are no cameras or conveyors.

For an additional 5 of the 15 marks available, there are no conveyors.

Output Specification

For each empty cell, print one line with one integer, the minimum number of steps for the robot to move to this empty cell without being caught or -1 if it is impossible to move to this empty cell.

The output should be in row major order; the order of empty cells seen if the input is scanned line by line top-to-bottom and then left-to-right on each line. See the sample outputs for examples of row major order output.

Sample Input 1

4 5

Sample Output 1


Explanation for Sample Output 1

The robot cannot move to the top left empty cell because it is blocked by walls.

The top right empty cell can be reached in 2 steps and the bottom right empty cell can be reached in 1 step.

Sample Input 2

5 7

Sample Output 2


Explanation for Sample Output 2

The empty cell to immediate left of the robot is seen by the camera so the robot cannot move there.

The empty cell right below the R conveyor is also seen by the camera as conveyors do not block the the sight of cameras.

Note that the robot can use the U and L conveyors to avoid the getting caught by the camera.

If the robot moves to the R conveyor, it will become stuck forever there.


  • 1
    device  commented on Oct. 16, 2019, 2:57 p.m.

    I copied tzak_el's code. Please delete my account. Thank you!

  • 5
    Unturned3  commented on Feb. 2, 2019, 4:00 a.m.

    Hello, I'm getting WA for Batch 5 Case 2, and I couldn't find any bugs with my program. Are there any special cases/pitfalls that I'm unaware of? I already included code to handle the situation where the robot spawns in the sight of a camera, and I couldn't think of any other corner cases.

    Thanks for helping!

    • 3
      NewAccount  commented on Aug. 6, 2019, 3:36 p.m. edit 4

      Consider The Case:

      4 6  

      Output Should Be:


  • 2
    gandhiisback  commented on Jan. 23, 2019, 1:36 p.m.

    I don't think the DMOJ judge is reflective of the CCC Grader. My python code works fine on CCC Grader but TLEs on DMOJ.

    • 1
      Riolku  commented on Jan. 23, 2019, 7:22 p.m.

      Although this is the case, if your approach is correct, you can certainly solve this problem on DMOJ.

    • 7
      wleung_bvg  commented on Jan. 23, 2019, 4:35 p.m.

      Try submitting with PyPy

      • 4
        Kirito  commented on Jan. 23, 2019, 11:07 p.m.

        The CCC grader uses PYPY instead of CPython.

  • 0
    p1geon  commented on Jan. 14, 2019, 12:34 a.m.

    Can a conveyor lead to a wall?

    • 2
      JustinXu  commented on Jan. 14, 2019, 7:57 a.m.


  • 1
    Aarya  commented on Oct. 21, 2018, 10:16 p.m.

    Any way to make java code faster? TLE on a couple cases with large inputs.

  • -1
    MowMowChow  commented on Oct. 12, 2018, 6:14 p.m.

    Ideas on why I'm getting an 'Invalid Return'? (any help would be much appreciated :D )

  • 1
    Cameron  commented on July 6, 2018, 1:51 p.m.

    When I upload the program, there is a tle, and rte, and sometimes, no output. I don't understand, because it seems to work when I test the program on my ide. What's going on?

  • 7
    GeoHD  commented on Feb. 27, 2018, 8:24 p.m. edited

    There is one scenario where the robot spawns right next to a camera.

    • 3
      Plasmatic  commented on Oct. 2, 2018, 2:41 p.m. edited

      I heard that tripped quite a few people on the CCC

  • -1
    william9518  commented on Feb. 18, 2018, 9:03 p.m.

    I'm getting 10/15 due to Batch #3 saying "clipped". What does this mean? What is the correct answer for the first test case of Batch #3?

    • 3
      aeternalis1  commented on Feb. 18, 2018, 10:56 p.m.

      When you fail a test case and your program has output (with partial output enabled), you are allowed to see a limited amount of your output. As for what you are getting wrong, I suggest you think about possible edge cases. If you ever need in-depth help, visit the dmoj slack channel at

  • 0
    Darcy_Liu  commented on Feb. 18, 2018, 4:22 p.m.

    Can't get partial on this one