CDC12_E - External Falling Objects

no tags 

After Ronny finished the race challenge, he decided to go home. When he arrives, he notice that his family is not there, instead, there is a note that says: ”We are at the basement”. Ronny read this and decided to go to the basement. This one is located at some point of the backyard, but due to the collision and fusion of the planets, there are objects of the external planet falling from the sky, therefore, streets and gardens are full of these objects. These objects have different sizes and shapes, but always with the colors of the rainbow. Beside this, Ronny’s family will close the basement door after K minutes, to avoid a possible invasion of the new life forms that are arriving to the Earth. Ronny need to get to the basement before these K minutes. You must know that every object has a number, this number represents the time that Ronny will take to avoid it. Ronny can move to any of the adjacent position but never diagonal. Every step that Ronny make will take 1 second.

Input

The first line contains an integer T, which specifies the number of test cases. Then, will follow the descriptions of T test cases.

For each test case, there will be a line with 3 integers, H, W and K. H and W are the dimensions of the garden, and K is the time in seconds that Ronny’s family will wait until they lock the door. Then H lines will follow, with W characters each; you may know that:

  • The character ’.’ denotes a free space on the garden.
  • One digit character denotes an object. An object with the number X on it represents that Ronny will take X seconds to avoid it.
  • The character ’R’ indicates the initial position of Ronny in the garden.
  • The character ’D’ indicates the door of the basement where is located Ronny’s family.

Output

For each input case you must print a line with "Scenario #i: " where i is the number of the test case that you are evaluating (starting at 1), followed by the minimum time that Ronny will take to reach his family. If it’s impossible to Ronny to reach his family in less than K units of time, then you should print "Ronny will be destroyed". (Quotes for clarity)

Sample

Input:
2
2 2 2
R9
1D
3 3 10
R..
42.
D1.

Output:
Scenario #1: Ronny will be destroyed
Scenario #2: 6

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ H ≤ 1,000
  • 1 ≤ W ≤ 1,000
  • 1 ≤ K ≤ 8,000,000

hide comments
nadstratosfer: 2019-11-17 20:53:46

An efficient algorithm passes even in PyPy, so the constraints are very well chosen. Also cool story =) Enjoyed myself here, thanks.

Wajeeh Kapoor: 2013-01-05 21:34:26

What happens if H==1 && W==1 ?
Thanks !

:D: 2012-11-30 22:32:15

A very good problem. Time limit maybe is a little tight, but there is a very specific algo that proved to be efficient with those particular constraints.


Added by:Venezuelan Programming League
Date:2012-10-27
Time limit:0.800s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own problem used for UCV-CEIDEC contest.