Identical grids

4

1

You’re given two \$r×c\$ grids. Each cell contains either 0 or 1. What are the minimum number of swaps (between horizontally and vertically adjacent cell elements, no wrapping i.e no swapping between last and first element of a row) are required in the first grid for it to match the second. If the matched arrangement can never be achieved, output -1.

Constraints

\$1 \leq r \leq 100\$
\$1 \leq c \leq 100 \$

Examples

input:
00  
11  

01  
10  
output:
1  

input:
00  
11  

01  
00  
output:
-1  

input:
0011011  

0101101  
output:
2 

Kiara Dan

Posted 2019-08-19T16:44:44.067

Reputation: 161

I have a strong feeling that this is a duplicate but can't find one... – Giuseppe – 2019-08-19T16:57:25.980

1

Also, I highly recommend using The Sandbox to get feedback on a challenge. I do think this is an interesting challenge, but you only stand to gain by posting there first, and at worst you lose a day or two.

– Giuseppe – 2019-08-19T16:59:39.380

2What is a swap? Please define it more rigorously. – HyperNeutrino – 2019-08-19T17:02:16.483

1Is it adjacent? Any two items? Cycling any n items? – wizzwizz4 – 2019-08-19T17:03:01.717

2@HyperNeutrino, by swap I mean exchanging the values of two adjacent cells. – Kiara Dan – 2019-08-19T17:07:46.203

@wizzwizz4, swap between adjacent elements, no cycles. – Kiara Dan – 2019-08-19T17:08:29.570

3Guys, cmon, this challenge is clear enough. Don't make it a clown fiesta. I don't have any problem with understanding the challenge. – Krzysztof Szewczyk – 2019-08-19T17:27:00.083

1When you say "adjacent" do you include diagonally adjacent? – Luis Mendo – 2019-08-19T17:30:27.297

@LuisMendo, only horizontally and vertically adjacent. – Kiara Dan – 2019-08-19T18:19:24.863

2Is this a question from another site? – xnor – 2019-08-19T19:35:01.840

1@KrzysztofSzewczyk Even after you commented that, there was still a clarification to be made. So, stop berating others for requesting clarification; just because (you think) you get it doesn't mean others have to. – HyperNeutrino – 2019-08-20T13:55:45.340

@HyperNeutrino I feel like these guys never played three-in-a-row game - you can request clarification - but sometimes the challenge is clear enough, and if you don't get it just abadon it because you are making people angry and not helping – Krzysztof Szewczyk – 2019-08-20T14:51:33.723

1@KrzysztofSzewczyk If a challenge isn't clear and we can't request clarification, the alternative method is to VTC it, which is much worse of a solution for an almost perfectly fine challenge otherwise. I don't see OP getting angry; if you are, you have every right to ignore our comments. – HyperNeutrino – 2019-08-20T16:11:12.410

Answers

5

Python 3, 383 334 bytes

-6 bytes thanks to @Kevin Cruijssen

-43 bytes thanks to @Jitse

import numpy
z=len
d=lambda a,b,c,d:abs(a-c)+abs(b-d)
def g(l,m):
 if z(l)==1:yield d(*l[0],*m[0])
 for i in range(z(l)):
  for s in g(l[1:],m[:i]+m[i+1:]):yield d(*l[0],*m[i])+s
def c(a,b):
 e=a-b;k=z(a);f,*h=[],
 for x in range(k*z(a[0])):w=[(x%k,x//k)];v=e[x%k][x//k];f+=w*(v>0);h+=w*(v<0)
 return-1 if numpy.sum(e)else min(g(f,h))

Try it online!

Takes input as two numpy arrays.

Explanation:

First, e=a-b determines which positions change from one grid to the other. The next line finds all of the differences and sorts them into the lists f and h. If the sum of differences is not zero, meaning there are more 1s in one grid than the other, this returns -1, or the minimum of all possible paths found by g.

This is the fun part. Essentially, in order to make this work, a 1 must move from one position to another by swapping. Therefore, the minimum number of swaps for each pair is the manhattan distance between them, found in d. g finds every possible pairing between start and end points and returns a list of the total distances between them. If there is only one start point, it returns the distance between that and the end point. Beyond that, it pairs the first start point with each end point iteratively and adds their distance to the total distance of the rest of the points, calculated recursively.

Hiatsu

Posted 2019-08-19T16:44:44.067

Reputation: 679

1-6 bytes with some simple golfs (removed some spaces, and changed !=0 to >0) – Kevin Cruijssen – 2019-08-20T07:44:24.697

-43 bytes – Jitse – 2019-08-20T09:24:37.057

If I'm understanding this correctly, does it run on O(n!)? Obviously fine for golf, but just out of curiosity do you know if there's a more efficient algorithm? – Jonah – 2019-08-21T02:40:52.647

1@Jonah Technically it's O(n!!) in the total number of spaces to be swapped minus 1 (135*7...). I'm nearly certain that a more efficient algorithm exists, since this is brute forcing every possible solution. – Hiatsu – 2019-08-21T18:12:40.647

1

Based on @Hiatsu's answer.

Python 3, 262 bytes

e=enumerate
def f(r,s):
 if not r:yield 0
 for i,t in e(r):yield abs(t[0]-s[0][0])+abs(t[1]-s[0][1])+min(f(r[:i]+r[i+1:],s[1:]))
def g(x,y):
 z=x-y;n=len(x.T);v,*w=[],
 for i,p in e(z.flat):t=[(i%n,i//n)];v+=t*(p>0);w+=t*(p<0)
 return-1if z.sum()else min(f(v,w))

Try it online!

spenceryue

Posted 2019-08-19T16:44:44.067

Reputation: 111