Description
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
Output
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
//
#include <iostream>
#include <string>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 105;
int cases = 1;
int equ, var;
int a[maxn][maxn];
int x[maxn]; // 解集.
int mat[5][6];
int d[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
// 有equ个方程,var个变元。增广阵行数为equ, 分别为1到equ,列数为var + 1,分别为1到var+1。
int Gauss_XOR(int a[maxn][maxn], int x[maxn], int var, int equ)
{
int row, col;
for (row = col = 1; row <= equ && col <= var; ++row, ++col)
{
if (!a[row][col])
{
for (int i = equ; i > row; --i)
{
if (a[i][col])
{
for (int j = row; j <= var + 1; ++j)
{
swap(a[i][j], a[row][j]);
}
break;
}
}
}
if (!a[row][col])
{
--row;
continue;
}
for (int i = row + 1; i <= equ; ++i)
{
if (a[i][col])
{
for (int j = var + 1; j >= col; --j)
{
a[i][j] ^= a[row][j];
}
}
}
}
for (int i = row; i <= equ; ++i)
{
if (a[i][var + 1]) return -1;
}
if (row <= var)
{
return var - row + 1;
}
for (int i = var; i >= 1; --i)
{
x[i] = a[i][var + 1];
for (int j = i + 1; j <= var; ++j)
{
x[i] ^= a[i][j] && x[j];
}
}
return 0;
}
void print()
{
printf("PUZZLE #%d\n", cases++);
int tt = 1;
for (int i = 1; i <= var; i++)
{
if (tt % 6 == 0) printf("%d\n", x[i]);
else
printf("%d ", x[i]);
tt++;
}
}
int main()
{
//freopen("1.txt", "w", stdout);
int i, j, t, r, l, xx, yy;
scanf("%d", &t);
while (t--)
{
//初始化
memset(a, 0, sizeof(a));
memset(x, 0, sizeof(x));
for (i = 1; i <=5; i++)
for (j = 1; j <=6; j++)
scanf("%d", &mat[i][j]);
equ = var = 30;
r = l = 1;//增广矩阵的最后一列
for (i = 1; i <=equ; i++)
{
a[i][var+1] = mat[r][l];
l++;
if (l > 6)
{
r++;
l = 1;
}
}
r = l = 1;//构建系数矩阵
for (i = 1; i <= 30; i++)
{
a[i][i] = 1;
for (j = 0; j < 4; j++)
{
xx = r + d[j][0];
yy = l + d[j][1];
if (xx >= 1 && xx <= 5 && yy >= 1 && yy <= 6)
a[i][(xx-1) * 6 + yy] = 1;
}
l++;
if (l > 6)
{
r++;
l = 1;
}
}
Gauss_XOR(a,x,var,equ);
print();
}
return 0;
}
本文介绍了一种解决LightsOut游戏的算法实现。通过构建增广矩阵并利用高斯消元法求解线性方程组,实现了从任意初始状态出发关闭所有灯的目标。文章详细解释了算法流程,并提供了完整的C++代码示例。
1万+

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



