POJ 1222 EXTENDED LIGHTS OUT 高斯消元
最基本的高斯消元异或方程解决开关灯问题
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <iostream>
#include <string>
using namespace std;
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const double DINF = 1e60;
const int maxn = 40;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int a0[maxn][maxn], a[maxn][maxn], ans[maxn][maxn];
void init() {
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 6; j++) {
int u = i * 6 + j;
a0[u][u] = 1;
for(int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k], nu = nx * 6 + ny;
if(nx >= 0 && nx < 5 && ny >= 0 && ny < 6) {
a0[nu][u] = a0[u][nu] = 1;
}
}
}
}
}
void gauss() {
for(int i = 0; i < 30; i++) {
int k = i;
while(a[k][i] == 0 && k < 30) k++;
for(int j = 0; j <= 30; j++) swap(a[i][j], a[k][j]);
for(int j = 0; j < 30; j++) {
if(i != j && a[j][i] != 0) {
for(int k = 0; k <= 30; k++) {
a[j][k] = a[i][k] ^ a[j][k];
}
}
}
}
}
int main() {
init();
int T, kase = 1; scanf("%d", &T);
while(T--) {
memcpy(a, a0, sizeof(a0));
for(int i = 0; i < 30; i++) scanf("%d", &a[i][30]);
gauss();
for(int i = 0; i < 30; i++) {
int x = i / 6, y = i % 6;
if(a[i][30]) ans[x][y] = 1;
else ans[x][y] = 0;
}
printf("PUZZLE #%d\n", kase++);
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 6; j++) {
printf("%d ", ans[i][j]);
}
puts("");
}
}
return 0;
}

浙公网安备 33010602011771号