Puzzle (1311) "Operation" Light up all the lights

Table of contents

1. Light up all the lights

2. Nature of sub-problems

3. Definition of terms

4. The structure of the solution space

5. The relationship between global status and global operations

6. Programming solution

POJ 3279 Fliptile

POJ 1222 EXTENDED LIGHTS OUT

Seven, fourth-order and fifth-order strategies

1. Fourth-order strategy

2. Five-level strategy

8. Equivalence class analysis of global operations

1. Question

2. Definition of terms

3. Programming enumeration and finding patterns

4. Theoretical analysis

5. Rules of coefficient matrix

(1) Enumerate the coefficient matrix and observe the rules

(2) Single term definition of coefficient matrix

(3) Row-by-row interpretation and column-by-column interpretation of the coefficient matrix

(4) Column-based construction method of coefficient matrix

(5) Prove Rule 4: The coefficient matrices are all symmetric matrices

(6) Prove Rule 5: The coefficient matrices are all centrally symmetric matrices

(7) Use Rule 6 to derive Rule 4 and Rule 5

6. Sorting out remaining issues and ideas

7,9 order coefficient matrix analysis

8. Programming to find the coefficient matrix

9. Gaussian elimination to solve the system of equations

(1) System of equations AX=0

(2) Equation AX=b

9. Rules of coefficient matrix

1. Find the coefficient matrix

2. Display viewing instructions

3. Morphological rules

4. Numerical rules - the number of 1

5. Expand the pattern of patterns - the number of independent 1's and clustered 1's


1. Light up all the lights

Click to open the yo puzzle in the game  or  resource sharing summary  . There are various puzzle modes. The flip mode is the mode to light up all the lights. This is the only light up all the lights mini-game I have found that allows you to freely set the dimensions.

   

rule:

The game board can be diverse. This article only discusses n*n boards.

Each grid has 4 neighbors (only 3 neighbors if it is on the border, and only 2 neighbors in the 4 corners),

Each grid has 2 states, light or black.

Every time you click on any grid, it and its four neighbors (ignoring if they do not exist, which will not be repeated below) will change state.

Now let you perform a series of clicks with no limit on the number of steps (you can click on the same grid repeatedly) to make all grids light up.

