Ultraviolet A 1560-extended lights out
Question Link
If a matrix is given, 1 indicates that the light is on, 0 indicates that the light is turned off, and no switch is pressed, the four positions around will change. Ask one method to make all the lights dimmed.
Two methods:
1. Enumeration recurrence
This is relatively simple. enumerate the first line and then recursive it. If the previous line is on, the next line must be switched down.
2. Gaussian elimination,
This practice is somewhat embarrassing. Each Location corresponds to the top, bottom, and left positions, and one exclusive or expression can be listed. Then 30 positions correspond to 30 exclusive or expressions, the Gaussian elimination method can be used to find the solution of each position.
Code:
Gaussian elimination method:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int d[5][2] = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}};int t, a[31][31], out[6][6];void back() { for (int i = 29; i >= 0; i--) {out[i / 6][i % 6] = a[i][30];for (int j = 0; j < i; j++) { if (a[j][i])a[j][30] ^= a[i][30];} }}void guess() { for (int i = 0; i < 30; i++) {int k = i;for (; k < 30; k++) if (a[k][i]) break;for (int j = 0; j <= 30; j++) swap(a[i][j], a[k][j]);for (int j = i + 1; j < 30; j++) { if (a[j][i]) {for (int k = i; k <= 30; k++) a[j][k] ^= a[i][k]; }} } back();}int main() { int cas = 0; scanf("%d", &t); while (t--) {memset(a, 0, sizeof(a));for (int i = 0; i < 30; i++) scanf("%d", &a[i][30]);for (int i = 0; i < 30; i++) { int x = i / 6, y = i % 6; for (int j = 0; j < 5; j++) {int xx = x + d[j][0];int yy = y + d[j][1];if (xx < 0 || xx >= 5 || yy < 0 || yy >= 6) continue;a[i][xx * 6 + yy] = 1; }}guess();printf("PUZZLE #%d\n", ++cas);for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++)printf("%d ", out[i][j]); printf("%d\n", out[i][5]);} } return 0;}
Enumeration recurrence:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int d[5][2] = {{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}};const int N = 6;int t, g[N][N], tmp[N][N], out[N][N];bool judge(int s) { memset(out, 0, sizeof(out)); for (int i = 0; i < 5; i++)for (int j = 0; j < 6; j++) tmp[i][j] = g[i][j]; for (int i = 0; i < 6; i++) {if (s&(1<<i)) { out[0][i] = 1; for (int j = 0; j < 5; j++) {int xx = d[j][0];int yy = i + d[j][1];if (xx < 0 || yy < 0 || xx >= 5 || yy >= 6) continue;tmp[xx][yy] = (!tmp[xx][yy]); }} } for (int i = 1; i < 5; i++) {for (int j = 0; j < 6; j++) { if (tmp[i - 1][j]) {out[i][j] = 1;for (int k = 0; k < 5; k++) { int xx = i + d[k][0]; int yy = j + d[k][1]; if (xx < 0 || yy < 0 || xx >= 5 || yy >= 6) continue; tmp[xx][yy] = (!tmp[xx][yy]);} }} } for (int i = 0; i < 6; i++)if (tmp[4][i]) return false; return true;}void solve() { for (int i = 0; i < (1<<6); i++)if (judge(i)) return;}int main() { int cas = 0; scanf("%d", &t); while (t--) {for (int i = 0; i < 5; i++) for (int j = 0; j < 6; j++)scanf("%d", &g[i][j]);solve();printf("PUZZLE #%d\n", ++cas);for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++)printf("%d ", out[i][j]); printf("%d\n", out[i][5]);} } return 0;}