13
1
Challenge description
In taxicab metric, a distance between two points is defined as:
Consider a matrix with only zeros and ones:
0 0 0 1 0
1 0 0 0 0
0 0 0 0 0
1 0 0 1 0
1 0 0 0 0
Let's map each 1
to the distance to nearest different 1
in the matrix (of course, assuming that distance between two adjacent rows/columns is equal to 1):
0 0 0 3 0
2 0 0 0 0
0 0 0 0 0
1 0 0 3 0
1 0 0 0 0
For this challenge, given a matrix, find it's map of distances as shown above.
Examples
0 0 1
0 0 0
0 0 0
0 1 0
0 0 0
0 0 4
0 0 0
0 0 0
0 4 0
0 0 0
-----
0 0 0 0 0 0 1
0 1 0 1 0 0 0
1 1 0 0 0 0 1
0 0 1 0 0 0 0
0 0 0 0 0 1 0
0 1 0 0 0 0 0
1 0 0 0 0 0 0
0 0 0 1 0 0 1
0 0 0 0 0 0 2
0 1 0 2 0 0 0
1 1 0 0 0 0 2
0 0 2 0 0 0 0
0 0 0 0 0 3 0
0 2 0 0 0 0 0
2 0 0 0 0 0 0
0 0 0 3 0 0 3
-----
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
-----
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1
22 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 22
-----
0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 9 0 0 0 0 0 0 0 0 2 0 0 0 4
0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0
-----
1 1
1 1
Notes
- You may take input in any reasonable format
- Apart from
0
and1
you may choose any two distinct values - You may assume that a matrix is non-empty (contains at least one row of length larger or equal to 1), as well that it is rectangular
- You may also assume there are at least two truthy values present in the matrix (otherwise, output is undefined)
- You may write a full program, or a function
- Instead of returning a new matrix, you may modify an existing one
- This is code-golf, so make your byte count as low as possible!
3Requesting more examples please. – Magic Octopus Urn – 2017-08-09T20:29:34.140
9Oh, a taxi solution would probably be overvoted >_> – Mr. Xcoder – 2017-08-09T20:33:32.883
Can we take input as an integer (giving the width) and a flat array? – Arnauld – 2017-08-09T20:43:53.727
1@Mr.Xcoder A Taxi solution would also be a nightmare... sighs [opens TIO] – Engineer Toast – 2017-08-09T21:03:35.790
1@Arnauld sounds like reasonable format to me – shooqie – 2017-08-09T21:05:26.327
Can the input be a Boolean matrix (true / false instead of 1 / 0)? – Luis Mendo – 2017-08-09T22:03:46.403
@LuisMendo it literally says so in the notes – shooqie – 2017-08-10T11:02:25.167