(I hope I've described it completely clearly, if not, please click on the link above, very fun game)

2. Nature of sub-problems

Anyone who has played with a Rubik's Cube knows that any ordinary n (n>3) order Rubik's Cube can be pieced together, transformed into a 3-level Rubik's Cube, and finally restored using the 3-level Rubik's Cube method.

Although this is certainly not the only method, it is the most effective method. Almost all people who play n-order Rubik's Cube use this method.

But does this problem have such good sub-problem properties?

Sadly, no. As long as you think about it for a moment, you will know that there really is no such thing.

The key to this difference is the difference in operational granularity. The operation granularity of the Rubik's Cube is arbitrary, it can be transferred to 1 level or 2 levels. . .

In this game, the operation is fixed (only the position is optional), and the impact of the operation on the entire situation is also fixed, and has nothing to do with n.

This place is very similar to the N Queens problem: place N queens on an N*N chessboard so that they cannot attack each other.

If you try to think of the 7-queen problem as a sub-problem of the 8-queen problem, then, after the 7 queens are placed, the 8th queen can only be placed in the remaining row and column of the remaining row. There is no room for choice, and it is likely that there will be no solution due to diagonal conflicts.

Therefore, it is not feasible to regard the 7-Queens problem as a sub-problem of the 8-Queens problem. The same goes for this game.

3. Definition of terms

1. Click operation: The operation mentioned in the game rules. Clicking on one grid is one click operation.

2. Point state: If a grid is lit (colored), it is called a good state.

If it is not lit (grey) then it is called a bad state or a state that needs to be changed.

3. Row neighbors: The two row neighbors of row i are row i-1 and row i+1. If i=1 or n, then it has only 1 row neighbor

4. Downward operation (performed on row i (i<n)): For each bad state grid in row i, perform a point operation on the neighbor in row i+1. In this way, the i-th row is restored.

5. Upward operation (performed on row i (i>1)): For each bad state grid in row i, perform a point operation on the neighbor in row i-1. In this way, the i-th row is restored.

6. Global operation: For each of the n*n grids, either a point operation is performed or not performed. Such a series of operations is called a global operation. A global operation can be understood as a subset of a set of size n*n, or as a 0-1 matrix.

7. Transition state: For the correct state of an n-level game, after performing downward row operations on the 1st, 2nd, 3rd...n-1 in sequence, all except the nth row will definitely be restored. . Then we call the state at this time the transit state.

8. Restorable state: A state that can be restored to a restorable state after several operations is called a restorable state, also called a correct state.

4. The structure of the solution space

Obviously the solution space is a complex graph, not a tree, but this is not the focus of our discussion.

For an n*n game, each grid has 2 states, so there are 2^(n*n) states globally.

Not every state among these states is necessarily a correct state. All correct states, according to the neighbors defined by a transition, can be connected into a simple undirected graph and are connected.

Each correct state can be transferred to the recovery state along a certain path, and correspondingly, the recovery state can also be transferred to this state along this path.

(The above are all nonsense, here comes the point)

Such two paths have opposite directions but completely overlap their edges. Their corresponding global operations are exactly the same!

That is to say, for a state that can be restored, if it is restored after a certain global operation, then performing this global operation on the restored state can obtain the original global operation.

PS: This is actually a group.

5. The relationship between global status and global operations

(1)

There are 2^(n*n) global states, some of which are recoverable and some that cannot be recovered.

There are also 2^(n*n) kinds of global operations. For a state that can be restored, there must be a global operation that can restore it.

For an irreversible state, there must be no global operation that can restore it.

For the restored state, any global operation will result in recoverable operations.

Therefore, there is a very natural mapping from global operations to global state:

The mapping result of each global operation is: the global state obtained by restoring the state through this global operation.

This mapping is not necessarily bijective, that is to say, some global operations may be equivalent, that is, two global operations correspond to the same global state.

(2)

The superposition of any two global operations results in one global operation.

Certain global operations are equivalent, such as:

01110  00000
10101  00000
11011  00000
10101  00000
01110  00000

These two operations are obviously equivalent.

Therefore, we can divide all global operations into m equivalence classes .

The amazing thing is that it is not difficult to prove that the number of elements in each equivalence class is the same , so m is a divisor of 2^(n*n).

Assuming m=2^k, then each equivalence class has 2^t elements, where t=n*nk

These equivalence classes, and all the correct states, can be mapped one by one.

Furthermore, it is not difficult to deduce that 0<=t<=n, because all matrices equivalent to the 0 matrix can be determined by the first row.

6. Programming solution

For a correct state, if the enumeration method is used to find the appropriate global operation, the efficiency is very low.

If the dimension is 6, then 2^36 global operations need to be enumerated. Although there will be a lot of pruning in the middle, the final efficiency is still very low.

Because I am playing this game on my mobile phone, I can preprocess it first, that is, turn it into a transit state.

At this time, enter the last row of data into the program, and the input is much simpler.

It is also very simple to find appropriate global operations at this time.

For example, if n=3, then the global operation must be in the following form:

Why can it only be in this form?

Because the transit state must be restored after this global operation, this table can be derived from top to bottom.

So we only need to enumerate a, b, c in the first line and find the global operation that meets the conditions (must exist).

for example:

For this transit state, input 1,1,1 into the program, and the program needs to solve such a system of equations:

b+c=1,a+b+c=1,a+b=1

The additions here are all XOR, or in the end they all have to be %2

The methods for obtaining b+c, a+b+c, and a+b here are the same as in the previous table.

To solve this equation with programming, you can use Gaussian elimination method to solve the XOR equation, or you can use brute force enumeration.

Solve a=0,b=1,c=0

In other words, first click on the grid in the first row and first column, and then in this state, her transit state is the recovery state.

In other words, after clicking on the grid in the first row and column, and performing downward row operations on the 1st, 2nd, 3rd...n-1 in sequence, you will definitely get the restored state.

Now, programming becomes very simple.

Code:

#include<iostream>
using namespace std;

int main()
{	
	int needlighten[10];
	cout << "输入维度" << endl;
	int n;//n阶点亮所有的灯
	cin >> n;
	cout << "点亮第1行,第2行。。。第" << n - 1;
	cout << "行\n最后一行中,从左到右输入状态,1表示黑,0表示亮" << endl;
	for (int nl = 0; nl<n; nl++) cin >> needlighten[nl];
	int biao[12][12];
	for (int number = 1; number<(1<<n); number++)
	{
		int num = number;
		for (int k = 0; k<n; k++)
		{
			biao[1][k + 1] = num % 2;
			num = num / 2;
		}
		for (int i = 0; i<n + 2; i++)for (int j = 0; j<n + 2; j++)
		{
			if (i == 0 || j == 0 || j == n + 1)biao[i][j] = 0;
			else if (i != 1)biao[i][j] = (biao[i - 2][j] + biao[i - 1][j - 1] + biao[i - 1][j] + biao[i - 1][j + 1]) % 2;
		}
		bool buer = 1;
		for (int u = 0; u<n; u++)if (biao[n + 1][u + 1] != needlighten[u])buer = 0;
		if (buer)
		{
			cout << "点第一行的第";
			for (int v = 0; v < n; v++)if (biao[1][v + 1])cout << v + 1 << "  ";
			cout << "个,再逐行完成即可";
		}
		if (buer)break;
	}
	system("pause > nul");
	return 0;
}

With this code, the game is very simple to play.

However, this does not mean that this game has no research value. On the contrary, there are still very interesting things that cannot be displayed in the code.

POJ 3279 Fliptile

 topic:

Description

Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.

As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.

Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".

Input

Line 1: Two space-separated integers: M and N 
Lines 2.. M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white

Output

Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

Sample Input

4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

Sample Output

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

The meaning of this question is to input m, n, input a 0-1 matrix of m*n, and require the output of an m*n 0-1 matrix.

Except that the size is not square, everything else is the same as my description, input global state (0-1 matrix), output global operation (0-1 matrix)

 The meaning of the question is, if there are multiple feasible solutions, output the solution with the smallest lexicographic order.

However, there is a problem with the dictionary order in the standard process. For details: click to open the link

Because the previous one is for my own game playing, and this one is for ACM programming, so although it is the same problem, the codes are slightly different.

Here, I use state compression , which uses 1 integer to represent 1 row.

However, the code written in this way is indeed difficult to understand.

f(k) represents the row increment corresponding to a row operation represented by k, that is, if the row operation represented by k is performed on a certain row, what impact will this row have?

For example, if k represents abcde, then the corresponding influence is a+b a+b+c b+c+d c+d+e d+e

This influence can be seen as the superposition of three influences: (1) abcde (2) bcde 0 (3) 0 abcd

After the state is compressed, it is the calculation method in f.

Code:

#include<iostream>
using namespace std;

int m, n, a;
int l[17];
int temp[17];

int f(int k)
{
	int t = (1 << n) - 1;
	return (k * 2 ^ k^k / 2)&t;
}

bool ok(int k)
{
	for (int i = 0; i < 17; i++)temp[i] = l[i];
	temp[0] = k;
	for (int i = 1; i < m; i++)
	{
		temp[i] = (temp[i] ^ f(temp[i - 1]));
		temp[i + 1] = (temp[i + 1] ^ temp[i - 1]);
	}
	return temp[m]==f(temp[m-1]);
}

void out(int k)
{
	for (int i = 0; i < 17; i++)temp[i] = l[i];
	temp[0] = k;
	for (int i = 1; i <= m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << (temp[i - 1] >> j) % 2;
			if (j < n - 1)cout << " ";
		}
		cout << endl;
		temp[i] = (temp[i] ^ f(temp[i - 1]));
		temp[i + 1] = (temp[i + 1] ^ temp[i - 1]);
	}
}


