POJ 3185 The Water bowls Gaussian elimination + Enumeration free variable _ state compression

Source: Internet
Author: User
The Water bowls
Time Limit: 1000MS Memory Limit: 65536K
Total submissions: 5085 accepted: 1991

Description the cows have a line of water bowls from which they. The bowls can is either right-side-up (properly oriented to serve refreshing cool water) or upside-down (a position which Holds no water). They want all water bowls to is right-side-up and thus use their wide to flip snouts.

Their snouts, though, are so wide this they flip not only one bowl but also the bowls on either side of this bowl (a total of three or--in the case of either end bowl--two bowls).

Given the initial state of the bowls (1=undrinkable, 0=drinkable--it even looks like a bowl), and what is the minimum number of bowl flips necessary to turn all bowls right-side-up?

Input Line 1: "A single" with space-separated integers

Output Line 1:the minimum number of bowl flips necessary to flip all bowls right-side-up (i.e., to 0). For the inputs given, it'll always be possible to find some combination of flips of that would manipulate the bowls to 20 0 ' S.

Sample Input

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0-0

Sample Output

3

Hint Explanation of the sample:

Flip Bowls 4, 9, and them all drinkable:
0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 [Initial State]
0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping Bowl 4]
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping Bowl 9]

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping Bowl 11]


20 integers of 0 or 1, known to change the number of position I, will change the number of i-1 and i+1 positions at the same time, changing the effect--0->1 or 1->0. Ask you the least action to make 20 integers all 0.


Train of thought: 20 equations, 20 variable--Gaussian elimination + makeup pressure enumeration free variable can be.




AC Code:


#include <cstdio> #include <cstring> #include <algorithm> #define MAXN #define INF 0x3f3f3f3f using n
Amespace std;
int A[MAXN][MAXN];
int FREE_REC[MAXN];
int X[MAXN];
int Equ, VAR;
    void Init_a () {equ = var = 20;
    memset (A, 0, sizeof (a));
        for (int i = 1; i < i++) {scanf ("%d", &a[i][var]), A[i][var] ^= 0;
        A[i][i] = 1;
        A[I][I-1] = 1;
    if (I <) a[i][i+1] = 1;
    an int Gauss () {int max_r, K;
    int col = 0;
    int num = 0;
        for (k = 0; k < equ && Col < var; k++, col++) {max_r = k;
        for (int i = k+1 i < equ i++) if (A[i][col] > a[max_r][i]) max_r = i;
        if (Max_r!= k) for (int i = col; i < var+1; i++) Swap (a[k][i), a[max_r][i]);
            if (a[k][col] = = 0) {k--;
            free_rec[num++] = col;
        Continue for (int i = k+1 I < EQu i++) if (A[i][col]!= 0) for (int j = col; J < Var+1; J +) A[i][j] ^= a[k
    ][J];
    for (int i = k+1 i < equ i++) if (A[i][col)!= 0) return-1;
    if (Var > k) return var-k;
return 0;
    } void Solve (int S) {int state = (1<<s);
    int ans = INF;
        for (int i = 0; i < state; i++) {int cnt = 0;
                for (int j = 0; J < S; J +) {if ((1<<j) & i) {cnt++;
            X[FREE_REC[J]] = 1;
        else X[free_rec[j]] = 0;
            for (int j = var-s-1 J >= 0; j--) {int temp = A[j][var];
            for (int l = j+1 L < var; l++) if (a[j][l]) temp ^= x[l];
            X[J] = temp; CNT + + X[j]?
        1:0;
    ans = min (ans, CNT);
printf ("%d\n", ans); int main () {WHIle (scanf ("%d", &a[0][20])!= EOF) {a[0][0] = 1, a[0][1] = 1, a[0][20] ^= 0;
        Init_a ();
    Solve (Gauss ());
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.