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
题意:5 * 6 个灯组成一个矩阵,0代表此时熄灭,1代表此时亮着,每次开关一颗灯都会影响上下左右四个灯同时翻转,问要熄灭所有灯,需要对哪些灯进行一次开关操作(被动翻转的灯不算)
题解:高斯消元,设每个灯开关x次,因为一颗灯要么开关一次要么不开关,所以最后解出来的x要么是0,要么的1,则在消元过程中对x的系数对2取模不影响答案,最后的答案算出来可能存在负数,所以再对每个答案模2处理一下就行了。
//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 1e6 + 3;
const int maxn = 105;
const double PI = acos(-1.0);
const double E = 2.718281828459;
const double eps=1e-7;
struct Matrix {
void clear() { MT(a, 0); }
int equ, var;
int a[maxn][maxn], x[maxn];
Matrix () {}
Matrix (int x, int y, int p[maxn][maxn]) : equ(x), var(y) {
for(int i=0; i<equ; i++) for(int j=0; j<=var; j++) a[i][j] = p[i][j];
}
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a%b); }
int LCM(int a, int b) { return abs(a) * abs(b) / gcd(abs(a), abs(b)); }
int GetTriangle() {
int i = 0;
for(int col=0; i<equ && col<var; i++, col++) {
int max_row = abs(a[i][col]), row = i;
for(int k=i+1; k<equ; k++)
if(max_row <= abs(a[k][col])) max_row = abs(a[row=k][col]);
for(int j=col; j<=var; j++) swap(a[row][j], a[i][j]);
if(a[i][col] == 0) { i --; continue; }
for(int k=i+1; k<equ; k++) {
if(a[k][col] == 0) continue;
int lcm = LCM(a[i][col], a[k][col]), g = 0;
int pi = lcm / abs(a[i][col]), pk = lcm / abs(a[k][col]);
if(a[i][col] * a[k][col] < 0) pk = - pk;
for(int j=col; j<=var; j++){
a[k][j] = a[k][j]*pk - a[i][j]*pi;
if(a[k][j] == 0) continue;
g = !g ? abs(a[k][j]) : gcd(g, abs(a[k][j]));
}
if(g > 1)
for(int j=col; j<=var; j++) a[k][j] /= g;
for(int j=col; j<=var; j++) a[k][j] %= 2; // 关键
}
}
return i;
}
int Gauss(){
int num = GetTriangle();
for(int i=num; i<equ; i++) if(a[i][var] != 0) return -1;
if(num < var) return var - num;
for(int i=var-1; i>=0; i--) {
int y = a[i][var];
for(int j=var-1; j>i; j--) y -= x[j] * a[i][j];
if(y % a[i][i] != 0) return -2;
x[i] = y / a[i][i]; x[i] = (x[i] + 100) % 2; // 关键
}
return 0;
}
};
int main(){
Matrix M; M.equ = 30, M.var = 30;
int c[10][10];
int run[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int T, l = 0; scanf("%d", &T);
while( T --) {
M.clear();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
scanf("%d", &c[i][j]);
int id = i * 6 + j;
M.a[id][id] = 1;
for (int k = 0; k < 4; k++) {
int x = i + run[k][0];
int y = j + run[k][1];
if (x < 0 || x >= 5 || y < 0 || y >= 6) continue;
int t = x * 6 + y;
M.a[t][id] = 1;
}
}
}
for (int i = 0; i < 30; i++) M.a[i][30] = c[i / 6][i - i / 6 * 6];
int f = M.Gauss();
printf("PUZZLE #%d\n", ++l);
for(int i=0; i<30; i++) printf("%d%c", M.x[i], (i + 1) % 6 == 0 ? '\n' : ' ');
}
return 0;
}
本文介绍了解决POJ-1222 Lights Out游戏的算法,通过高斯消元法求解按钮操作,使所有灯熄灭。详细解析了如何设置方程组,利用矩阵运算解决此问题。
&spm=1001.2101.3001.5002&articleId=97519356&d=1&t=3&u=e45a13d9e2754acb81f535669ff94963)

被折叠的 条评论
为什么被折叠?