int main()
{
	cin >> m >> n;
	for (int i = 0; i < 17; i++)l[i] = 0;
	for (int i = 1; i <= m; i++)for (int j = 0; j < n; j++)
	{
		cin >> a;
		l[i] += (a << j);
	}
	for (int k = 0; k < (1 << n); k++)if (ok(k))
	{
		out(k);
		return 0;
	}
	cout << "IMPOSSIBLE" << endl;
	return 0;
}

AC completed

POJ 1222 EXTENDED LIGHTS OUT

Description

In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual puzzle has 5 rows of 5 buttons each). Each button has a light. When a button is pressed, that button and each of its (up to four) neighbors above, below, right and left, has the state of its light reversed. (If on, the light is turned off; if off, the light is turned on.) Buttons in the corners change the state of 3 buttons; buttons on an edge change the state of 4 buttons and other buttons change the state of 5. For example, if the buttons marked X on the left below were to be pressed,the display would change to the image on the right.


The aim of the game is, starting from any initial set of lights on in the display, to press buttons to get the display to a state where all lights are off. When adjacent buttons are pressed, the action of one button can undo the effect of another. For instance, in the display below, pressing buttons marked X in the left display results in the right display.Note that the buttons in row 2 column 3 and row 2 column 5 both change the state of the button in row 2 column 4,so that, in the end, its state is unchanged.


Note:
1. It does not matter what order the buttons are pressed.
2. If a button is pressed a second time, it exactly cancels the effect of the first press, so no button ever need be pressed more than once.
3. As illustrated in the second diagram, all the lights in the first row may be turned off, by pressing the corresponding buttons in the second row. By repeating this process in each row, all the lights in the first
four rows may be turned out. Similarly, by pressing buttons in columns 2, 3 ?, all lights in the first 5 columns may be turned off.
Write a program to solve the puzzle.

Input

The first line of the input is a positive integer n which is the number of puzzles that follow. Each puzzle will be five lines, each of which has six 0 or 1 separated by one or more spaces. A 0 indicates that the light is off, while a 1 indicates that the light is on initially.

Output

For each puzzle, the output consists of a line with the string: "PUZZLE #m", where m is the index of the puzzle in the input file. Following that line, is a puzzle-like display (in the same format as the input) . In this case, 1's indicate buttons that must be pressed to solve the puzzle, while 0 indicate buttons, which are not pressed. There should be exactly one space between each 0 or 1 in the output puzzle-like display.

Sample Input

2
0 1 1 0 1 0
1 0 0 1 1 1
0 0 1 0 0 1
1 0 0 1 0 1
0 1 1 1 0 0
0 0 1 0 1 0
1 0 1 0 1 1
0 0 1 0 1 1
1 0 1 1 0 0
0 1 0 1 0 0

Sample Output

PUZZLE #1
1 0 1 0 0 1
1 1 0 1 0 1
0 0 1 0 1 1
1 0 0 1 0 0
0 1 0 0 0 0
PUZZLE #2
1 0 0 1 1 1
1 1 0 0 0 0
0 0 0 1 0 0
1 1 0 1 0 1
1 0 1 1 0 1

Same as the previous question.

#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<algorithm>
using namespace std;

int m, n, a;
int l[17];
int temp[17];

int f(int k)
{
	int t = (1 << n) - 1;
	return (k * 2 ^ k ^ k / 2) & t;
}

