Poj 1222 extended lights out

Source: Internet
Author: User

The fifth question of Gauss's elimination of Yuan seems to be the only way to do this. I still have little knowledge. Don't lie to me anyway ~!


Question:

A 5*6 matrix composed of lights is given. When the status of a lamp changes, the adjacent (edge adjacent, that is, the upper and lower sides, if any) lamp status also changes. Changing the status of those lights can turn off all the lights.



Solution:

30 lamps represent 30 equations. The expression of the equation indicates that operating those lights has an impact on the current lamp.

The last step is Gaussian elimination to solve the equation, but it is just a Model 2.



The following code is used:

# Include <set> # include <map> # include <queue> # include <math. h> # include <vector> # include <string> # include <stdio. h> # include <string. h> # include <stdlib. h >#include <iostream> # include <algorithm> # define EPS 1e-6 # define PI ACOs (-1.0) # define INF 107374182 # define inf64 1152921504606846976 # define LC l, m, tr <1 # define RC m + 1, R, TR <1 | 1 # define iabs (x)> 0? (X):-(x) # define clear1 (A, X, size) memset (A, X, sizeof (A [0]) * (size )) # define clearall (A, x) memset (A, X, sizeof (A) # define memcopy1 (A, X, size) memcpy (A, X, sizeof (X [0]) * (size) # define memcopyall (A, x) memcpy (A, X, sizeof (x) # define max (x, y) (x)> (y ))? (X): (y) # define min (x, y) (x) <(y ))? (X): (y) using namespace STD; struct node {long num [35]; node () {clearall (Num, 0);} void clen () {clearall (Num, 0) ;}}; struct node matrix [35]; int n, m, Len; bool free_x [35]; long x [35], P; void debug (void) {puts (""); int I, j; for (I = 0; I <m; I ++) {for (j = 0; j <n + 1; j ++) {cout <matrix [I]. num [J] <";}cout <Endl ;}int guass () {int I, J, K, Col; clearall (X, 0); clea Rall (free_x, 1); // clears the solution set. All variables are labeled as free variables for (k = 0, Col = 0; k <M & Col <N; + + k, ++ col) // enumeration column {// printf ("% d \ n", k); // debug (); int max_r = K; // find the row with the largest absolute value of the col column element and swap it with row K. (to reduce the error during Division) while (Matrix [max_r]. num [col] = 0 & max_r <m) max_r ++;/* for (I = k + 1; I <m; ++ I) {If (iabs (Matrix [I]. num [col])> iabs (Matrix [max_r]. num [col]) max_r = I;} */If (max_r! = K) // exchange {for (I = K; I <n + 1; ++ I) Swap (Matrix [K]. num [I], matrix [max_r]. num [I]);}/* If (Matrix [K]. num [col]! = 0) // if all the columns for the row should be 0, enumerate the next column of the row {k --; continue;} */for (I = k + 1; I <m; ++ I) // perform Elementary Transformations on the col behind K to form a step matrix {If (Matrix [I]. num [col]! = 0) {long X1 = matrix [I]. num [col], X2 = matrix [K]. num [col]; for (j = Col; j <n + 1; ++ J) {matrix [I]. num [J] = matrix [I]. num [J] * x2-X1 * matrix [K]. num [J]; matrix [I]. num [J] = (Matrix [I]. num [J] % P + p) % P;} // debug () ;}}// debug (); // 1. solution-free: the simplified augmented array exists (0, 0 ,..., a) such a row (! = 0). R ()! = R (A') unsolvable/* for (I = K; I <m; ++ I) {If (iabs (Matrix [I]. num [col])> EPS) Return-1;} * // 2. infinite solution: in the N * (n + 1) augmented array (0, 0 ,..., 0) This indicates that no strict upper triangle array is formed. // The number of rows displayed is the number of free yuan. that is, R (A) = R (A') <N // printf ("% d \ n", k, n);/* If (k <n) {// The comments are free variables for multiple solutions. // first, there are n-k free variables, that is, there are at least n-k uncertain variables. int num = 0, freeidx; for (I = k-1; I> = 0; -- I) {num = 0; // determines the number of uncertain variable elements in the row, if there are more than one variable, it cannot be solved, and they are still variable elements of uncertainty. double TM P = matrix [I]. num [N]; // row I won't be (0, 0 ,..., 0), because the rows are from row K to Row M. // Similarly, row I will not be (0, 0 ,..., a),! If it is 0, there is no such solution. for (j = 0; j <n; ++ J) {If (iabs (Matrix [I]. num [J])> EPS & free_x [J]) {num ++; freeidx = J ;}} if (Num> 1) continue; // The fixed variable cannot be obtained. // there is only one uncertain variable free_index, so we can solve the variable and the variable is determined. TMP = matrix [I]. num [N]; for (j = 0; j <n; ++ J) {If (iabs (Matrix [I]. num [J])> EPS & J! = Freeidx) TMP-= matrix [I]. num [J] * X [J];} X [freeidx] = tmp/matrix [I]. num [freeidx]; free_x [freeidx] = 0;} return n-k;} * // 3. unique Solution: a strict upper triangle array is formed in the N * (n + 1) augmented array. // calculate the Xn-1, Xn-2... x0.for (I = k-1; I> = 0; -- I) {long TMP = matrix [I]. num [N]; for (j = I + 1; j <n; ++ J) {TMP = (TMP-matrix [I]. num [J] * X [J]) % P + p) % P;} while (TMP % matrix [I]. num [I]) TMP + = P; X [I] = (TMP/matrix [I]. num [I]) % P + p) % P;} return 0;} const char s [9] [10] = {"abde", "ABC ", "bcef", "ADG", "bdefh", "CFI", "degh", "Ghi", "efhi"}; const int num [9] = {4, 3, 4, 3, 5, 3, 4}; int main () {int T, case1 = 1; scanf ("% d", & T); P = 2; n = 30; M = 30; while (t --) {clearall (matrix, 0); For (INT I = 0; I <5; I ++) {for (Int J = 0; j <6; j ++) {scanf ("% d", & matrix [I * 6 + J]. num [N]); matrix [I * 6 + J]. num [I * 6 + J] = 1; if (j> 0) matrix [I * 6 + J-1]. num [I * 6 + J] = 1; if (J + 1 <6) matrix [I * 6 + J + 1]. num [I * 6 + J] = 1; if (I> 0) matrix [(I-1) * 6 + J]. num [I * 6 + J] = 1; if (I + 1 <5) matrix [(I + 1) * 6 + J]. num [I * 6 + J] = 1 ;}// debug (); guass (); // debug (); int CNT = 0; bool flat = false; printf ("puzzle # % d \ n", case1 ++); For (INT I = 0; I <n; I ++) {If (flat) printf (""); printf ("% LLD", X [I]); CNT ++; flat = true; If (CNT = 6) {flat = false; CNT = 0; puts ("") ;}// debug () ;}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.