zoukankan      html  css  js  c++  java
  • 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
    ", kase++);
            for(int i = 0; i < 5; i++) {
                for(int j = 0; j < 6; j++) {
                    printf("%d ", ans[i][j]);
                }
                puts("");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    webstorm2018.1 汉化
    微信小程序转发微信小程序转发
    微信小程序下拉刷新和上拉加载的实现
    微信小程序基本目录结构学习
    JavaScript中闭包的写法和作用详解
    前端面试 问题汇总
    js 循环
    vue.js的手脚架vue-cli项目搭建的步骤
    js 条件判断
    数组
  • 原文地址:https://www.cnblogs.com/rolight/p/4066462.html
Copyright © 2011-2022 走看看