bool ok(int k)
{
	for (int i = 0; i < 17; i++)temp[i] = l[i];
	temp[0] = k;
	for (int i = 1; i < m; i++)
	{
		temp[i] = (temp[i] ^ f(temp[i - 1]));
		temp[i + 1] = (temp[i + 1] ^ temp[i - 1]);
	}
	return temp[m] == f(temp[m - 1]);
}

void out(int k)
{
	for (int i = 0; i < 17; i++)temp[i] = l[i];
	temp[0] = k;
	for (int i = 1; i <= m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << (temp[i - 1] >> j) % 2;
			if (j < n - 1)cout << " ";
		}
		cout << endl;
		temp[i] = (temp[i] ^ f(temp[i - 1]));
		temp[i + 1] = (temp[i + 1] ^ temp[i - 1]);
	}
}


int main()
{
	int t;
	cin >> t;
	for (int id = 1; id <= t; id++) {
		m = 5, n = 6;
		for (int i = 0; i <= 17; i++)l[i] = 0;
		for (int i = 1; i <= m; i++)for (int j = 0; j < n; j++)
		{
			cin >> a;
			l[i] += (a << j);
		}
		cout << "PUZZLE #" << id << endl;
		for (int k = 0; k < (1 << n); k++)if (ok(k))
		{
			out(k);
			break;
		}
	}
	return 0;
}

AC completed

Seven, fourth-order and fifth-order strategies

1. Fourth-order strategy

This picture looks somewhat regular.

For a transit state, if you want to formulate an equation, you need to find 4 expressions based on this diagram.

(1) a+c+b+c+d+a+b+d, the result is 0

(2) d+b+c+d+a+b+d+a+c+d, the result is 0

(3) a+a+b+d+a+c+d+a+b+c, the result is 0

(4) b+d+a+c+d+a+b+c, the result is 0

I was surprised to find that it was actually 4 zeros!

In other words, unless four zeros are entered, the equation has no solution at all.

Furthermore, for level 4 games, any transition state is a recovery state!

That is: any correct state can be restored by three downward operations.

Illustration:

start:

1. Perform downward row operations on row 1, that is, perform point operations on row 2 and column 1. The operation is as follows:

2. Perform downward operations on row 2, that is, perform click operations on row 3, column 2, and column 3. The operation is as follows:

3. Perform downward row operations on line 3. It will be restored at this time, as follows:

2. Five-level strategy

According to the n-color fifth-order formula, when n=2, the equation becomes

8. Equivalence class analysis of global operations

1. Question

How many global operations are equivalent to zero matrices?

That is, how many n-order 0-1 matrices are there such that the elements at the top, bottom, left, and right positions of each element (if they exist) plus the element itself are even numbers?

Each equivalence class has 2^t elements, 0<=t<=n, now let’s find t

2. Definition of terms

(1) Standard recursive formula

x[i][j] = (x[i - 2][j] + x[i - 1][j - 1] + x[i - 1][j] + x[i - 1][j + 1]) % 2

(2) The matrix obtained by recursion

For any given first row (elements 0,1), a matrix of n rows and n columns uniquely determined by standard recurrence.

(3) Expand the matrix obtained by recursion

For any given first row (elements 0,1), a matrix of n+1 rows and n columns uniquely determined by standard recurrence.

(4) Recursive expansion row

Expand the last row of the matrix resulting from the recursion.

(5) Equivalence matrix

If the recursive expansion rows of two matrices x and y obtained by recursion are the same, that is, the corresponding global operations are equivalent, then x and y are equivalent matrices.

(6) 0 equivalent matrix

A matrix that is equivalent to the 0 matrix

Note 1: In the standard recursion formula, items out of the array range are ignored directly.

Note 2: Any first row, the matrix obtained by recursion, and the matrix obtained by extended recursion are all in one-to-one correspondence.

Note 3: "The recursive expansion rows are all 0" is equivalent to "The matrix obtained by the recursion is an equivalent matrix of 0"

Note 4: Finding the number of 0-equivalent matrices is actually a condition that requires the recursive expansion rows to be all 0.

3. Programming enumeration and finding patterns

(1) Ideas

Enumerate the first row and determine whether the recursive expansion row is all 0

(2) Code

#include<iostream>
using namespace std;

int main()
{
	int n, ans, x[30][30], num, k, i, j, flag;
	cout << "输入维度" << endl;
	while (cin >> n)
	{
		ans = 1;
		for (int number = 1; number < (1 << n); number++)
		{
			num = number, flag = 1;
			for (k = 0; k < n; k++)x[1][k + 1] = num % 2, num /= 2;
			for (i = 0; i < n + 2; i++)for (j = 0; j < n + 2; j++)
			{
				if (i == 0 || j == 0 || j == n + 1)x[i][j] = 0;
				else if (i > 1)x[i][j] = (x[i - 2][j] + x[i - 1][j - 1] + x[i - 1][j] + x[i - 1][j + 1]) % 2;
			}			
			for (i = 0; i < n; i++)if (x[n + 1][i + 1] != 0)flag = 0;
			if (flag)ans++;
		}
		cout << ans << " ";
	}
	return 0;
}

(3) Enter n
1 2 3 …… 27 28

(4) Output the number of 0 equivalent matrices
    (1-10) 1 1 1 16 4 1 1 1 256 1
    (11-20) 64 1 1 16 1 256 4 1 65536 1
    (21-28) 1 1 16384 16 1 1 1 1

