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 | +---+---+---+
:
. , , (). , , / , 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>
, N N (), N .
, K K . , K + 1- / K + 1'- , K + 1 K + 1. 4 :
, K + 1 . , (K + 2), , . , N.
, , / , , .
:1. 1x1, 1- /2. 2x2, /3. 3x3, /...N. NxN, N- /
Source: https://habr.com/ru/post/1694176/More articles:invalid reference image Arkyt 1.5 - image-processingHow to get maximum value based on another series in pandas groupby - pythonImplementing real "forall" in scala futures list - scalafragment recyclerview different views do not work - javaWhy can I compare an object with IEnumerable of these objects, but not List? - castingWhy string comparison and authentication behave differently in pdb and python console - pythonRuby Methods BasicObject - methodsПочему отрицательный размер массива не является ошибкой компиляции, но вызывает java.lang.NegativeArraySizeException - javahow does a list actually work in python using for-loop? - pythonGet formatted resource string from xml layout - androidAll Articles