• POJ 1681 Painter's Problem 高斯消元


    利用高斯消元求解异或方程

    #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 = 250;
    const int dx[] = {0, 0, 1, -1};
    const int dy[] = {1, -1, 0, 0};
    int a[maxn][maxn], n;
    
    void Gauss() {
        for(int i = 0; i < n * n; i++) {
            int k = i;
            while(a[k][i] == 0 && k < n * n) k++;
            if(k >= n * n) break;
            for(int j = 0; j <= n * n; j++) swap(a[i][j], a[k][j]);
            for(int j = 0; j < n * n; j++) if(j != i && a[j][i] != 0) {
                for(int k = 0; k <= n * n; k++) {
                    a[j][k] ^= a[i][k];
                }
            }
        }
    }
    
    void pp() {
        for(int i = 0; i < n * n; i++) {
            for(int j = 0; j <= n * n; j++) {
                printf("%d ", a[i][j]);
            }
            puts("");
        }
    }
    
    void solve() {
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                int u = i * n + j;
                a[u][u] = 1;
                for(int k = 0; k < 4; k++) {
                    int nx = i + dx[k], ny = j + dy[k], nu = nx * n + ny;
                    if(nx >= 0 && nx < n && ny >= 0 && ny < n) {
                        a[u][nu] = a[nu][u] = 1;
                    }
                }
            }
        }
        Gauss();
        int ans = 0;
        bool bad = false;
        for(int i = 0; i < n * n; i++) {
            if(a[i][n * n]) ans++;
            int nsum = 0;
            for(int j = 0; j  < n * n; j++) nsum += a[i][j];
            if(nsum == 0 && a[i][n * n] == 1) bad = true;
        }
        if(bad) puts("inf");
        else printf("%d
    ", ans);
    }
    
    int main() {
        int T; scanf("%d", &T);
        while(T--) {
            memset(a, 0, sizeof(a));
            scanf("%d", &n);
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < n; j++) {
                    char tmp; scanf(" %c", &tmp);
                    if(tmp == 'w') a[i * n + j][n * n] = 1;
                    else a[i * n + j][n * n] = 0;
                }
            }
            solve();
        }
        return 0;
    }
    

      

  • 相关阅读:
    linux shell 的切换
    linux:TeamViewer安装使用详解
    虚拟机与主机互ping
    每R一点:层次聚类分析实例实战-dist、hclust、heatmap等(转)
    每R一点:各种画地图,全是知识点,90%人不知道!(转)
    R语言学习路线和常用数据挖掘包(转)
    SAS PROC MCMC example in R: Logistic Regression Random-Effects Model(转)
    SparkR安装部署及数据分析实例
    R语言的导数计算(转)
    awk之随机函数rand()和srand() (转)
  • 原文地址:https://www.cnblogs.com/rolight/p/4066464.html
Copyright © 2020-2023  润新知