28
6
Suppose we have a matrix like this:
11111
12221
12321
12221
11111
This matrix represents a terrain, and each cell represents a portion of terrain. The number in each cell represents the time the portion of terrain needs to be completely burnt (in minutes, if a measurement unit is needed), according to its combustibility. If a fire starts at any given position (cell), that cell needs to be completely burnt before the fire propagates to the adjacent cells (horizontal and vertical only, not diagonal). So, if a fire is started at the center position, the fire needs:
11111 11111 11111 11011 10001 00000
12221 3 m. 12221 2 m. 12021 1 m. 11011 1 m. 00000 1 m. 00000
12321 -----> 12021 -----> 10001 -----> 00000 -----> 00000 -----> 00000
12221 12221 12021 11011 00000 00000
11111 11111 11111 11011 10001 00000
Explanation:
- Fire starts at [2,2] (0-based), which has a burn time of 3.
- After 3 minutes, [1,2],[2,1],[2,3],[3,2] start to burn.
- After 2 minutes, those cells end burning and fire propagates to all adjacent cells, but [0,2],[2,0],[2,4],[0,4] need only 1 more minute to burn, so
- After 1 minute, those cells are burnt and the cell propagates to their adjacent cells.
- After 1 more minute, the rest of cells from step 3 end burning and fire propagates to their adjacent cells (that are already burnt, so nothing happens).
- After 1 last minute, fire ends burning the whole terrain.
So the solution to that case is 8 minutes. If the fire starts in the top leftmost cell [0,0]:
11111 01111 00111 00011 00001 00000
12221 1 12221 1 02221 1 01221 1 00121 1 00011 1
12321 --> 12321 --> 12321 --> 02321 --> 01321 --> 00321 -->
12221 12221 12221 12221 02221 01221
11111 11111 11111 11111 11111 01111
00000 00000 00000 00000 00000
00000 1 00000 1 00000 1 00000 1 00000
00221 --> 00110 --> 00000 --> 00000 --> 00000
00221 00121 00020 00010 00000
00111 00011 00001 00000 00000
So now the total time is 10 minutes.
The challenge
Given a NxM matrix (N>0, M>0) of integer values that represent the time every cell needs to be completely consumed, write the shortest program/function that takes that matrix and a pair of integers with the position the fire starts in, and returns/prints the time needed for the fire to completely consume the whole terrain.
- Every cell will have a positive (non-zero) burn time. You cannot assume a maximum value for the cells.
- The matrix does not need to be square nor symmetric.
- The matrix can be 0-indexed or 1-indexed, as you like.
- The position can be given as a single parameter with a tuple of integers, two separate parameters of whatever other reasonable format.
- The dimensions of the matrix cannot be specified as input parameters.
- You do not need to output every intermediate step, just the amount of time asked. But I won't complain if the steps are visualized in any way.
Another example:
Fire starts at [1,1] (a '>' represents a minute):
4253 4253 4253 4153 4043 3033 2023 0001 0000
2213 > 2113 > 2013 > 1003 > 0002 > 0001 > 0000 >> 0000 > 0000
1211 1211 1211 1111 1001 0000 0000 0000 0000
Output: 9
This is code-golf, so may the shortest program for each language win!
1@LeanderMoesinger it has to work with any matrix. What I mean is that your program or function cannot accept the dimensions of the matrix as input parameters, but of course you can calculate those dimensions inside your code. – Charlie – 2017-06-26T18:15:51.027
Can the input be taken as a single number in column-major order? That is, matrix entries are numbered down, then across
– Luis Mendo – 2017-06-29T13:37:27.3101@LuisMendo yes, of course. But note that the burning time of every cell can be greater than 9, if that matters for the "single number" part. – Charlie – 2017-06-29T13:40:36.277
Thanks. No, it doesn't matter. I meant a single number but possibly with several digits. The number will range from
1
toM*N
– Luis Mendo – 2017-06-29T13:50:37.037