题意:5*6的格子,你翻一个地方,那么这个地方和上下左右的格子都会翻面,要求把所有为1的格子翻成0,输出一个5*6的矩阵,把要翻的赋值1,不翻的0,每个格子只翻1次
代码:
#include<queue> #include<cstring> #include<set> #include<map> #include<stack> #include<cmath> #include<vector> #include<cstdio> #include<iostream> #include<algorithm> #define eps 1e-9 typedef long long ll; const int maxn = 1e4 + 10; const int seed = 131; const ll MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; using namespace std; int a[35][35], x[35], Case = 1; int equ, var; int free_num,free_x[35]; int Gauss(){ int max_r, col, k; free_num = 0; equ = var = 30; for(k = 0, col = 0; k < equ && col < var; k++, col++){ max_r = k; for(int i = k + 1; i < equ; i++){ if(abs(a[i][col]) > abs(a[max_r][col])) max_r = i; } if(a[max_r][col] == 0){ k--; free_x[free_num++] = col; continue; } if(max_r != k){ for(int j = col; j < var + 1; j++){ swap(a[k][j], a[max_r][j]); } } for(int i = k + 1; i < equ; i++){ if(a[i][col] != 0){ for(int j = col; j < var + 1; j++){ a[i][j] ^= a[k][j]; } } } } for(int i = k; i < equ; i++){ if(a[i][col] != 0) return -1; } if(k < var) return var - k; for(int i = var - 1; i >= 0; i--){ x[i] = a[i][var]; for(int j = i + 1; j < var; j++){ x[i] ^= (a[i][j] & x[j]); } } return 0; } int pos(int i, int j){ return i * 6 + j; } void solve(){ int u; memset(a, 0, sizeof(a)); memset(x, 0, sizeof(x)); for(int i = 0; i < 5; i++){ for(int j = 0; j < 6; j++){ if(i > 0) a[pos(i - 1, j)][pos(i, j)] = 1; if(i < 4) a[pos(i + 1, j)][pos(i, j)] = 1; if(j > 0) a[pos(i, j - 1)][pos(i, j)] = 1; if(j < 5) a[pos(i, j + 1)][pos(i, j)] = 1; a[pos(i, j)][pos(i, j)] = 1; int u; scanf("%d", &u); a[pos(i, j)][30] = u; } } Gauss(); printf("PUZZLE #%d ", Case++); for(int i = 0; i < 5; i++){ for(int j = 0; j < 6; j++){ if(j != 0) printf(" "); printf("%d", x[pos(i, j)]); } printf(" "); } } int main(){ int T; scanf("%d", &T); while(T--){ solve(); } return 0; }