POJ 3185 The water bowls (math: Gaussian elimination)

Source: Internet
Author: User
Tags abs

Each location affects only adjacent locations

Do the Gaussian elimination and then enumerate.

The code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAXN usi

NG namespace Std;
int X[MAXN];
int FREE_X[MAXN];

int A[MAXN][MAXN];
        void print () {for (Int. i=0; i<20; ++i) {for (int j=0; j<=20; ++j) printf ("%d", a[i][j]);
    Puts ("");
    }} int Gauss (int equ, int var) {int I, J, K, Col;
    int TA, TB, LCM;
    int tmp, FREE_INDEX;

    int Free_x_num, Max_r;
    int num = 0;
        for (int i=0; i<=var; ++i) {x[i] = 0;
    Free_x[i] = 0;
    } col = 0;
        for (k=0; k<equ&&col<var; ++k, ++col) {max_r = k;
        for (i=k+1; i<equ; ++i) {if (ABS (A[i][col]) > abs (A[max_r][col])) Max_r = i;
        } if (Max_r! = k) for (j=k; j<=var; ++j) Swap (A[k][j], a[max_r][j]);
            if (a[k][col] = = 0) {--k;
            free_x[num++] = col;
        Continue
    }    For (i=k+1, i<equ; ++i) {if (A[i][col]) {for (j=col; j<=var; ++j)
            A[I][J] ^= a[k][j];
    }}} for (i=k; i<equ; ++i) {if (A[i][col]) return-1;
} return var-k;
    } void Solve (int equ, int var) {int ans = Gauss (equ, Var);
        if (ans = = 0) {int res = 0;
        for (int i=0; i<equ; ++i) Res + = X[i];
        printf ("%d\n", res);
    return;
        } else {int res = 0x3f3f3f3f;
        int tot = (1<<ans);
            for (int i=0; i<tot; ++i) {int cnt = 0;
                    for (int j=0; j<ans; ++j) {if (i& (1<<j)) {X[free_x[j]] = 1; 
                ++cnt;
            } else X[free_x[j]] = 0;
                } for (int j=var-ans-1; j>=0;--j) {int tmp = A[j][var];
                   for (int l=j+1; l<var; ++l) if (A[j][l])     TMP ^= X[L];
                X[J] = tmp;
            if (X[j]) ++cnt;
        } res = min (res, CNT);
    } printf ("%d\n", res);
    }} int main (void) {int nx;
        while (~SCANF ("%d", &nx)) {memset (A, 0, sizeof (a));
        A[0][20] = NX;

        for (int i=1; i<20; ++i) scanf ("%d", &a[i][20]);
        A[0][0] = a[0][1] = 1;
        A[19][18] = a[19][19] = 1;
        for (int i=1; i<19; ++i) {a[i][i] = a[i][i-1] = a[i][i+1] = 1;
        }//print ();
        Solve (20, 20);
    Print ();
} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.