Mini-game Lights out (off the lights) solution--XOR equations

Source: Internet
Author: User

Author:evensgnBlog link:http://www.cnblogs.com/joefan/Article link:http://www.cnblogs.com/joefan/p/4338003.htmlGame Introduction

Lights out (lights off) is a small game that is said to have been designed in the the 1990s, and the gameplay is very simple.

First, given a rectangular grid of n-row m-columns, there is a light on each grid.

Initially, some of the lights were on and some were closed.

Each time the player makes an operation, selects a lamp, clicks on it, and then changes the state of the lamp that is adjacent to it, that is, the open light turns off, and the light turned on.

The final aim is to turn off all the lights.

I did not find the original Lights out of the web version of the demo, but I found a blog park a bo friend DIY a similar game, blue jigsaw puzzles.

This version of the blue puzzle is essentially the same as Lights out, except that the initial state is fixed and that all lights are turned on.

Lights out of the rules of the game is so simple, but to the back of a few close, more squares, the situation is complex, the artificial solution for me is very difficult.

Therefore, we consider using the program to solve this game.

Solution Method

First, we'll turn this game process into a mathematical model.

Obviously, for a square, it affects its squares only itself and the 4 squares adjacent to it (the adjacent squares are less than 4 for the bounding box).

And it is easy to find that every square we either do not click, or click 1 times, because clicking on a square two times and above is meaningless. Two hits per click is equivalent to no clicks.

For grid I, we use 0 to indicate that we do not click on it, 1 means to click it, to remember as Si.

each lamp state only on or off, we use 0 and 1 to indicate the state of the box I, the initial state of the square I is recorded as Mi .

As can be seen, the final state of each lamp I only with Mi + Si + Sk1 + Sk2 + .... + SKP (K1 ... KP is the enumeration of all squares adjacent to i) is related to the parity.

Since it is only related to parity, we can represent it with an XOR operation.

In other words, for each lamp I, we can get an equation Mi xor Si xor Sk1 xor Sk2 xor = 0.

the 0 representation to the right of the equation Finally, the status of each lamp is closed.

This equation is actually equivalent to Si XOR Sk1 xor Sk2 xor ... xor SKP = Mi.

We get a tot of such an equation (tot is the number of lights, that is, tot = n * m), a total of tot unknown (that is, tot Si), is an XOR equation group.

Since there must be a solution to the initial state of the game, we are sure to find out a set of solutions to this XOR equation group.

So here's the question: How to solve an XOR or a group of equations?

Obviously, we want to use Gaussian elimination to find the solution of the different or the equations.

This process is similar to the use of Gaussian elimination to solve a common linear equation group (if you do not understand the Gaussian elimination element can look at the wikipedia-Gaussian elimination method), but each time in an equation to eliminate an unknown, not the equation by the previous coefficient and another equation subtraction, Instead, the coefficients of this equation are different from the coefficients of another equation, and the number to the right of the two equations is also different.

In this way, the solution of Lights out can be obtained.

The code is as follows because the code is so simple that no comments are added:

#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <  Algorithm> #include <cmath>using namespace std;const int MaxL = ten + 5, MAXN = + 5;const int dx[5] = {0, 0, 1, -1}, Dy[5] = {1,-1, 0, 0};int N, M, Tot;int A[MAXN][MAXN], MAP[MAXL][MAXL], ans[maxl][maxl];inline bool Inside (int x, in T y) {if (x < 0 | | x >= N) return false;if (Y < 0 | | y >= m) return False;return true; inline int Get_index (int x, int y) {return x * m + y + 1;} struct pos{int x, y;}; inline Pos get_pos (int Num) {pos ret;ret.x = (Num-1)/M;ret.y = ((Num% m-1) + m)% m;return ret;} void get_equation (int x, int y) {int Id, Id2, xx, yy;id = Get_index (x, y); for (int i = 1; I <= Tot; ++i) a[id][i] = 0;a [Id] [Tot + 1] = Map[x][y]; A[id][id] = 1;for (int k = 0; k < 4; ++k) {xx = x + dx[k]; yy = y + dy[k];if (! Inside (xx, yy)) Continue;id2 = Get_index (xx, yy); A[ID][ID2] = 1;}} inline void Swap (int p, int q) {int temp;for (int i = 1; I <= Tot +1; ++i) {Temp = A[p][i]; A[p][i] = A[q][i]; A[q][i] = Temp;}} void Gauss () {int tj;for (int i = 1; I <= Tot; ++i) {Tj = i;for (int j = i + 1; j <= Tot; ++j) {if (a[tj][i] = = 0 &amp ;& A[j][i] = = 1) {Tj = J;break;}} if (a[tj][i] = = 0) continue;if (TJ! = i) Swap (TJ, I); for (int j = i + 1; j <= Tot; ++j) {if (a[j][i] = = 0) continue;for (int k = i; k <= Tot + 1; ++k) A[J][K] ^= a[i][k];} Pos pi;for (int i = Tot; I >= 1; i.) {Pi = Get_pos (i); ANS[PI.X][PI.Y] = A[i][tot + 1];for (int j = i-1; J >= 1;--j) if (A[j][i]) A[j][tot + 1] ^= A[i][tot + 1];}} int main () {scanf ("%d%d", &n, &m); Tot = n * m;for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) scanf ("%1d", &map[i][j]); for (int i = 0; I &lt ; N ++i) for (int j = 0; j < m; ++j) Get_equation (i, j); Gauss ();p rintf ("solution:\n"), for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) printf ("%d", Ans[i][j]);p rintf ( "\ n");} return 0;}

  

Mini-game Lights out (off the lights) solution--XOR equations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.