POJ 1222 EXTENDED LIGHTS Out Enumeration | | Gaussian elimination element

Source: Internet
Author: User
Tags cas numeric printf first row cmath

The main idea is not to say, that is, the Board on the 1 full change 0 can be

If enumerated, it appears that there are 2 of the 30 possible, in fact not.

In fact, you only need to enumerate the state of the first row, and then, if you want to solve the problem, you must push it according to the state of the first row.

For each position, if this column of the previous row has 1, the key must be pressed down, otherwise it is impossible to achieve the required results.

The enumeration code is as follows, using the binary enumeration directly

/* id:sdj22251 prog:subset lang:c++ * * #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitse t> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> # Include <cctype> #include <map> #include <string> #include <cstring> #include <cmath> #
Include <ctime> #define MAXN 100007 #define INF 1000000000 #define EPS 1e-7 using namespace std;
int a[5][6], MX, flag, MI, ta[5][6];
int tmp[5][6];
    void Press (int i, int j) {Ta[i][j] =!ta[i][j];
    if (i > 0) ta[i-1][j] =!ta[i-1][j];
    if (J > 0) ta[i][j-1] =!ta[i][j-1];
    if (I < 4) ta[i + 1][j] =!ta[i + 1][j];
if (J < 5) Ta[i][j + 1] =!ta[i][j + 1]; } bool OK ({for (int i = 0; i < 6; i++) if (Ta[4][i]) return false;
return true; } void Output () {for (int i = 0, i < 5; i++) {for (int j = 0; J < 6; J + +) {if (j
            ) printf ("");
        printf ("%d", tmp[i][j]);
    } printf ("\ n"); }} void Solve () {for (int k = 0; k <, k++) {for (int i = 0; i < 5; i++) for (int j = 0; J < 6;
        J + +) Ta[i][j] = A[i][j];
        memset (tmp, 0, sizeof (TMP));
        for (int j = 0; J < 6; J + +) if (K & (1 << j)) {Press (0, J); tmp[0][j] = 1;} 
                    for (int i = 1, i < 5; i++) for (int j = 0; J < 6; J + +) if (Ta[i-1][j]) {
                    TMP[I][J] = 1;
                Press (I, j);
    } if (ok ()) {output (); return;}
    }} int main () {int T, cas = 0;
    scanf ("%d", &t);
      while (t--) {for (int i = 0; i < 5; i++)      for (int j = 0; J < 6; J + +) scanf ("%d", &a[i][j]);
        printf ("PUZZLE #%d\n", ++cas);
    Solve ();
} return 0; }


And then it's Gauss elimination.

The process of establishing the equations is a reference to http://hi.baidu.com/ofeitian/blog/item/9385662a30c2ebf7e7cd4016.html

Have to say that this is a kind of reverse thinking, the question is how to eliminate all, the establishment of the equation is how to solve from the total to the current state, in fact, because a light two times the effect is the same, so the solution is the final solution.

Then the solution, it is best to use the XOR or operation. This avoids negative and particularly large numbers. It is possible to experiment with this.

This is the use of Gaussian elimination method to find the different or the equation set

An XOR Equation group is a group of equations that are shaped like this:


M[0][0]X[0]^M[0][1]X[1]^...^M[0][N-1]X[N-1]=B[0]
M[1][0]X[0]^M[1][1]X[1]^...^M[1][N-1]X[N-1]=B[1]
...
M[N-1][0]X[0]^M[N-1][1]X[1]^...^M[N-1][N-1]X[N-1]=B[N-1]


where "^" Denotes xor (XOR, exclusive OR), M[i][j] represents the coefficient of x[j] in the i-clause, which is 1 or 0. B[i] is a constant at the right end of the I equation, which is 1 or 0.


This equation can be applied to the Gaussian elimination method, only the original addition and subtraction operation can be replaced by an XOR operation, the left side of the two equations or later, their public items are gone.


The specific operation method is this: for K=0..n-1, find a m[i][k] not 0 of the line I, to the K-line exchange, with the K-line to XOR or all the following m[i][j] not 0 of the line I, eliminating their k coefficients, so that the original matrix into the upper triangular matrix The last line has only one unknown, this unknown has been found out, with it with all of the above-mentioned equation is different or, it is underestimated all the unknown, at this time the penultimate line is also only an unknown, it was asked to come out, in such a way can be found from the bottom up all unknowns.


That's what I'm asking for.


/* id:sdj22251 prog:subset lang:c++ * * #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitse t> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> # Include <cctype> #include <map> #include <string> #include <cstring> #include <cmath> #
Include <ctime> #define MAXN 100007 #define INF 1000000000 #define EPS 1e-7 using namespace std;
int a[10][10];
int d[33][33], ans[33];
int n;
    int Gauss () {int pos;
        for (int k = 0; k < n; k++) {pos =-1; for (int i = k; i < n; i++)//Find the first non-0-element row in this column if (d[i][k]! = 0) {pos = i; break 
            ; } for (int i = k; I<= N; i++) Swap (D[k][i], d[pos][i]);   
            Switching positions, we often do this when we are learning linear algebra for (int i = k + 1; i < n; i++) {if (d[i][k] = = 0) continue;
        for (int j = k; J <= N; j + +)//For the column that appears below, there are 1 rows that need to be removed 1 D[i][j] ^= d[k][j]; }} for (int i = n-1, i >= 0; i--) {for (int j = 0; J < i; J + +) {if (d[j][i
            ] = = 0) continue;
        for (int k = 0; k <= N; k++) d[j][k] ^= d[i][k];
        } int flag = 0;   
        for (int j = 0; J < N; j + +) if (d[i][j]) flag = 1;   if (!flag) return 0;
    If the coefficient of a row is all 0, the description is not ans[i] = d[i][n]; } for (int i = 0; i < 5; i++) for (int j = 0; J < 6; J + +) {printf ("%d", Ans[i * 6 + j]
            );
            if (j = = 5) printf ("\ n");
        else printf ("");
} return 1;
    } int main () {int T, cas = 0;
    scanf ("%d", &t);
    n = 30;
    while (t--){memset (d,0,sizeof (d)); for (int i = 0; i < 5; i++) for (int j = 0; J < 6; J + +) {scanf ("%d", &a[i]
                [j]);
            if (A[i][j]) d[6 * i + j][n] = 1; } for (int i = 0, i < 5; i++) for (int j = 0; J < 6; J + +) {D[6 * i +
                J][6 * i + j] = 1;
                if (i > 0) d[6 * i + j][6 * (i-1) + j] = 1;
                if (J > 0) d[6 * i + j][6 * i + j-1] = 1;
                if (I < 4) d[6 * i + j][6 * (i + 1) + j] = 1;
            if (J < 5) d[6 * i + j][6 * i + j + 1] = 1;
        } printf ("PUZZLE #%d\n", ++cas);
    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.