Mapa de distancia mínima en taxi


13

Descripción del desafío

En la métrica del taxi , una distancia entre dos puntos ingrese la descripción de la imagen aquíse define como:

ingrese la descripción de la imagen aquí

Considere una matriz con solo ceros y unos:

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

Asignemos cada uno 1a la distancia a la diferente más cercana 1en la matriz (por supuesto, suponiendo que la distancia entre dos filas / columnas adyacentes es igual a 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

Para este desafío, dada una matriz, encuentre su mapa de distancias como se muestra arriba.

Ejemplos

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

Notas

  • Puede tomar la entrada en cualquier formato razonable
  • Aparte de 0y 1puede elegir dos valores distintos
  • Puede suponer que una matriz no está vacía (contiene al menos una fila de longitud mayor o igual a 1), así como que es rectangular
  • También puede suponer que hay al menos dos valores verdaderos presentes en la matriz (de lo contrario, la salida no está definida)
  • Puedes escribir un programa completo o una función
  • En lugar de devolver una nueva matriz, puede modificar una existente
  • Este es el , así que haga que su byte cuente lo más bajo posible

3
Solicitando más ejemplos por favor.
Urna mágica del pulpo

99
Oh, una solución de taxi probablemente sería sobrevalorada> _>
Sr. Xcoder

¿Podemos tomar la entrada como un entero (dando el ancho) y una matriz plana?
Arnauld

1
@ Mr.Xcoder Una solución de taxi también sería una pesadilla ... suspira [abre TIO]
Engineer Toast

1
@Arnauld me parece un formato razonable
shooqie

Respuestas:


5

MATL , 23 22 bytes

tt0*&v&fht1&ZPtg/X<yf(

Pruébalo en línea! O verificar todos los casos de prueba .

Explicación

Para ver resultados intermedios, puede ser útil insertar X#(mostrar pila) entre dos declaraciones.

t      % Implicit input: matrix. Duplicate
t0*    % Duplicate and multiply by 0. Gives a matrix of zeros
&v     % Concatenate vertically to input matrix. This is needed
       % in case the input is a row vector, because in that case the
       % subsequent &f would give row vectors, not column vectors
&f     % Push two column vectors with the row and column indices of
       % nonzero elements
h      % Concatenate into a 2-column matrix. Each row gives the
       % coordinates of a nonzero entry in the input matrix
t      % Duplicate
1&ZP   % Taxicab distance between the two copies of the 2-column
       % matrix containing the coordinates. The diagonal contains
       % zeros (distance of each point to itself)
tg     % Duplicate, convert to logical (turn nonzeros into 1)
/      % Divide, element-wise. This leaves off-diagonal entries in
       % the distance matrix unchanged, and sets the diagonal to NaN
X<     % Minimum of each column. NaN values are ignored. The 
       % resulting vector contains the new values to be written in
       % the input matrix
y      % Duplicate from below: push input matrix again
f      % Linear indices of nonzero entries
(      % Assignment indexing: write specified values at specified
       % positions. Implicitly display

1
Eso es asombroso. No es un comando de reorganización de una sola pila, a pesar de 4 tsy noM
Sanchises

5

Taxi , 14652 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 r 1 l 2 r.Pickup a passenger going to Auctioneer School.Pickup a passenger going to Chop Suey.Go to Auctioneer School:n 1 r.1 is waiting at Starchild Numerology.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 l.Pickup a passenger going to Rob's Rest.Pickup a passenger going to Bird's Bench.Go to Rob's Rest:w 1 r 2 l 1 r.Go to Bird's Bench:s.Go to Chop Suey:n 1 r 1 l 2 r 1 r 3 r.[a]Switch to plan "d" if no one is waiting.Pickup a passenger going to Crime Lab.'|' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 3 l.Pickup a passenger going to Crime Lab.Go to Zoom Zoom:n.Go to Crime Lab:w 1 l 2 r.Switch to plan "b" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Rob's Rest:s 1 r 1 l 1 l 1 r 1 r.Pickup a passenger going to Firemouth Grill.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 l 1 r 2 l.Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 4 l.Pickup a passenger going to Rob's Rest.Go to Rob's Rest:n 1 r 2 r 1 r.Go to Bird's Bench:s.Pickup a passenger going to Addition Alley.Go to Firemouth Grill:n 1 r 1 l 1 r 1 l 1 r.Go to Cyclone:w 1 l 1 r 2 r.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to Bird's Bench.Go to Bird's Bench:n 1 l 1 l 1 l 2 r 1 l.Switch to plan "c".[b]Go to Rob's Rest:s 1 r 1 l 1 l 1 r 1 r.Pickup a passenger going to Cyclone.Go to Bird's Bench:s.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 r 1 l 2 l.Pickup a passenger going to Trunkers.Pickup a passenger going to Addition Alley.Pickup a passenger going to Sunny Skies Park.Go to Trunkers:s 1 l.1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 2 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Rob's Rest.Go to Cyclone:n 1 l 1 l.Pickup a passenger going to Bird's Bench.Go to Sunny Skies Park:n 1 r.Go to Rob's Rest:s 2 r 1 r.Go to Bird's Bench:s.[c]Go to Chop Suey:n 1 r 1 l 2 r 1 r 3 r.Switch to plan "a".[d]Go to Rob's Rest:n 1 l 3 l 1 l 2 r 1 r.Pickup a passenger going to Firemouth Grill.Go to Bird's Bench:s.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:n 1 r 1 l 1 r 1 l 1 r.Go to Auctioneer School:w 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 4 l.Pickup a passenger going to Auctioneer School.Pickup a passenger going to Chop Suey.Go to Auctioneer School:n 1 r.0 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 l.Pickup a passenger going to Addition Alley.Go to Chop Suey:w 1 r 3 r 1 r 3 r.[e]Switch to plan "g" if no one is waiting.Pickup a passenger going to Cyclone.Go to Zoom Zoom:n 1 l 3 r.Go to Cyclone:w.Pickup a passenger going to Crime Lab.'|' is waiting at Writer's Depot.Go to Writer's Depot:s.Pickup a passenger going to Crime Lab.Go to Zoom Zoom:n.Go to Crime Lab:w 1 l 2 r.Switch to plan "f" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Cyclone:n 4 l 2 l.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 l 2 l 1 r.Go to Chop Suey:e 1 l 4 r 1 l.Switch to plan "e".[f]Go to Cyclone:n 4 l 2 l.Pickup a passenger going to Joyless Park.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 2 l 2 r.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Addition Alley.Go to Joyless Park:n 1 r 1 r 2 l.Go to Chop Suey:w 1 r 1 r 1 l.Switch to plan "e".[g]Go to Addition Alley:n 1 l 2 l.Pickup a passenger going to Rounders Pub.Go to Rounders Pub:n 1 r 1 r 2 r 1 l.Go to Joyless Park:n 1 r.Pickup a passenger going to KonKat's.[h]Switch to plan "i" if no one is waiting.Pickup a passenger going to KonKat's.Go to Zoom Zoom:w 1 r 2 l 2 r.Go to KonKat's:w 1 l 2 r.Pickup a passenger going to KonKat's.Go to Joyless Park:s 2 l.Switch to plan "h".[i]Go to KonKat's:w 1 r.Pickup a passenger going to Tom's Trims.Go to Tom's Trims:s 3 r 1 l.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 r 1 l 1 l 2 l.Pickup a passenger going to Joyless Park.Go to Joyless Park:w 1 r 2 r 1 r 2 l 4 r.[j]Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 2 l 2 l.Pickup a passenger going to Joyless Park.Pickup a passenger going to The Underground.Go to Joyless Park:n 2 r 2 r 2 l.Go to Tom's Trims:w 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 r 1 l 2 r.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Chop Suey.Go to Tom's Trims:s 1 l 2 r 1 l.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Go to The Underground:s 1 r 1 l.[k]Switch to plan "l" if no one is waiting.Pickup a passenger going to The Underground.Go to Chop Suey:n 2 r 1 l.Pickup a passenger going to Firemouth Grill.Go to Zoom Zoom:n 1 l 3 r.Go to Firemouth Grill:w 3 l 2 l 1 r.Go to The Underground:e 1 l.Switch to plan "k".[l]Go to Chop Suey:n 2 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 r 1 l.Go to Chop Suey:n 6 r 1 l.[m]Switch to plan "o" if no one is waiting.Pickup a passenger going to Firemouth Grill.Switch to plan "n" if no one is waiting.Pickup a passenger going to Firemouth Grill.Switch to plan "n" if no one is waiting.Pickup a passenger going to Firemouth Grill.[n]Go to Zoom Zoom:n 1 l 3 r.Go to Firemouth Grill:w 3 l 2 l 1 r.Go to Chop Suey:e 1 l 4 r 1 l.Switch to plan "m".[o]Go to The Babelfishery:s 1 r 1 l.Pickup a passenger going to The Underground.Go to The Underground:n.Switch to plan "p" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 r.Go to Joyless Park:e 1 l 3 r.Switch to plan "q".[p]' 0' is waiting at Writer's Depot.Go to Writer's Depot:s 2 r 1 l 2 l.Pickup a passenger going to Heisenberg's.Go to Heisenberg's:n 3 r 3 r.Go to Joyless Park:s 1 r 1 l 1 l.Switch to plan "4".[q]Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 2 l 2 l.Pickup a passenger going to Joyless Park.Pickup a passenger going to The Underground.Go to Joyless Park:n 2 r 2 r 2 l.Go to Trunkers:w 1 l 2 r 1 l.[r]Pickup a passenger going to Cyclone.Go to Sunny Skies Park:w 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l.Pickup a passenger going to Trunkers.Pickup a passenger going to Rob's Rest.Go to Trunkers:s 1 l.Go to Rob's Rest:w 1 l 1 r 1 r.Go to Cyclone:s 1 l 1 l 2 l.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to Bird's Bench.Go to Zoom Zoom:n.Go to Sunny Skies Park:w 2 l.Go to Bird's Bench:s 2 r 1 l.Go to The Underground:n 1 r 1 l 1 r 1 r 2 l.Switch to plan "s" if no one is waiting.Pickup a passenger going to The Underground.Go to Rob's Rest:s 2 r 1 l 1 l 1 r 1 r.Pickup a passenger going to Firemouth Grill.Go to Bird's Bench:s.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:n 1 r 1 l 1 r 1 l 1 r.Go to Trunkers:w 1 l 1 r.Switch to plan "r".[s]Go to Rounders Pub:n 1 l 1 l.Pickup a passenger going to Cyclone.Go to Joyless Park:n 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 2 l 2 l.Pickup a passenger going to Rounders Pub.Pickup a passenger going to What's The Difference.Pickup a passenger going to Joyless Park.Go to Rounders Pub:n 2 r 2 r 2 r 1 l.Go to Joyless Park:n 1 r.Go to Cyclone:w 1 r 2 l 2 l.Pickup a passenger going to What's The Difference.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 2 l 2 r.Pickup a passenger going to Addition Alley.Go to What's The Difference:w 1 r 3 r 1 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:e 2 r.Pickup a passenger going to The Underground.Go to The Underground:n 1 r 1 r.[t]Switch to plan "u" if no one is waiting.Pickup a passenger going to The Underground.Go to Trunkers:s 2 r 1 l.Pickup a passenger going to Trunkers.Go to Sunny Skies Park:w 1 r.Pickup a passenger going to Sunny Skies Park.Go to Zoom Zoom:n 1 r.Go to The Underground:w 1 l 2 r.Switch to plan "t".[u]Go to Trunkers:s 2 r 1 l.Go to Sunny Skies Park:w 1 r.Go to Tom's Trims:s 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 r 1 l 2 r.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Chop Suey.Go to Tom's Trims:s 1 l 2 r 1 l.Go to Chop Suey:n 1 r 1 l 4 r 1 l.[v]Switch to plan "0" if no one is waiting.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 r 1 l.Pickup a passenger going to The Underground.Go to The Underground:n.Switch to plan "z" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 r.Go to Trunkers:w 1 l 1 r.Pickup a passenger going to Cyclone.Go to Rob's Rest:w 1 l 1 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 l 1 l 2 l.Pickup a passenger going to Trunkers.Pickup a passenger going to What's The Difference.Pickup a passenger going to Rob's Rest.Go to Trunkers:s 1 l.Go to Rob's Rest:w 1 l 1 r 1 r.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 l 1 r 2 l.Pickup a passenger going to Addition Alley.Go to Cyclone:w 1 r 4 l.Pickup a passenger going to What's The Difference.Go to Zoom Zoom:n.Go to What's The Difference:w 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Multiplication Station.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to The Underground.Go to The Underground:n 1 r 1 r.Switch to plan "w" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 r.-1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 1 r 1 l 2 l.Pickup a passenger going to Multiplication Station.Go to The Underground:w 1 r 2 r 1 r 2 l.[w]Go to Multiplication Station:s 1 l 1 r.Go to Sunny Skies Park:s 1 r 2 l 1 r.Pickup a passenger going to Cyclone.Go to Bird's Bench:s 2 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 r 1 l 2 l.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to What's The Difference.Pickup a passenger going to Bird's Bench.Go to Sunny Skies Park:n 1 r.Go to Bird's Bench:s 2 r 1 l.1 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 r 1 r 2 l.Pickup a passenger going to Addition Alley.Go to Cyclone:w 1 r 4 l.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Multiplication Station.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to The Underground.Go to The Underground:n 1 r 1 r.Switch to plan "x" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 r.-1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 1 r 1 l 2 l.Pickup a passenger going to Multiplication Station.Go to The Underground:w 1 r 2 r 1 r 2 l.[x]Go to Multiplication Station:s 1 l 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Addition Alley.-1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 r 2 l 1 l 2 l.Pickup a passenger going to Multiplication Station.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:n 1 r 1 r 3 l 1 r.Pickup a passenger going to Addition Alley.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 r 2 l 1 l 2 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to The Underground.Go to The Underground:n 1 r 1 r.Switch to plan "y" if no one is waiting.Pickup a passenger going to Narrow Path Park.[y]Go to Narrow Path Park:n 4 l.Go to Chop Suey:e 1 r 1 l 1 r.Switch to plan "v".[z]Go to Trunkers:s 2 r 1 l.Pickup a passenger going to Trunkers.Go to Sunny Skies Park:w 1 r.Pickup a passenger going to Sunny Skies Park.Go to Zoom Zoom:n 1 r.Go to Trunkers:w 3 l.Go to Sunny Skies Park:w 1 r.Go to Chop Suey:n 1 r 1 r 3 r.Switch to plan "v".[0]Go to Rob's Rest:n 1 l 3 l 1 l 2 r 1 r.Pickup a passenger going to Firemouth Grill.Go to Bird's Bench:s.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:n 1 r 1 l 1 r 1 l 1 r.Go to Narrow Path Park:e 1 l 4 l.Pickup a passenger going to Writer's Depot.[1]Switch to plan "3" if no one is waiting.Pickup a passenger going to Cyclone.Go to Zoom Zoom:w 1 l 1 r 1 r.Go to Writer's Depot:w.Pickup a passenger going to Magic Eight.Go to Cyclone:n.Pickup a passenger going to Magic Eight.Go to Magic Eight:s 1 l 2 r.Switch to plan "2" if no one is waiting.Pickup a passenger going to Writer's Depot.Go to Cyclone:n 1 l 2 r.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 l 2 l 1 r.Go to Narrow Path Park:e 1 l 4 l.Switch to plan "1".[2]Go to Cyclone:n 1 l 2 r.Pickup a passenger going to Writer's Depot.Go to Narrow Path Park:n 2 r 1 l 1 r.Switch to plan "1".[3]Go to Writer's Depot:w 1 l 1 r 2 l.Pickup a passenger going to The Babelfishery.' ' is waiting at Writer's Depot.Pickup a passenger going to KonKat's.Go to The Babelfishery:n 1 r 2 r 1 r.Pickup a passenger going to KonKat's.Go to KonKat's:n.Pickup a passenger going to Heisenberg's.Go to Heisenberg's:n 1 r 1 r.Go to Joyless Park:s 1 r 1 l 1 l.[4]Pickup a passenger going to Magic Eight.Go to Rounders Pub:w 2 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 r 1 l 2 l 2 l.Pickup a passenger going to Rounders Pub.Pickup a passenger going to Magic Eight.Go to Rounders Pub:n 2 r 2 r 2 r 1 l.Go to Magic Eight:n 1 r 1 r 2 r.Switch to plan "5" if no one is waiting.Pickup a passenger going to Addition Alley.1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 1 l 2 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Joyless Park.Go to Joyless Park:n 1 r 1 r 2 l.Switch to plan "j".[5]Go to Auctioneer School:w 1 l 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 3 r 1 r 3 r.[6]Switch to plan "9" if no one is waiting.Pickup a passenger going to Crime Lab.'|' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 3 l.Pickup a passenger going to Crime Lab.Go to Zoom Zoom:n.Go to Crime Lab:w 1 l 2 r.Switch to plan "7" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:n 1 l.'\n' is waiting at Writer's Depot.Go to Writer's Depot:w 1 l 1 r 2 l.Pickup a passenger going to KonKat's.Go to KonKat's:n 3 r 2 r.Switch to plan "8".[7]Go to Heisenberg's:n 5 r 1 l.Pickup a passenger going to KonKat's.Go to KonKat's:n 1 l 1 l.[8]Pickup a passenger going to KonKat's.Go to Chop Suey:n 1 r 1 r.Switch to plan "6".[9]Go to KonKat's:n 1 l 1 l.Pickup a passenger going to Post Office.Go to Post Office:s 3 r 1 l.

Pruébalo en línea!

Supongo que ningún ser humano quiere intentar leer ese código, ¡así que pruébelo en línea con comentarios, etiquetas significativas y resultados más legibles!


Entrada: cadena que representa una matriz. La cadena debe estar compuesto por 3 personajes: 0, |representando rompe filas, y cualquier dígito mayor positivo de un (PDGTO). Me gusta usar 8. OP permite la entrada en cualquier formato razonable y creo que esto califica. La cadena no tiene que formatearse en una matriz rectangular, pero se supondrá que está alineada a la izquierda, sea lo que sea.

Salida: Matriz de valores que muestran la distancia mínima de taxi desde cada uno PDGTOa su otro más cercano PDGTO.

Ejemplo:

OP's Input:         OP's Output:
-------------       -------------
0 0 0 0 0 0 1       0 0 0 0 0 0 2
0 1 0 1 0 0 0       0 1 0 2 0 0 0
1 1 0 0 0 0 1       1 1 0 0 0 0 2
0 0 1 0 0 0 0       0 0 2 0 0 0 0
0 0 0 0 0 1 0       0 0 0 0 0 3 0
0 1 0 0 0 0 0       0 2 0 0 0 0 0
1 0 0 0 0 0 0       2 0 0 0 0 0 0
0 0 0 1 0 0 1       0 0 0 3 0 0 3

Taxi Input: 0000008|0808000|8800008|0080000|0000080|0800000|8000000|0008008

Taxi Output: (golfed)
------------------------------------
 0 0 0 0 0 0 2.000000
 0 1.000000 0 2.000000 0 0 0
 1.000000 1.000000 0 0 0 0 2.000000
 0 0 2.000000 0 0 0 0
 0 0 0 0 0 3.000000 0
 0 2.000000 0 0 0 0 0
 2.000000 0 0 0 0 0 0
 0 0 0 3.000000 0 0 3.000000

Taxi Output: (ungolfed version)
------------------------------------
 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 2.000000
 0.000000 1.000000 0.000000 2.000000 0.000000 0.000000 0.000000
 1.000000 1.000000 0.000000 0.000000 0.000000 0.000000 2.000000
 0.000000 0.000000 2.000000 0.000000 0.000000 0.000000 0.000000
 0.000000 0.000000 0.000000 0.000000 0.000000 3.000000 0.000000
 0.000000 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000
 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
 0.000000 0.000000 0.000000 3.000000 0.000000 0.000000 3.000000

El segundo formato de salida es claramente mucho más fácil de leer, pero también agrega 7 bytes al programa y , después de todo, esto es . Hubiera sido posible regresar al final y eliminar todos los ceros finales en lugar de simplemente rellenar los 0valores, pero eso habría agregado aún más bytes y se consideró un costo inaceptable.


Aquí está el texto del código sin golf con comentarios, mejores etiquetas y una salida más clara:
(Es lo mismo que obtienes con el enlace de arriba).

[ Minimal Taxi Cab Distance ]
[ Inspired by: /codegolf//q/138311 ]

[ INPUT:  String of 8 & 0 using a pipe | as a delineator for each row of the array                  ]
[ OUTPUT: String formatted as an array showing min taxi distance from each 8 to the nearest other 8 ]
[ Note:   Any single digit > 1 may be substituted for 8. I.E., it must be 2 <= i <= 9               ]

[ PROCESS:                                                                 ]
[ Store STDIN exactly as input and keep it around until the end            ]
[ Store 3 arrays: Input, X Positions, Y Positions                          ]
[ Store a count of how many elements are in Input                          ]
[ For each element in Input, create an array of minimal taxi cab distances ]
[ Find the minimum value greater than zero in that array and store it      ]
[ Pad with a space and, if it's the last element in the row, a line break  ]
[ Repeat for each element, storing the minimums each time                  ]
[ Concatenate all the results and output                                   ]

[ DATA                     LOCATION              REASON                    ]
[ Input                    Auctioneer School     Close to Cyclone          ]
[ Input w/o Delineator     Tom's Trims           Running out of options    ]
[ Elements in Input        Rounders Pub          Close to Joyless Park     ]
[ Iteration                Joyless Park          Close to The Underground  ]
[ X Positions              Trunkers              Close to Rob's Rest       ]
[ Y Positions              Sunny Skies Park      Close to Bird's Bench     ]
[ Element's X              Rob's Rest            Easy to remember          ]
[ Element's Y              Bird's Bench          Easy to remember          ]
[ Array of distances       Narrow Path Park      LIFO is OK for this       ]
[ Array of minimums        Heisenberg's          Close to KonKat's         ]

[ Note: We use the Input w/o Delineator to keep track of iteration             ]
[ By storing it at Chop Suey, we can keep going until we run out of passengers ]

[ Note: When using arrays other than the Input w/o Delineator, is it critical  ]
[ that we clone each element and enque it again. The iteration tracking will   ]
[ let us stop before we go through the array again.                            ]

[ Some things I learned about Taxi in TIO while writing this:                            ]
[  - You can store any passenger at Writer's Depot and Starchild Numerology              ]
[  - You can store any passenger at Heisenberg's and even pick them up again *in order*  ]
[    (You don't start picking up random integers until that queue is gone)               ]
[  - If you try to drop someone at Rob's Rest or Bird's Bench but it's already occupied, ]
[    it won't error out. The passenger just stays in the Taxi which will probably cause  ]
[    a "Too Many Passengers" error eventually. It cost me a week figuring that one out.  ]




[ Pickup stdin and split it into elements ]
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st right 1st left 2nd right.
Pickup a passenger going to Auctioneer School.
Pickup a passenger going to Chop Suey.
Go to Auctioneer School: north 1st right.
1 is waiting at Starchild Numerology.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st left.
Pickup a passenger going to Rob's Rest.
Pickup a passenger going to Bird's Bench.
Go to Rob's Rest: west 1st right 2nd left 1st right.
Go to Bird's Bench: south.
Go to Chop Suey: north 1st right 1st left 2nd right 1st right 3rd right.

[ Create the arrays of X & Y positions ]
[NextInputElement]
Switch to plan "XYCreated" if no one is waiting.
Pickup a passenger going to Crime Lab.
'|' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 3rd left.
Pickup a passenger going to Crime Lab.
Go to Zoom Zoom: north.
Go to Crime Lab: west 1st left 2nd right.
Switch to plan "NextColumn" if no one is waiting.

[ Next Row ]
[ Set X = 1 and Y = Y + 1 ]
Pickup a passenger going to Firemouth Grill.
Go to Rob's Rest: south 1st right 1st left 1st left 1st right 1st right.
Pickup a passenger going to Firemouth Grill.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st left 1st right 2nd left.
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 4th left.
Pickup a passenger going to Rob's Rest.
Go to Rob's Rest: north 1st right 2nd right 1st right.
Go to Bird's Bench: south.
Pickup a passenger going to Addition Alley.
Go to Firemouth Grill: north 1st right 1st left 1st right 1st left 1st right.
Go to Cyclone: west 1st left 1st right 2nd right.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: north 2nd right 1st right.
Pickup a passenger going to Bird's Bench.
Go to Bird's Bench: north 1st left 1st left 1st left 2nd right 1st left.
Switch to plan "EndOfElement".

[NextColumn]
[ Set X = X + 1, Store X & Y positions ]
Go to Rob's Rest: south 1st right 1st left 1st left 1st right 1st right.
Pickup a passenger going to Cyclone.
Go to Bird's Bench: south.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st right 1st left 2nd left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Sunny Skies Park.
Go to Trunkers: south 1st left.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 2nd left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Rob's Rest.
Go to Cyclone: north 1st left 1st left.
Pickup a passenger going to Bird's Bench.
Go to Sunny Skies Park: north 1st right.
Go to Rob's Rest: south 2nd right 1st right.
Go to Bird's Bench: south.

[EndOfElement]
Go to Chop Suey: north 1st right 1st left 2nd right 1st right 3rd right.
Switch to plan "NextInputElement".

[XYCreated]
[ Clean up the leftovers ]
Go to Rob's Rest: north 1st left 3rd left 1st left 2nd right 1st right.
Pickup a passenger going to Firemouth Grill.
Go to Bird's Bench: south.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: north 1st right 1st left 1st right 1st left 1st right.

[ Create the Input w/o Delineator string ]
Go to Auctioneer School: west 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 4th left.
Pickup a passenger going to Auctioneer School.
Pickup a passenger going to Chop Suey.
Go to Auctioneer School: north 1st right.
0 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st left.
Pickup a passenger going to Addition Alley.
Go to Chop Suey: west 1st right 3rd right 1st right 3rd right.

[NextInputWithoutDelineatorElement]
Switch to plan "DelineatorRemoved" if no one is waiting.
Pickup a passenger going to Cyclone.
Go to Zoom Zoom: north 1st left 3rd right.
Go to Cyclone: west.
Pickup a passenger going to Crime Lab.
'|' is waiting at Writer's Depot.
Go to Writer's Depot: south.
Pickup a passenger going to Crime Lab.
Go to Zoom Zoom: north.
Go to Crime Lab: west 1st left 2nd right.
Switch to plan "NotAPipe" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Cyclone: north 4th left 2nd left.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st left 2nd left 1st right.
Go to Chop Suey: east 1st left 4th right 1st left.
Switch to plan "NextInputWithoutDelineatorElement".

[NotAPipe]
Go to Cyclone: north 4th left 2nd left.
Pickup a passenger going to Joyless Park.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 2nd left 2nd right.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Addition Alley.
Go to Joyless Park: north 1st right 1st right 2nd left.
Go to Chop Suey: west 1st right 1st right 1st left.
Switch to plan "NextInputWithoutDelineatorElement".

[DelineatorRemoved]
Go to Addition Alley: north 1st left 2nd left.
Pickup a passenger going to Rounders Pub.
Go to Rounders Pub: north 1st right 1st right 2nd right 1st left.
Go to Joyless Park: north 1st right.
Pickup a passenger going to KonKat's.

[NextBit]
Switch to plan "EndOfBits" if no one is waiting.
Pickup a passenger going to KonKat's.
Go to Zoom Zoom: west 1st right 2nd left 2nd right.
Go to KonKat's: west 1st left 2nd right.
Pickup a passenger going to KonKat's.
Go to Joyless Park: south 2nd left.
Switch to plan "NextBit".

[EndOfBits]
Go to KonKat's: west 1st right.
Pickup a passenger going to Tom's Trims.
Go to Tom's Trims: south 3rd right 1st left.

[ Start with the first character ]
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st right 1st left 1st left 2nd left.
Pickup a passenger going to Joyless Park.
Go to Joyless Park: west 1st right 2nd right 1st right 2nd left 4th right.



[ At this point, we have the following data structure: ]
[ Input                           Auctioneer School    ]
[ Input w/o Delineator (IwoD)     Tom's Trims          ]
[ Elements in Input               Rounders Pub         ]
[ Iteration = 1                   Joyless Park         ]
[ X Positions                     Trunkers             ]
[ Y Positions                     Sunny Skies Park     ]
[                                                      ]
[ Now, we use Joyless Park to track our starting point ]
[ and IwoD to iterate through all the destinations. If ]
[ the starting point's value is <=1, then add '0 ' to  ]
[ Heisenberg's and go to the next element. If value    ]
[ is >1, then calculate the distance to every other    ]
[ element. IwoD will start full and be emptied as we   ]
[ iterate through all the possible destinations. Once  ]
[ that's done, cycle back through those results until  ]
[ we find the smallest value >0 and add that to        ]
[ Heisenberg's. Either way, in order to move to        ]
[ the next starting point, we add 1 to Joyless Park,   ]
[ check that it's less than Rounders Pub, and loop. If ]
[ it's not less, then we're done so we can go build    ]
[ the output with the passengers at Heisenberg's.      ]
[ We'll use the Auctioneer School to find pipes so we  ]
[ can add line breaks to the output as needed.         ]

[NextElementForStartingPoint]

[ Extract this element's value ]
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 2nd left 2nd left.
Pickup a passenger going to Joyless Park.
Pickup a passenger going to The Underground.
Go to Joyless Park: north 2nd right 2nd right 2nd left.
Go to Tom's Trims: west 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st right 1st left 2nd right.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Chop Suey.
Go to Tom's Trims: south 1st left 2nd right 1st left.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.
Go to The Underground: south 1st right 1st left.

[GetTheNextElement]
Switch to plan "FoundThisElement" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Chop Suey: north 2nd right 1st left.
Pickup a passenger going to Firemouth Grill.
Go to Zoom Zoom: north 1st left 3rd right.
Go to Firemouth Grill: west 3rd left 2nd left 1st right.
Go to The Underground: east 1st left.
Switch to plan "GetTheNextElement".

[FoundThisElement]
Go to Chop Suey: north 2nd right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st right 1st left.
Go to Chop Suey: north 6th right 1st left.

[TrashNextElement]
Switch to plan "ElementsAllTrashed" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Switch to plan "AllRiders" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Switch to plan "AllRiders" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
[AllRiders]
Go to Zoom Zoom: north 1st left 3rd right.
Go to Firemouth Grill: west 3rd left 2nd left 1st right.
Go to Chop Suey: east 1st left 4th right 1st left.
Switch to plan "TrashNextElement".

[ElementsAllTrashed]
Go to The Babelfishery: south 1st right 1st left.
Pickup a passenger going to The Underground.
Go to The Underground: north.
Switch to plan "ItsAZero" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st right.
Go to Joyless Park: east 1st left 3rd right.
Switch to plan "ExtractXY".

[ItsAZero]
' 0.000000' is waiting at Writer's Depot.
Go to Writer's Depot: south 2nd right 1st left 2nd left.
Pickup a passenger going to Heisenberg's.
Go to Heisenberg's: north 3rd right 3rd right.
Go to Joyless Park: south 1st right 1st left 1st left.
Switch to plan "GotoNextStartingPoint".

[ Extract the X & Y values for this element ]
[ExtractXY]
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 2nd left 2nd left.
Pickup a passenger going to Joyless Park.
Pickup a passenger going to The Underground.
Go to Joyless Park: north 2nd right 2nd right 2nd left.
Go to Trunkers: west 1st left 2nd right 1st left.

[GetNextXY]
Pickup a passenger going to Cyclone.
Go to Sunny Skies Park: west 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Rob's Rest.
Go to Trunkers: south 1st left.
Go to Rob's Rest: west 1st left 1st right 1st right.
Go to Cyclone: south 1st left 1st left 2nd left.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to Bird's Bench.
Go to Zoom Zoom: north.
Go to Sunny Skies Park: west 2nd left.
Go to Bird's Bench: south 2nd right 1st left.
Go to The Underground: north 1st right 1st left 1st right 1st right 2nd left.
Switch to plan "FoundXY" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Rob's Rest: south 2nd right 1st left 1st left 1st right 1st right.
Pickup a passenger going to Firemouth Grill.
Go to Bird's Bench: south.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: north 1st right 1st left 1st right 1st left 1st right.
Go to Trunkers: west 1st left 1st right.
Switch to plan "GetNextXY".

[FoundXY]
[ Keep rotating the arrays until they're back to normal ]
Go to Rounders Pub: north 1st left 1st left.
Pickup a passenger going to Cyclone.
Go to Joyless Park: north 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 2nd left 2nd left.
Pickup a passenger going to Rounders Pub.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to Joyless Park.
Go to Rounders Pub: north 2nd right 2nd right 2nd right 1st left.
Go to Joyless Park: north 1st right.
Go to Cyclone: west 1st right 2nd left 2nd left.
Pickup a passenger going to What's The Difference.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 2nd left 2nd right.
Pickup a passenger going to Addition Alley.
Go to What's The Difference: west 1st right 3rd right 1st left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: east 2nd right.
Pickup a passenger going to The Underground.
Go to The Underground: north 1st right 1st right.

[RotateXY]
Switch to plan "XYareRotated" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Trunkers: south 2nd right 1st left.
Pickup a passenger going to Trunkers.
Go to Sunny Skies Park: west 1st right.
Pickup a passenger going to Sunny Skies Park.
Go to Zoom Zoom: north 1st right.
Go to The Underground: west 1st left 2nd right.
Switch to plan "RotateXY".


[XYareRotated]
[ At this point, we have the following data structure: ]
[ Input                           Auctioneer School    ]
[ Input w/o Delineator (IwoD)     Tom's Trims          ]
[ Elements in Input               Rounders Pub         ]
[ Iteration                       Joyless Park         ]
[ X Positions                     Trunkers             ]
[ Y Positions                     Sunny Skies Park     ]
[ Element's X                     Rob's Rest           ]
[ Element's Y                     Bird's Bench         ]

[ Now we need to compute the distance to each point greater than one ]
[ IwoD will start full and be emptied as we iterate through all the  ]
[ possible destinations. Once that's done, cycle back through those  ]
[ results until we find the smallest value >0 and add that to the    ]
[ result at Heisenberg's.                                            ]

[ Finish the RotateXY process by dropping off the passengers ]
[ They have been carried around to raise money for gas       ]
Go to Trunkers: south 2nd right 1st left.
Go to Sunny Skies Park: west 1st right.

[ Split IwoD into pieces ]
Go to Tom's Trims: south 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st right 1st left 2nd right.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Chop Suey.
Go to Tom's Trims: south 1st left 2nd right 1st left.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.

[NextDestination]
Switch to plan "EndOfDestinations" if no one is waiting.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st right 1st left.
Pickup a passenger going to The Underground.
Go to The Underground: north.
Switch to plan "DestinationIsZero" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st right.

[ Calculate the X distance ]
Go to Trunkers: west 1st left 1st right.
Pickup a passenger going to Cyclone.
Go to Rob's Rest: west 1st left 1st right 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st left 1st left 2nd left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to Rob's Rest.
Go to Trunkers: south 1st left.
Go to Rob's Rest: west 1st left 1st right 1st right.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st left 1st right 2nd left.
Pickup a passenger going to Addition Alley.
Go to Cyclone: west 1st right 4th left.
Pickup a passenger going to What's The Difference.
Go to Zoom Zoom: north.
Go to What's The Difference: west 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st right.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Multiplication Station.
Go to Addition Alley: north 2nd right 1st right.
Pickup a passenger going to The Underground.
Go to The Underground: north 1st right 1st right.
Switch to plan "XDiffIsNegative" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st right.
-1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 1st right 1st left 2nd left.
Pickup a passenger going to Multiplication Station.
Go to The Underground: west 1st right 2nd right 1st right 2nd left.
[XDiffIsNegative]
Go to Multiplication Station: south 1st left 1st right.

[ Calculate the Y distance ]
Go to Sunny Skies Park: south 1st right 2nd left 1st right.
Pickup a passenger going to Cyclone.
Go to Bird's Bench: south 2nd right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st right 1st left 2nd left.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to Bird's Bench.
Go to Sunny Skies Park: north 1st right.
Go to Bird's Bench: south 2nd right 1st left.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: north 1st right 1st right 2nd left.
Pickup a passenger going to Addition Alley.
Go to Cyclone: west 1st right 4th left.
Pickup a passenger going to What's The Difference.
Go to What's The Difference: north 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st right.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Multiplication Station.
Go to Addition Alley: north 2nd right 1st right.
Pickup a passenger going to The Underground.
Go to The Underground: north 1st right 1st right.
Switch to plan "YDiffIsNegative" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st right.
-1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 1st right 1st left 2nd left.
Pickup a passenger going to Multiplication Station.
Go to The Underground: west 1st right 2nd right 1st right 2nd left.
[YDiffIsNegative]
Go to Multiplication Station: south 1st left 1st right.

[ Add the two negative distances and multiply by -1 ]
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Addition Alley.
-1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st right 2nd left 1st left 2nd left.
Pickup a passenger going to Multiplication Station.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station: north 1st right 1st right 3rd left 1st right.

[ Do not record the distance if it's zero     ]
[ i.e., if start and destination are the same ]
Pickup a passenger going to Addition Alley.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st right 2nd left 1st left 2nd left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to The Underground.
Go to The Underground: north 1st right 1st right.
Switch to plan "IgnoreZeroDistance" if no one is waiting.
Pickup a passenger going to Narrow Path Park.
[IgnoreZeroDistance]
Go to Narrow Path Park: north 4th left.
Go to Chop Suey: east 1st right 1st left 1st right.
Switch to plan "NextDestination".

[DestinationIsZero]
[ Rotate X & Y and keep going ]
Go to Trunkers: south 2nd right 1st left.
Pickup a passenger going to Trunkers.
Go to Sunny Skies Park: west 1st right.
Pickup a passenger going to Sunny Skies Park.
Go to Zoom Zoom: north 1st right.
Go to Trunkers: west 3rd left.
Go to Sunny Skies Park: west 1st right.
Go to Chop Suey: north 1st right 1st right 3rd right.
Switch to plan "NextDestination".

[EndOfDestinations]
[ Clean up the leftovers ]
Go to Rob's Rest: north 1st left 3rd left 1st left 2nd right 1st right.
Pickup a passenger going to Firemouth Grill.
Go to Bird's Bench: south.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: north 1st right 1st left 1st right 1st left 1st right.

[ Run through all the passengers are Narrow Path Park  ]
[ None of them should be 0 because we dropped those    ]
[ Find the smallest value and store it at Heisenberg's ]
Go to Narrow Path Park: east 1st left 4th left.
Pickup a passenger going to Writer's Depot.

[CheckNextDistance]
Switch to plan "FoundMinDistance" if no one is waiting.
Pickup a passenger going to Cyclone.
Go to Zoom Zoom: west 1st left 1st right 1st right.
Go to Writer's Depot: west.
Pickup a passenger going to Magic Eight.
Go to Cyclone: north.
Pickup a passenger going to Magic Eight.
Go to Magic Eight: south 1st left 2nd right.
Switch to plan "SecondIsLess" if no one is waiting.

[ Keep the first passenger b/c it's less and dump the second ]
Pickup a passenger going to Writer's Depot.
Go to Cyclone: north 1st left 2nd right.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st left 2nd left 1st right.
Go to Narrow Path Park: east 1st left 4th left.
Switch to plan "CheckNextDistance".

[SecondIsLess]
[ Go pickup the second passenger's clone b/c it's less or equal ]
Go to Cyclone: north 1st left 2nd right.
Pickup a passenger going to Writer's Depot.
Go to Narrow Path Park: north 2nd right 1st left 1st right.
Switch to plan "CheckNextDistance".

[FoundMinDistance]
[ Store the results ]
Go to Writer's Depot: west 1st left 1st right 2nd left.
Pickup a passenger going to The Babelfishery.
' ' is waiting at Writer's Depot.
Pickup a passenger going to KonKat's.
Go to The Babelfishery: north 1st right 2nd right 1st right.
Pickup a passenger going to KonKat's.
Go to KonKat's: north.
Pickup a passenger going to Heisenberg's.
Go to Heisenberg's: north 1st right 1st right.
Go to Joyless Park: south 1st right 1st left 1st left.

[GotoNextStartingPoint]
[ Start at Joyless Park ]
Pickup a passenger going to Magic Eight.
Go to Rounders Pub: west 2nd left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st right 1st left 2nd left 2nd left.
Pickup a passenger going to Rounders Pub.
Pickup a passenger going to Magic Eight.
Go to Rounders Pub: north 2nd right 2nd right 2nd right 1st left.
Go to Magic Eight: north 1st right 1st right 2nd right.
Switch to plan "GoDoOutput" if no one is waiting.

[ We're not done yet so start over ]
Pickup a passenger going to Addition Alley.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 1st left 2nd left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Joyless Park.
Go to Joyless Park: north 1st right 1st right 2nd left.
Switch to plan "NextElementForStartingPoint".



[ At this point, we have the following data structure: ]
[ (The queues not listed here aren't needed any more)  ]
[ Input                           Auctioneer School    ]
[ Array of minimums               Heisenberg's         ]

[GoDoOutput]
[ Split the original input at Chop Suey ]
Go to Auctioneer School: west 1st left 1st left.
Pickup a passenger going to Chop Suey.
Go to Chop Suey: north 3rd right 1st right 3rd right.

[OutputNextResult]
Switch to plan "PrintOutput" if no one is waiting.
Pickup a passenger going to Crime Lab.
'|' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 3rd left.
Pickup a passenger going to Crime Lab.
Go to Zoom Zoom: north.
Go to Crime Lab: west 1st left 2nd right.
Switch to plan "OutputThisResult" if no one is waiting.

[ Output line breaks where every pipe was in the input ]
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: north 1st left.
'\n' is waiting at Writer's Depot.
Go to Writer's Depot: west 1st left 1st right 2nd left.
Pickup a passenger going to KonKat's.
Go to KonKat's: north 3rd right 2nd right.
Switch to plan "GoToNextOutput".

[OutputThisResult]
[ Output the actual result, which will include space padding ]
Go to Heisenberg's: north 5th right 1st left.
Pickup a passenger going to KonKat's.
Go to KonKat's: north 1st left 1st left.

[GoToNextOutput]
Pickup a passenger going to KonKat's.
Go to Chop Suey: north 1st right 1st right.
Switch to plan "OutputNextResult".

[PrintOutput]
Go to KonKat's: north 1st left 1st left.
Pickup a passenger going to Post Office.
Go to Post Office: south 3rd right 1st left.

@ Mr.Xcoder ¿Estás feliz ahora?
Engineer Toast


2

Perl 5 , 197 bytes

@a=map{[/\d/g]}<>;for$r(0..$#a){for$c(0..$#{$a[$r]}){$d=@a*@{$a[0]}*$a[$r][$c];for$y(0..$#a){$a[$r][$c]=$d=$a[$y][$_]&&($t=abs($y-$r)+abs($_-$c))&&$t<$d?$t:$d for 0..$#{$a[$y]}}}}$,=$";say@$_ for@a

Pruébalo en línea!


3
Parece que el resaltador SE para Perl necesita muchas mejoras.

@ThePirateBay $#, básicamente ...
Erik the Outgolfer

2

JavaScript (ES6), 97 bytes

Toma entrada en la sintaxis de curry (w)(a), donde w es el ancho de la matriz y a es una matriz plana de ceros y unos. Devuelve otra matriz plana.

w=>a=>a.map((c,i)=>c*Math.min(...a.map((d,j)=>d&&A((j/w|0)-(i/w|0))+A(j%w-i%w)||1/0)),A=Math.abs)

Casos de prueba

Este fragmento incluye una función auxiliar para formatear la salida para facilitar la lectura.


1

Octava, 59 bytes

for f=find(a=input(''))'a(f)=0;a(f)=bwdist(a,'ci')(f);end;a

Toma la entrada como una matriz 2D.

Pruébalo en línea!

Explicación:

Para cada ubicación de 1s que se encuentre en la matriz, configúrela en cero y calcule la transformación de la distancia de la cuadra de la ciudad de la matriz y finalmente configure la ubicación en la distancia calculada.



Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.