(5) Analyze rules

n ranges from 1 to 28, and the values ​​of t are

(1-10)0 0 0 4 2 0 0 0 8 0

(11-20)6 0 0 4 0 8 2 0 16 0

(21-28)0 0 14 4 0 0 0 0

You can see the following rules:

Rule 1: All t’s are even numbers

Rule 2: t=n only when n=4, in other cases, t<n

Rule 3: Most of t is 0, a small part is a power of 2, and a very few is not a power of 2.

4. Theoretical analysis

(1) Ideas

List the general formula of the matrix obtained by recursion, and determine the conditions for becoming a 0-equivalent matrix.

Use n variables to represent the n numbers in the first row, find the last row of the matrix obtained by the extended recursion according to the recursion formula, and determine the conditions under which each number is 0.

(2) Example

(2.1)n=3

Matrix obtained by recursion: (all %2 is omitted, the same below)

Recursive expansion behavior b+c, a+b+c, a+b

The condition for all 0s is a=b=c=0. There is only one case, so 2^t=1, t=0

(2.2)n=4

The resulting matrix of recursion:

The recursive expansion behavior is 0,0,0,0, so t=4

(2.3)n=5

The matrix obtained by expanding the recursion: (all plus signs are omitted, the same below)

Recursive expansion behavior bce, abc, abde, cde, acd

The condition for all 0s is bce=abc=abde=cde=acd=0, which simplifies to a=e=b+c, ​​b=d

So 2^t=4, t=2

(3) Unified representation - coefficient matrix

Taking (2.3) as an example, the formula bce=abc=abde=cde=acd=0 can be expressed as a system of equations AX=0, where A is the coefficient matrix of the system of equations, and X is the transpose of the vector (abcde), that is, vertical vector.

\begin{pmatrix} 0 &1 &1 &0 &1\\ 1 &1 &1 &0 &0 \\ 1 &1 &0 & 1 &1\\ 0 & 0 &1 &1 &1\\ 1 &0 &1 &1 &0 \end{pmatrix}\begin{pmatrix} a \\b \\c \\d \\e \end{pmatrix}=\begin{pmatrix} 0 \\0 \\0 \\0 \\0 \end{pmatrix}

The number of X vectors that satisfy the condition is 2^(nR(A)), where R(A) is the rank of the A matrix

By the same token we can conclude:

For any n and any matrix obtained by expansion and recursion, each number in the recursion expansion row can be expressed as the sum of several numbers (at least 0, at most n) in the first row of the matrix obtained by expansion and recursion. , so the recursive expansion behavior 0 is equivalent to the corresponding system of equations AX=0

Therefore, we get that the number of 0 equivalent matrices is 2^(nR(A)), that is, t=nR(A)

5. Rules of coefficient matrix

(1) Enumerate the coefficient matrix and observe the rules

When n=2,3,4,5, the coefficient matrices are respectively

\begin{pmatrix} 1 &0 \\ 0 & 1 \end{pmatrix} \begin{pmatrix} 0 &1 &1 \\ 1 &1 &1 \\ 1 & 1 &0 \end{pmatrix} \begin{pmatrix} 0 &0 &0 &0 \\ 0 &0 &0 & 0 \\ 0 & 0 &0 &0 \\ 0 &0 &0 &0 \end{pmatrix} \begin{pmatrix} 0 &1 &1 &0 &1\\ 1 &1 &1 &0 &0 \\ 1 &1 &0 & 1 &1\\ 0 & 0 &1 &1 &1\\ 1 &0 &1 &1 &0 \end{pmatrix}

Rule 4: Coefficient matrices are all symmetric matrices

Rule 5: The coefficient matrices are all centrally symmetric matrices

Rule 6: The coefficient matrices all satisfy the recursion formula x[i][j] = (x[i - 2][j] + x[i - 1][j - 1] + x[i - 1][j + 1]) % 2, that is, the 4 numbers around each number add up to an even number

To prove Rule 5, we must first look at what each number in the coefficient matrix represents.

(2) Single term definition of coefficient matrix

For an n-order coefficient matrix A, use Aij to represent the i-th row and j-th column of A, and the value range is 0 or 1.

Aij=1 means that there is the j-th component of X in the i-th equation of the equation system AX=0, that is, there is the j-th variable in the expression of the i-th number in the recursive expansion row

Aij=0 means that there is no j-th variable in the expression of the i-th number in the recursive expansion row

(3) Row-by-row interpretation and column-by-column interpretation of the coefficient matrix

for example,

The first row of the fifth-order coefficient matrix is ​​0 1 1 0 1, which indicates that there is no ad in the expression of the first number in the recursive expansion row, but there is bce, that is, its value is bce

The first column of the fifth-order coefficient matrix is ​​0 1 1 0 1, indicating that there is no a in the expression of the 1st and 4th numbers in the recursive expansion row, and there is no a in the expression of the 2nd, 3rd and 5th numbers.

Recursive expansion behavior bce, abc, abde, cde, acd

(4) Column-based construction method of coefficient matrix

Here we first need to introduce a concept:

(4.1) Univariate matrix

