How to make all entries of 4x4 matrix to either 1 or 0 when only one operation is allowed? - matrix

I have a 4x4 matrix. Say
0 1 1 0
0 0 1 0
0 1 1 0
0 1 1 1
My task is to transform the matrix to either
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
or
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Single operation is defined as pick any element of the matrix and then replace the element and elements to its left, right, up and down with their xor with 1.
Example
0 1 1 0
0 0 1 0
0 1 1 0
0 1 1 1
operation on marked element will transform the matrix to
0 1 0 0
0 1 0 1
0 1 0 0
0 1 1 1
My objective is to calculate the minimum number of operations needed to get the final result.
I don't even understand under what category this problem lies? Is this branch and bound, bactracking or something else.

What you've described is called lights out puzzle. There's relevant OEIS that gives you the minimal number of nontrivial switch flippings needed to solve the all-ones lights out problem on an n X n square.
Wikipedia describes a general sketch of algorithm how to solve it. You might be also interested in this answer here on SO, which includes a link to example implementation. Citing the answer:
The idea is to set up a matrix representing the button presses a column vector representing the lights and then to use standard matrix simplification techniques to determine which buttons to press. It runs in polynomial time and does not require any backtracking.

Related

what are the parity bits in a 7,3 linear code

If I have a linear (7,4)-Hamming Code I know that the last 3 bits are the parity bits but I just have seen that there are multiple Codes like (7,3) for example (this should represent a matrix):
1 0 0 1 0 0 0
1 0 0 0 0 0 1
1 0 1 0 1 0 0 (Notice that it's not important if this is correct or not)
Are the first 3 bits the parity bits and the last four the information words? Or how can I understand this matrix?
Thank You very much for your help!

find the path with least number of jumps for a frog crossing pond using divide and conquer

A frog is trying to cross a pond but he can only jumps on stones and he can jump up to five units.
We have been given an array with ones and zeros (zeros for water and ones for stones) and the task to find the best possible approach to find the path with the least number of jump(using divide and conquer if possible).
Example for array -> [0 0 0 1 0 0 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0]
p.s: the frog starts one unit before the array and have to reach a unit after the array (it is possible that there is no path available with conditions mentioned)
I'm just starting to learn algorithms so it would be nice if you guys help me with the algorithm and code.
My idea for a divide and conquer approach would be the following:
program frog(array)
begin
divide array after the '1' left of the center of the array;
foreach (partialArray) do begin
if (length(partialArray) > 5) then frog(partialArray);
end foreach;
end.
Let's use your given array as an example: [0 0 0 1 0 0 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0]
1st division: [0 0 0 1 0 0 1 0 0 1|0 1 0 0 1 0 1 1 0 0 0 0]
2nd division: [0 0 0 1|0 0 1 0 0 1|0 1 0 0 1|0 1 1 0 0 0 0]
3rd division: [0 0 0 1|0 0 1|0 0 1|0 1 0 0 1|0 1 1|0 0 0 0]
In this case, the frog would jump to the first, the second, the third, the fifth, and the seventh 1 before reaching the goal (= 6 jumps).
Why does this algorithm work? Let's assume, it wouldn't output a shortest way of jumping for the frog. That would mean, we didn't find the partialArrays with the largest jumps for the frog. If one partialArray wouldn't resemble a largest jump, that would mean: There would be at least one 1 after the 1 inside the partialArray which the frog can still reach. This isn't possible, since length(partialArray) ≤ 5 holds. Thus, every 1 after the partialArray would be too far away (> 5).

Finding the largest submatrix contain only one zero and 1's in a matrix

I need to find a submatrix that only contain a 0,but others are 1.
Here's an example:
5*4 matrix
1 0 1 1 1
1 1 1 0 0
1 1 1 0 1
0 1 0 1 1
And the answer will be
3*3
1 0 1
1 1 1
1 1 1
I tried to use other similar questions' methods.But so far I can't figure out how to calculate it without using brutal solution.

Find cluster boundary in given matrix

Given matrix contains many clusters. Cluster is represented by 1's.
For Example:
0 1 1 1 0
1 1 1 0 0
0 0 0 1 1
1 1 0 0 1
In this example, there are 3 clusters (connected 1's horizontally or vertically).
Now suppose that matrix size is very big and it contains too many clusters.
Now my question is, I want to know the boundary of all the clusters.
For example given matrix:
0 0 1 1 0
0 1 1 1 1
0 0 1 1 0
0 0 0 1 0
Now output should be coordinates of bold locations:
0 0 1 1 0
0 1 1 1 1
0 0 1 1 0
0 0 0 1 0
Consider matrix is huge with many such clusters, suggest optimized way of finding boundaries of all clusters.
A 1 is on the boundary of some cluster only when it has an 0 as one of its immediate neighbors or when it's on the outline of the matrix, so the naive method would be outputting the coordinates of 1's on the outline, then going through all the other matrix cells and outputting the coordinates of 1's satisfying the said condition.
It doesn't look like there is a way to optimize this algorithm, though.
Suppose we have a huge matrix with only one random 1 inside it. There is no way to find it without going through each and every cell out there.
There is also no way to skip 1's of the same cluster by following the outline of it.
Consider this example:
0 0 0 1 1 1 1 1 1 1
0 0 0 1 0 0 0 0 0 1
0 1 1 1 1 1 0 1 0 1
0 0 0 0 0 1 0 0 0 1
0 0 0 0 0 1 1 1 1 1
One cluster can easily fit inside another one, so following the outline is not an option.

create multiple NxM matrices

I'm looking for a code which returns all combinations of NxM matrices consisting of only ones and zeros. Every row contains precisely one '1'. Every column consists of one or more '1'. If the rules for rows and columns are hard to program they may be left out since computational problem is not going to be an issue.
1 0 0
1 0 0
1 0 0
0 1 0
0 0 1
1 0 0
1 0 0
0 1 0
0 1 0
0 0 1
etc. etc. etc.
Hopefully someone can help me out.
Cheers, Raymond

Resources