CCC '18 S3 - RoboThieves

View as PDF

Submit solution


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

Author:
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
WWWWW
W.W.W
WWS.W
WWWWW

Sample Output 1

-1
2
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
WWWWWWW
WD.L.RW
W.WCU.W
WWW.S.W
WWWWWWW

Sample Output 2

2
1
3
-1
-1
1

Explanation for Sample Output 2

The empty cell to the 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 sight of cameras.

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

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


Comments


  • 1
    jo3_l  commented on Sept. 25, 2021, 5:41 p.m. edited

    My solution is WAing on the 2nd test case of batch 5, but I'm not sure what's wrong. I have checked all related comments and am handling all edge cases I know of.

    I've also tested my solution locally using the official test data and there was no difference between my output and the expected output (checked via diff), which leads me to suspect that my code is relying upon undefined behavior somewhere. Unfortunately, I'm not skilled enough to find it.

    If someone could help, that'd be great. Thanks!


    • 5
      lilweege  commented on Sept. 26, 2021, 3:15 a.m.

      The judge server compiles your program with -O2, which in your case seems to cause problems with g++; specifically on line 132 and 133 (storing your index in a variable rather than inline seems to fix it). Regardless though, submitting your original solution with clang++ seems to pass just fine.


      • 0
        jo3_l  commented on Sept. 26, 2021, 2:27 p.m. edit 3

        Ugh. Thank you so much, really appreciate it. In hindsight, I'm actually somewhat surprised that it doesn't WA earlier / at all on Clang.


  • 1
    theMang  commented on Aug. 24, 2021, 8:45 a.m.

    I hate this problem


  • 0
    WSM_simon  commented on Aug. 23, 2021, 6:38 p.m.

    Anyone cannot pass batch 5 Considering this case

    6 6
    WWWWWW
    WRRRDW
    WU.WDW
    WUWSDW
    WULLLW
    WWWWWW

    the answer should be

    -1

  • 6
    zhenga1  commented on Feb. 10, 2021, 8:38 a.m. edit 4

    Also, guys, there is a very annoying test case where the conveyor forms a loop and the robot is stuck on conveyors forever. It is batch 3 test 2. I got maximum recursion depth exceeded.


  • 1
    stevenning203  commented on Jan. 8, 2021, 8:29 p.m. edit 5

    How do I check the distances when using a BFS using a queue? I am currently storing the distances inside structs but I have to reset the whole matrix every time I BFS and I think it's causing TLE. Also, I don't know how to make it so that the robot prioritizes taking the path with a bunch of conveyors because my program treats conveyors as normal points but it doesn't add one to the distance so sometimes it thinks that the shortest path is the path with a bunch of dots instead of a path with a conveyor.

    EDIT: I used while loops to fix the shortest path conveyor issue but I still don't know how to store distances the optimal way. Could someone guide me? Thanks.


    • 1
      DynamicSquid  commented on Jan. 8, 2021, 10:31 p.m. edit 3

      For the distances, you can store a dist variable inside your struct, and every time you move to a cell, set that cell's distance to the current distance travelled.

      Also for conveyors, you can think of them as moving twice in the same step. And since this is a BFS, you are guaranteed to find the shortest path


      • 1
        stevenning203  commented on Jan. 8, 2021, 11:14 p.m. edited

        Yea that's what I did, I thought it would TLE but I only have 1 TLE and that's on batch 1 so I think storing the dist inside the structs was actually fine. I'm still not sure what to do about that 1 TLE though. Thanks for the response.

        Edit: Nevermind I finally managed to solve it by memoizing any dots passed by on any BFS so that if you BFS them again there is no need to call the function and instead you can just use an array lookup.


  • 1
    DynamicSquid  commented on Dec. 28, 2020, 6:52 p.m.

    I'm getting WA on Batch #3 Case #2 and Batch #5 Case #2. I read all the comments and made sure my code works for all special cases like when you start next to a camera, or a never ending conveyor. Is there something else I'm missing?


    • 3
      csDude256  commented on Dec. 28, 2020, 11:13 p.m.

      Consider the following test case:

      5 5
      WWWWW
      W..SW
      WC..W
      W.C.W
      WWWWW

      The output should be all -1s.


      • 2
        DynamicSquid  commented on Dec. 29, 2020, 12:53 p.m. edited

        Oh I see what I did wrong! I iterated through each camera, and then directly turned the invalid positions into walls.

        I should of stored the invalid positions in a separate array instead of modifying the grid directly.

        Thanks!


  • 4
    jasone  commented on July 29, 2020, 10:56 p.m. edited

    For anyone stuck on Batch 5 Case 2, make sure you have some code dealing with if the robot finds himself stuck on two conveyors and cannot escape (WLRW). (Besides also having some code to check if you spawn next to a camera) That was the issue for me.


  • 5
    devnarula  commented on June 16, 2020, 3:36 p.m.

    can someone assist, I am getting WA on case #2 of the last bacth


    • 3
      noYou  commented on June 16, 2020, 5:21 p.m.

      Did you check for the case where you spawn right beside the camera?


      • 1
        devnarula  commented on June 18, 2020, 12:26 p.m.

        thanks for your help. that was the only case i was missing


  • 2
    aaronhe07  commented on June 5, 2020, 2:09 p.m.

    Wow that took me about ten tries


    • 1
      Ibby  commented on June 8, 2020, 1:45 p.m.

      Same


  • 0
    bigboy2  commented on May 24, 2020, 6:56 p.m.

    Can someone take a look at my code I'm getting WA on case #1 of the last batch


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

    tzak_el cheesed this problem.


  • 8
    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!


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

      Consider The Case:

      4 6  
      WWWWWW
      W.LLSW
      W....W
      WWWWWW

      Output Should Be:

      1
      2
      3
      2
      1

      • 1
        TingHoMarcus  commented on Oct. 28, 2019, 8:38 a.m.

        Mine can past your testcase with no problem, but I am still failing that testcase


        • 5
          asdfg9240  commented on Jan. 17, 2020, 4:21 a.m.

          i also could solve NewAccount's test case but still got batch #5 case #2 wrong. make sure you can solve:

          4 4
          WWWW
          WSDW
          W..W
          WWWW
          
          
          4 4
          WWWW
          WS.W
          WR.W
          WWWW
          
          4 4
          WWWW
          WDSW
          W..W
          WWWW
          
          4 4
          WWWW
          W.SW
          W.LW
          WWWW

          etc.. both dots should be 1, one of them should not be 2.


        • 8
          Aaeria  commented on Oct. 29, 2019, 12:29 p.m.

          The test cases for this problem can be found at https://www.cemc.uwaterloo.ca/contests/computing/2019/index.html


  • 6
    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.

    https://dmoj.ca/submission/1214670

    https://gyazo.com/2035b158e7266b140435bc1d188128a7


    • 6
      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.


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

      Try submitting with PyPy


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

        The CCC grader uses PYPY instead of CPython.


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

    Can a conveyor lead to a wall?


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

      Indeed.


  • 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.


  • 0
    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 )


  • 0
    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?


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

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


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

      I heard that tripped quite a few people on the CCC


  • 0
    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? https://gyazo.com/6224aad6d59e8f8a5fe6e520db78f6f2


    • 7
      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 https://slack.dmoj.ca/.


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

    Can't get partial on this one


    • 58
      CopyPastePolice  commented on Oct. 13, 2018, 4:29 p.m. edited

      Darcy_Liu, YOU'RE UNDER ARREST

      It has come to our attention that you've been copying and pasting solutions on DMOJ. This does not show good sportsmanship and is unfair to DMOJistan. Blindly copying and pasting code does not improve your own abilities and breaks DMOJ's rules.

      Proof:

      Example 1: COCI '14 Contest 7 #3 ACM - https://dmoj.ca/problem/coci14c7p3

      Offender's Code: https://dmoj.ca/submission/1046085

      Source: https://dmoj.ca/src/1036320

      Example 2: CCC '10 S3 - Firehose - https://dmoj.ca/problem/ccc10s3

      Offender's Code: https://dmoj.ca/src/1026958

      Source: https://dmoj.ca/src/375814


      • 33
        Darcy___Liu  commented on June 12, 2019, 6:52 p.m. edit 2

        Darcy_Liu is the enemy of the people. He tries to copy code but the state is always watching.


        • 8
          DiscoverMeme  commented on April 8, 2020, 1:29 p.m.

          Darcy___Liu has betrayed the revolution.

          Edit 1:

          Do not believe their lies. Darcy_Liu did not copy code.

          Edit 2:

          Darcy_Liu is the enemy of the people. He tries to copy code but the state is always watching.


          • 1
            Darcy___Liu  commented on April 9, 2020, 8:48 p.m.

            It appears that you have yet to update your history books and databases. CopyPastePolice is an ally to the state and we are preparing an invasion of PETHCS.


      • -16
        dagsi  commented on Oct. 15, 2018, 9:55 a.m.

        This comment is hidden due to too much negative feedback. Click here to view it.


        • 19
          Plasmatic  commented on Oct. 15, 2018, 10:19 a.m.

          you have to have solved the problems first to view the source links


          • 11
            harry7557558  commented on April 5, 2020, 11:17 p.m.

            How does CopyPastePolice view other people's submissions with only 1 problem solved?


            • 47
              AQT  commented on April 6, 2020, 12:38 a.m.

              cuz ur mom lol


              • 9
                thomas_li  commented on April 6, 2020, 12:48 p.m.

                gottem


      • -5
        Plasmatic  commented on Oct. 13, 2018, 7:34 p.m. edited

        This comment is hidden due to too much negative feedback. Click here to view it.