If the first row of a matrix obtained by extended recursion has only one 1 and the other n-1 are 0, then the matrix obtained by extended recursion is called a single-variable matrix.

If the i-th number in the first row of a single-variable matrix is ​​1 and the others are 0, then we call it a single-variable matrix of the i-th variable.

For example, when n=5, the univariate matrix of the second variable is (empty cells represent 0, the same below)

(4.2) The relationship between single variable matrix and coefficient matrix

It is not difficult to find that the recursive expansion row of the single-variable matrix of the i-th variable, after being transposed into columns, is the same as the i-th column of the coefficient matrix.

For example, when n=5 above, the univariate matrix of the second variable shows that the second column of the fifth-order coefficient matrix is ​​1 1 1 0 0

PS: Before proving that the coefficient matrices are all symmetric matrices, don't make a mistake here. It is not the i-th row of the coefficient matrix, but the i-th column of the coefficient matrix.

(4.3) Column-wise construction method of coefficient matrix

Enumerate n single-variable matrices and obtain each column of the coefficient matrix in turn to obtain the coefficient matrix.

(5) Prove Rule 4: The coefficient matrices are all symmetric matrices

The entire coefficient matrix can be calculated from bottom to top using recursive expansion rows, and the calculation method is the same as using the first row to go down, so the coefficient matrix is ​​a symmetric matrix (symmetric about the main diagonal).

(6) Prove Rule 5: The coefficient matrices are all centrally symmetric matrices

The single-variable matrix of the i-th variable and the single-variable matrix of the n+1-i-th variable are symmetrical to each other.

So the i-th column and the n+1-i-th column are centrally symmetrical, so the entire matrix is ​​centrally symmetrical.

(7) Use Rule 6 to derive Rule 4 and Rule 5

First, the main diagonal itself is symmetrical about the main diagonal.

Secondly, it can be deduced from the fact that the sum of the elements (if they exist) at the upper, lower, left, and right positions of the first n-1 elements of the main diagonal is an even number, and it can be deduced that the two diagonals adjacent to the main diagonal The n-1 numbers of the line and the n-1 numbers are equal in turn, that is, the two diagonal lines are symmetrical.

Then, in the same way, prove that the two diagonal lines next to it are symmetrical

......

Finally, prove that the lower left corner and the upper right corner are symmetrical.

Therefore, the entire matrix is ​​symmetric about the main diagonal, and similarly, the entire matrix is ​​symmetric about the secondary diagonal.

So the entire matrix is ​​also a centrally symmetric matrix.

PS: Although Rule 6 can deduce Four and Five, I have not yet figured out how to prove Rule 6.

6. Sorting out remaining issues and ideas

n ranges from 1 to 28, and the values ​​of t are
(1-10) 0 0 0 4 2 0 0 0 8 0
(11-20) 6 0 0 4 0 8 2 0 16 0
(21-28) 0 0 14 4 0 0 0 0

There are currently 4 remaining issues:

Rule 1, Rule 2, Rule 3, Rule 6

Now, there is no clue to directly analyze why t is an even number, and the rule is probably related to the power of 2, so in addition to the cases of n=2, 3, 4, and 5, I decided to look at the case of n=9.

7,9 order coefficient matrix analysis

The calculation amount of the 9th-order coefficient matrix is ​​relatively large. Here we use the column-wise construction method of the coefficient matrix mentioned above to construct it.

n=9, the univariate matrix of the 1st, 2nd, 3rd, 4th, and 5th variables is in sequence

This will get the first 5 columns of the coefficient matrix

Because the coefficient matrix is ​​a centrally symmetric matrix, only these 5 columns are needed.

The resulting coefficient matrix:

So t=8, the corresponding system of equations is a+c+e+g+i=0


8. Programming to find the coefficient matrix

According to the above method, the coefficient matrix can be calculated in O(n^3) time, and the time complexity is greatly reduced.

Code:

#include<iostream>
using namespace std;

int n,x[303][303];

int main()
{
	for (n = 1; n < 300;n++)
	{
		cout << n << endl;
		for (int i = 1; i <= n;i++)
		{
			for (int k = 1; k <= n; k++)x[1][k] = (k==i);
			for (int i = 0; i < n + 2; i++)for (int j = 0; j < n + 2; j++)
			{
				if (i == 0 || j == 0 || j == n + 1)x[i][j] = 0;
				else if (i > 1)x[i][j] = (x[i - 2][j] + x[i - 1][j - 1] + x[i - 1][j] + x[i - 1][j + 1]) % 2;
			}
			for (int k = 1; k <= n; k++)cout<<x[n + 1][k];
			cout << "  ";
			for (int k = 1; k <= n; k++)cout << (x[n+1][k] ? '1' : ' ');//输出系数矩阵的转置矩阵
			cout << endl;
		}
	}
	return 0;
}

illustrate:

For each n, I output 2 matrices. The one on the left is the transposed matrix of the coefficient matrix (because I did not prove that the coefficient matrix is ​​a symmetric matrix, so it is better to make it clear), and the one on the right only outputs 1, 0 Replace with spaces.

Output:

The one on the left may be a bit confusing, but the matrix on the right is very obvious and very regular.

There is too much to write about this rule, and the pictures are too long, so I wrote another blog.

