123gjweq2's blog

By 123gjweq2, history, 18 months ago, In English

You are given a grid with $$$n$$$ rows and $$$m$$$ columns filled with zeros and ones. In one move, you can choose any cell in the grid and flip all of the bits in the adjacent cells. You do not flip the bit in the cell you chose.

For example, if you choose the cell $$$(3, 1)$$$ and then the cell $$$(2, 2)$$$.

0 0 0      0 0 0      0 1 0
0 0 0 ->   1 0 0 ->   0 0 1
0 0 0      0 1 0      0 0 0

You are given that you can make the grid into all zeros in some number of operations. Find the minimum number of operations to make the grid all zeros and output any minimum sequence of operations.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
18 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I think it can be solved row-by-row: from the second-highest row you apply the operation to make all of the highest row $$$0$$$ and then go to the third-highest to make the second-highest all 0, down to the final row. But I'm not sure if this is the minimum.

»
18 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

You would create a system of equations over GF(2) and solve. Look up lights out puzzle, I believe this is just a modified version.

  • »
    »
    18 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    There is not a single solution. For example, consider a possible solution G. If you flip every cell in any of the two diagonals you also get a solution. I believe this implies you cannot use methods like Gaussian elimination. Even if you somehow force some kind of solution like this you still have to prove it minimizes number of moves.

    • »
      »
      »
      18 months ago, hide # ^ |
       
      Vote: I like it +33 Vote: I do not like it

      Multiple solutions ≠ "can't use Gaussian elimination."
      Gaussian elimination expects multiple solutions — that's what the null space is for. Over GF(2), once you row reduce the system, you get a particular solution x₀, and then the general solution is:

      x = x₀ + c₁·v₁ + c₂·v₂ + ... + cₖ·vₖ
      

      where each vᵢ is a null-space basis vector and each cᵢ ∈ {0,1}.

      So yeah, flipping along a diagonal (or any weird pattern) is just adding some combination of those null vectors. It’s still covered by Gaussian elimination.
      If you want the minimal solution (e.g., with fewest 1s), brute force over all 2^k combinations of basis vectors — totally doable if k is small. This guarantees the minimal Hamming weight.

      Bottom line: multiple solutions don’t break Gauss-Jordan. They highlight its power.

    • »
      »
      »
      18 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      If there are multiple solutions it will still work, but it would not necessarily return the minimum. What the other guy said about brute forcing basis vectors should still be much faster than the naive solution in this case.

»
18 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I’d greedily perform the move which reduces the number of 1 the most at each step. I don’t have a proof for it but I’d definitely do this in a contest

  • »
    »
    18 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    After putting some more thought i realized this solution is stupid. Unfortunately I couldn’t come up with a better solution other than BFS: each grid is its own state and you have their distance to the empty grid. You run a bfs until you reach the final state. The time complexity of this is O((N+E)*K), where N is the number of states, E the number of transitions and K a factor for how heavy it is representing each state. As each cell you will either turn on or off you can use bitmasks, so you could use a vector of long longs (with size n*m/64)to represent each state. As the number of transitions of each state is n*m, let U = n*m, the final complexity is:

    (2^U * U) * U /64 = 2^U * U^2 / 64. You can solve this way for U up to something like 18.