An algorithm that determines whether a given binary matrix can be achieved by reversing the rows and columns of the matrix from them

I need help finding an algorithm that is as efficient as possible to check if a given binary matrix can be achieved by reversing the rows and columns of the matrix with just one. Whenever you flip a line or col, all 0 become 1 and all 1 become 0

An algorithm that determines whether a given binary matrix can be achieved by reversing the rows and columns of the matrix from those

for example, this matrix can be achieved by flipping the second row and then scrolling through the second column:

+---+---+---+
| 1 | 0 | 1 |
+---+---+---+
| 0 | 1 | 0 |
+---+---+---+
| 1 | 0 | 1 |
+---+---+---+

but this matrix cannot be what you do

+---+---+---+
| 1 | 0 | 0 |
+---+---+---+
| 0 | 1 | 0 |
+---+---+---+
| 0 | 0 | 1 |
+---+---+---+
+4
source share
2

:

. , , (). , , / , 1 .

, .

?

, , :

0: , .

, ( ): , .

, , . , . .

JavaScript, , :

function getFlips(matrix) {
    // Verification
    for (let i = 1; i < matrix.length; i++) {
        let flip = matrix[i][0] ^ matrix[0][0]; // XOR operation
        for (let j = 0; j < matrix[0].length; j++) {
            if (matrix[i][j] ^ flip != matrix[0][j]) return false; // Not possible
        }
    }
    // If we get here, it is possible: determine which rows/columns to flip
    let flips = { rows: [], columns: [] };
    for (let j = 0; j < matrix[0].length; j++) {
        if (matrix[0][j] == 0) flips.columns.push(j+1);
    }
    for (let i = 1; i < matrix.length; i++) {
        if (matrix[i][0] != matrix[0][0]) flips.rows.push(i+1);
    }
    return flips;
}


// I/O management
inp.oninput = function () {
    // Convert input to matrix of numbers
    let matrix = inp.value.split('\n').map(row => Array.from(row, Number));
    // Perform algorithm
    let flips = getFlips(matrix);
    // Output the result in human readable format
    out.textContent = flips 
        ? 'Number(s) of the column(s) to flip: ' 
            + (flips.columns.length ? flips.columns : 'none') + '\n' +
          'Number(s) of the row(s) to flip: ' 
            + (flips.rows.length ? flips.rows : 'none')
        : 'Not possible';
};

inp.oninput();
Enter the values of the matrix:<br>
<textarea id="inp" rows="4" cols="4">101
010
101</textarea><br>
Solution:
<pre id="out"></pre>
Hide result
+5

, N N (), N .

, K K . , K + 1- / K + 1'- , K + 1 K + 1. 4 :

  • K + 1- K + 1-
  • K + 1- , K + 1-
  • K + 1- , K + 1-
  • K + 1- , K + 1-

, K + 1 . , (K + 2), , . , N.

, , / , , .

:
1. 1x1, 1- /
2. 2x2, /
3. 3x3, /
...
N. NxN, N- /

0

Source: https://habr.com/ru/post/1694176/


All Articles