See: "Lighting All the Lamps" Advanced Analysis - The Law of Coefficient Matrix

"Light All the Lamps" Advanced Analysis - The Law of Coefficient Matrix_nameofcsdn's Blog-CSDN Blog

There are some vague rules, but no unified standard rules have been found, nor are there any products of reasoning.

9. Gaussian elimination to solve the system of equations

(1) System of equations AX=0

The system of equations here is actually a system of XOR equations. Gaussian elimination can be used to quickly calculate the rank of A, that is, the value of k.

The time complexity of Gaussian elimination is O(n^3), and the value of k can be quickly found for different n.

Code:

#include<iostream>
using namespace std;

int n, x[303][303], y[303][303];//y存系数矩阵
bool flag[303];

int main()
{
	for (n = 1; n <= 300; n++)
	{
		//cout << n << endl;
		for (int i = 1; i <= n; i++)
		{
			for (int k = 1; k <= n; k++)x[1][k] = (k == i);
			for (int i = 0; i < n + 2; i++)for (int j = 0; j < n + 2; j++)
			{
				if (i == 0 || j == 0 || j == n + 1)x[i][j] = 0;
				else if (i > 1)x[i][j] = (x[i - 2][j] + x[i - 1][j - 1] + x[i - 1][j] + x[i - 1][j + 1]) % 2;
			}
			for (int k = 1; k <= n; k++)y[k][i] = x[n + 1][k];
		}
		int ans = 0;//y矩阵的秩
		for (int i = 1; i <= n; i++)flag[i] = true;
		for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)
		{
			if (flag[j] && y[j][i])
			{
				for (int k = j + 1; k <= n; k++)if (flag[k] && y[k][i])
					for (int xi = 1; xi <= n; xi++)y[k][xi] ^= y[j][xi];
				flag[j] = false, ans++;
				break;
			}
		}
		cout << n - ans << " ";
		if (n % 10 == 0)cout << endl;
		if (n % 50 == 0)cout << endl;
	}
	return 0;
}

result:

n ranges from 1 to 300, and nk are:

0 0 0 4 2 0 0 0 8 0
6 0 0 4 0 8 2 0 16 0
0 0 14 4 0 0 0 0 10 20
0 20 16 4 6 0 0 0 32 0
2 0 0 4 0 0 30 0 8 8

0 0 2 4 0 0 0 0 22 0
40 24 0 28 42 0 32 0 8 0
14 0 0 4 0 0 2 0 64 0
0 0 6 12 0 0 0 0 10 0
0 20 0 4 62 0 0 20 16 0

18 0 0 4 0 0 6 0 8 0
0 0 2 4 0 0 0 8 46 0
0 0 80 4 50 56 0 56 56 0
86 0 0 4 64 0 2 0 16 0
0 0 30 4 0 0 0 0 10 0

0 8 0 24 6 0 0 0 128 0
2 0 0 24 0 0 14 0 24 36
0 0 2 4 0 0 0 0 22 0
0 0 0 4 42 8 0 24 8 0
126 0 0 28 0 0 42 0 32 0

0 0 38 24 0 0 0 0 10 0
0 0 0 4 14 20 0 0 16 8
2 0 0 4 0 0 6 0 8 20
0 0 2 4 0 0 16 0 94 0
0 0 0 4 2 0 160 0 8 0

102 0 112 140 0 144 114 0 112 0
0 0 174 4 0 0 0 0 10 0
128 0 0 4 6 0 0 20 32 0
2 0 0 4 0 0 62 8 8 0
0 0 2 4 0 20 0 0 22 0

It can be seen that all are even numbers, and most of them are 0.

(2) Equation AX=b

This is actually to change the above code from using the enumeration method to solve the system of equations to using the Gaussian elimination method, and what you get is the solution of the game itself.

As for the code, just change the above code.

9. Rules of coefficient matrix

1. Find the coefficient matrix

#include<iostream>
using namespace std;

int n,x[303][303];

int main()
{
	freopen("D:\\1.txt", "w", stdout);
	for (n = 1; n < 300;n++)
	{
		cout << n << endl;
		for (int i = 1; i <= n;i++)
		{
			for (int k = 1; k <= n; k++)x[1][k] = (k==i);
			for (int i = 0; i < n + 2; i++)for (int j = 0; j < n + 2; j++)
			{
				if (i == 0 || j == 0 || j == n + 1)x[i][j] = 0;
				else if (i > 1)x[i][j] = (x[i - 2][j] + x[i - 1][j - 1] + x[i - 1][j] + x[i - 1][j + 1]) % 2;
			}
			//for (int k = 1; k <= n; k++)cout << x[n + 1][k];
			//cout << "  ";
			for (int k = 1; k <= n; k++)cout << (x[n+1][k] ? '1' : ' ');//输出系数矩阵的转置矩阵
			cout << endl;
		}
	}
	return 0;
}

2. Display viewing instructions

(1) Use redirection to text, which can be saved in txt for easy viewing, such as search and viewing.

(2) The screen ratio on the console is better and looks more comfortable, but the lengths of 1 and spaces in txt are different, and it looks a little deformed.

For example, when n=27, the comparison between the console and txt is as follows:

   

