POJ 1222 EXTENDED LIGHTS OUT(高斯消元)题解

题意:5*6的格子,你翻一个地方,那么这个地方和上下左右的格子都会翻面,要求把所有为1的格子翻成0,输出一个5*6的矩阵,把要翻的赋值1,不翻的0,每个格子只翻1次

思路:poj 1222 高斯消元详解

代码:

#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 + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
int a[][], x[], Case = ;
int equ, var;
int free_num,free_x[];
int Gauss(){
int max_r, col, k;
free_num = ;
equ = var = ;
for(k = , col = ; k < equ && col < var; k++, col++){
max_r = k;
for(int i = k + ; i < equ; i++){
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == ){
k--;
free_x[free_num++] = col;
continue;
}
if(max_r != k){
for(int j = col; j < var + ; j++){
swap(a[k][j], a[max_r][j]);
}
}
for(int i = k + ; i < equ; i++){
if(a[i][col] != ){
for(int j = col; j < var + ; j++){
a[i][j] ^= a[k][j];
}
}
}
}
for(int i = k; i < equ; i++){
if(a[i][col] != )
return -;
}
if(k < var) return var - k;
for(int i = var - ; i >= ; i--){
x[i] = a[i][var];
for(int j = i + ; j < var; j++){
x[i] ^= (a[i][j] & x[j]);
}
}
return ;
}
int pos(int i, int j){
return i * + j;
}
void solve(){
int u;
memset(a, , sizeof(a));
memset(x, , sizeof(x));
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
if(i > ) a[pos(i - , j)][pos(i, j)] = ;
if(i < ) a[pos(i + , j)][pos(i, j)] = ;
if(j > ) a[pos(i, j - )][pos(i, j)] = ;
if(j < ) a[pos(i, j + )][pos(i, j)] = ;
a[pos(i, j)][pos(i, j)] = ;
int u;
scanf("%d", &u);
a[pos(i, j)][] = u;
}
}
Gauss();
printf("PUZZLE #%d\n", Case++);
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
if(j != ) printf(" ");
printf("%d", x[pos(i, j)]);
}
printf("\n");
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
solve();
}
return ;
}
上一篇:Vue -cli 入门 --项目搭建(一)


下一篇:poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)