Say I have a grid with some obstacles:
----
| X |
| |
|XX |
| |
----
I can fill this with numbers, as distance from the top left corner. But, because there are obstacles in the way some cells will take further to reach than a straight line:
----
|01X5|
|1234|
|XX45|
|7656|
----
This can be done through flood filling:
---- ---- ---- ---- ---- ---- ---- ----
|0 X | |01X | |01X | |01X | |01X | |01X5| |01X5| |01X5|
| | |1 | |12 | |123 | |1234| |1234| |1234| |1234|
|XX | |XX | |XX | |XX | |XX4 | |XX45| |XX45| |XX45|
| | | | | | | | | | | 5 | | 656| |7656|
---- ---- ---- ---- ---- ---- ---- ----
Your challenge is to, given a binary matrix containing obstacles, flood fill it like so from the top left hand corner and output a corresponding matrix.
The first example of a grid is equivalent to the following matrix:
[ [0, 0, 1, 0],
[0, 0, 0, 0],
[1, 1, 0, 0],
[0, 0, 0, 0] ]
And the corresponding output would be:
[ [0, 1, X, 5],
[1, 2, 3, 4],
[X, X, 4, 5],
[7, 6, 5, 6] ]
Where X is any value that isn't a positive integer.
You may take any two consistent values in the matrix instead of 1 and 0, and you may choose any value to represent obstacles. Distances may be 0 or 1-indexed. You may assume the whole matrix can be flood filled from the top left corner, which cannot be an obstacle.
You must be able to support numbers with multiple digits.
This is code-golf, shortest wins!
Testcases
These use -1 as X.
[ [0, 0, 1, 0], [0, 0, 0, 0], [1, 1, 0, 0], [0, 0, 0, 0] ] -> [ [0, 1, -1, 5], [1, 2, 3, 4], [-1, -1, 4, 5], [7, 6, 5, 6] ]
[ [0, 0, 0] ] -> [ [0, 1, 2] ]
[ [0, 1, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0] ] -> [ [0, -1, 4, 5, 6, -1, 10], [1, 2, 3, -1, 7, 8, 9] ]