Find correct positions in the Matrix

Asked

Viewed 193 times

0

I’m having difficulties with this reasoning. I have a matrix 4x4. I must fill it with 0 and 1, provided that the number 1 appears twice in each row and in each column, as in the example below.

1   1   0   0       
1   0   0   1       
0   0   1   1       
0   1   1   0

But the situation below can occur. Then I would have to move the "1" of (2,3) for (4,2) that will again be as in the example above.

1   1   0   0       
1   0   1   1       
0   0   1   1       
0   0   1   0

If I add the rows and columns together, I can identify which row and column is wrong, but I cannot make my program identify the exact position.

I need to give back a "move of (2,3) for (4,2)". How my program can find the exact positions?

  • 1

    How’s your current code? My suggestion - assuming the number of 0s and 1s is correct - is to search for the row and column with 1s more, and move that position to the row and column with more 0s. If the row sum is correct but the column sum is not (or vice versa), move a 1 in the row itself, from the column with more to the column with less.

1 answer

3


If I add the rows and columns together, I can identify which row and column is wrong, but I can’t get my program to identify the position exact.

A: Yes, the logic is to actually add each row and column, if the sum is greater than two(2) you will know the position of the row, column with more values or wherever there is more than 1’s, you should move to the position where the sum is less than two(2) this tells you exactly where is missing number 1, following your example the logic would be as follows:

For this matrix:

1   1   0   0       
1   0   1   1       
0   0   1   1       
0   0   1   0

Let’s start by adding up all the lines first !

Add the first line of the Matrix :

1   1   0   0  ==> soma == 2 (OK)

Add the second row of the Matrix :

1   0   1   1  ==> soma == 3 (segunda linha da matriz com problema tem número a mais) > 2 

Third line of the Matrix :

0   0   1   1  ==> soma == 2 (OK)

Fourth line of the Matrix:

0   0   1   0  ==> soma ==  1 (quarta linha da matriz com problema tem número a menos) <2

Time to add up the columns:

First column:

1        
1          ==> soma == 2 (OK)
0          
0  

Second column:

1        
0        ==> soma == 1 (problema) < 2
0       
0 

Third column:

0        
1        ==> soma == 3 (problema) > 2
1         
1

Fourth column:

0       
1       ==> soma == 2 (OK)
1       
0

Now note which row and column has sum greater than two (>2), it will be exactly the second row and third column position (2,3) you just find the positions with wrong values where if you have 1’s more than you should have, OK now to find for which position to move note which is the position whose value is less than two (<2) you will notice which will be the fourth row and second column (4,2), there is a logical way to get the result!

  • That! Messing around with so many matrix confuses me sometimes, but after this explanation became clearer and I was able to come up with a code here. Thank you/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.