标签:
解题思路:
30个格子,对每一个格子建立一个方程,高斯消元
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #define LL lonA lonA #define FOR(i, x, y) for(int i=x;i<=y;i++) using namespace std; const int MAXN = 50; int A[MAXN][MAXN]; int x[MAXN]; void init() { memset(A, 0, sizeof(A)); for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { int p = 6 * i + j; A[p][p] = 1; if(i > 0) A[(i-1)*6+j][p] = 1; if(i < 4) A[(i+1)*6+j][p] = 1; if(j > 0) A[i*6+j-1][p] = 1; if(j < 5) A[i*6+j+1][p] = 1; } } } void Gauss() { int k; int row, col; for (row =0, col =0; row <30&& col <30; row++, col++) { for (k = row; k <30; k++) if (A[k][col] !=0) break; if (k ==30) { row--; continue; } if (k != row) for (int i = col; i <=30; i++) swap(A[row][i], A[k][i]); for (int i = row +1; i <30; i++) if (A[i][col]) for (int j = col; j <=30; j++) A[i][j] ^= A[row][j]; } for (int i = row; i >=0; i--) { x[i] = A[i][30]; for (int j =29; j > i; j--) x[i] ^= (A[i][j] && x[j]); } } int main() { int T, kcase = 1; scanf("%d", &T); while(T--) { init(); for(int i=0;i<30;i++) scanf("%d", &A[i][30]); Gauss(); printf("PUZZLE #%d\n", kcase++); for(int i=0;i<30;i++) { if((i + 1) % 6 == 0) printf("%d\n", x[i]); else printf("%d ", x[i]); } } return 0; }
标签:
原文地址:http://blog.csdn.net/moguxiaozhe/article/details/44727813