Look at the bottom middle, the console is real, with regular empty triangles, but you can't see it in the txt.

(3) Using the old version of the console for display, the characters are smaller and more lines can be displayed on one screen.

(4) When n is large, line breaks will occur in the console and txt, which is not conducive to viewing. You can copy it to word and reduce the font for viewing.

3. Morphological rules

(1) When n is an even number, all 1s and 1s are scattered and not adjacent. When n is an odd number, most 1s will be adjacent to a certain 1, and there will be very few 1s distributed individually.

(2), when n+1 is the power of 2, the n-order coefficient matrix is ​​very regular

         

This also confirms my conjecture in the article "Advanced Analysis of "Light All the Lamps" - Equivalent Global Operations". The law of the coefficient matrix is ​​related to the power of 2.

After careful thinking, I found that these matrices can be expressed by unified rules:

When n+1 is a power of 2, the coefficient matrix is ​​obtained by tiling this small matrix:

Tiling method:

Starting from the upper right corner, tile it to the left and down until it is full, then keep the main diagonal line and the part above the diagonal line, and get the part below the diagonal line through symmetry.

PS: Although I have not proved that the coefficient matrices are all symmetric matrices, this article only explains the rules without reasoning, so it is okay to use symmetry to explain.

In fact, for many n, it is similar to using some kind of tiling, but the rules may not be so obvious.

(3), the shape of X is very common

One is a hollow big X, and the other is a solid small X.

   

In the case of n=19, the tiling pattern is quite obvious. Taking the X I drew as the basic unit, and following the tiling method I described above for n=31,63, this matrix can be obtained.

   

The situation of n=37 is relatively hidden. If you look carefully, there is a big X.

4. Numerical rules - the number of 1

Code:

#include<iostream>
using namespace std;

int n, x[303][303], s;

int main()
{
	//freopen("D:\\1.txt", "w", stdout);
	while (cin>>n)
	{
		//cout << n << endl;
		s = 0;
		for (int i = 1; i <= n;i++)
		{
			for (int k = 1; k <= n; k++)x[1][k] = (k==i);
			for (int i = 0; i < n + 2; i++)for (int j = 0; j < n + 2; j++)
			{
				if (i == 0 || j == 0 || j == n + 1)x[i][j] = 0;
				else if (i > 1)x[i][j] = (x[i - 2][j] + x[i - 1][j - 1] + x[i - 1][j] + x[i - 1][j + 1]) % 2;
			}
			//for (int k = 1; k <= n; k++)cout << x[n + 1][k];
			//cout << "  ";
			//for (int k = 1; k <= n; k++)cout << (x[n+1][k] ? '1' : ' ');//输出系数矩阵的转置矩阵
			for (int k = 1; k <= n; k++)if (x[n + 1][k])s++;
		}
		cout << s << " ";
	}
	return 0;
}

Output:

n from 1 to 30:

1 2 7 0 16 12 27 20 25 28
68 36 81 56 115 66 136 60 175 80
217 126 284 126 297 180 303 204 400 234

There is no pattern in visual inspection, or the pattern is too complicated.

5. Expand the pattern of patterns - the number of independent 1's and clustered 1's

Here we only look at rows, that is, if the left and right sides of a 1 are both 0 or boundary, it is an independent 1. If there is a 1 on the left or right, it is a gathered 1.

Based on the law that the coefficient matrix is ​​a symmetric matrix, if there are no neighbors on the left, it means there are no neighbors above and below, that is, independent 1

Code:

#include<iostream>
using namespace std;

int n, x[303][303], s1,s2;

int main()
{
	//freopen("D:\\1.txt", "w", stdout);
	for (n = 1; n < 300;n++)
	{
		//cout << n << endl;
		s1 = s2 = 0;
		for (int i = 1; i <= n;i++)
		{
			for (int k = 1; k <= n; k++)x[1][k] = (k==i);
			for (int i = 0; i < n + 2; i++)for (int j = 0; j < n + 2; j++)
			{
				if (i == 0 || j == 0 || j == n + 1)x[i][j] = 0;
				else if (i > 1)x[i][j] = (x[i - 2][j] + x[i - 1][j - 1] + x[i - 1][j] + x[i - 1][j + 1]) % 2;
			}
			//for (int k = 1; k <= n; k++)cout << x[n + 1][k];
			//cout << "  ";
			//for (int k = 1; k <= n; k++)cout << (x[n+1][k] ? '1' : ' ');//输出系数矩阵的转置矩阵
			for (int k = 1; k <= n; k++)if (x[n + 1][k])
			{
				if (x[n + 1][k - 1] == 0 && x[n + 1][k + 1] == 0)s1++;
				else s2++;
			}
		}
		//cout << s2 ;
		cout << s1 << " " << s2 << endl;
	}
	return 0;
}

result:

1 0
2 0
0 7
0 0
2 14
12 0
3 24
20 0
25 0
28 0
10 58
36 0
3 78
56 0
0 115
66 0
12 124
60 0
0 175
80 0
21 196
126 0
18 266
126 0

,,,,,,

When n is an even number from 2 to 300, s2 is all 0, that is, there are no bunched 0s.

Guess you like

Origin blog.csdn.net/nameofcsdn/article/details/132224953