POJ 1222 Gaussian elimination method to solve off-off problem

Source: Internet
Author: User

#include <cstdio> #include <cstring> #include <algorithm>const int maxn = 30;int INP[MAXN + 5][MAXN + 5],  Kase, T;void Gauss () {for (int i = 0; i < MAXN; i++) {int k = I;while (k < MAXN &&!inp[k][i]) k++;for (int j = 0; J <= Maxn; J + +) Std::swap (Inp[i][j], inp[k][j]); for (int j = 0; J < Maxn; J + +) if (i! = J && Inp[j][i]) for (int p = 0; p &lt ; = MAXN; p++) Inp[j][p] = Inp[i][p] ^ inp[j][p];}} int main (int argc, char const *argv[]) {scanf ("%d", &t), while (t--) {memset (INP, 0, sizeof (INP)); for (int i = 0; i < MAXN; i++) scanf ("%d", &AMP;INP[I][MAXN]), inp[i][i] = 1;for (int i = 0; i < MAXN; i++) {if (i% 6! = 0) Inp[i-1][i] = 1;if ( I% 6 = 5) Inp[i + 1][i] = 1;if (i > 5) inp[i-6][i] = 1;if (i <) Inp[i + 6][i] = 1;} Gauss ();p rintf ("PUZZLE #%d\n", ++kase), for (int i = 0; i < MAXN; i++) printf ("%d%c", INP[I][MAXN], I% 6 = 5? ' \ n ': ');} return 0;}

Upstairs for the simple version, downstairs for the version of the template.

#include <cstdio> #include <cstring> #include <algorithm>const int maxn = 30;int INP[MAXN + 5][MAXN + 5], Kase, t;//void Gauss ()//{//for (int i = 0; i < MAXN; i++)//{//int k = i;//while (k < MAXN &&!inp[k][i  ] k++;//for (int j = 0; J <= Maxn; j + +)/Std::swap (Inp[i][j], inp[k][j]);/for (int j = 0; J < Maxn; J + +)/if (i  ! = J && Inp[j][i])//for (int p = 0; p <= maxn; p++)/inp[j][p] = inp[i][p] ^ inp[j][p];//}//}int equ, Var,  X[MAXN], Free_num, Free_x[maxn];int gauss () {int max_r, col, k;free_num = 0;for (k = 0, col = 0; k < equ && Col < var; k++, col++) {max_r = k;for (int i = k + 1; i < equ; i++) if (ABS (Inp[i][col]) > abs (Inp[max_r][col])) Max_r = I;if (INP [Max_r] [col] = = 0) {k--; free_x[free_num++] = col;continue;} if (max_r! = k) for (int j = col; J < var + 1; j + +) Std::swap (Inp[k][j], inp[max_r][j]); for (int i = k + 1; i < equ; I + +) if (Inp[i][col]! = 0) for (int j = col; J < var + 1; j + +) Inp[i][j] ^= INP[K][J];} for (int i = k; i < equ; i++) if (inp[i][col]! = 0) return-1;if (K < Var) return var-k;for (int i = var-1; i > = 0; i--) {X[i] = inp[i][var];for (int j = i + 1; j < var; j + +) X[i] ^= (Inp[i][j] && x[j]);} return 0;} int main (int argc, char const *argv[]) {scanf ("%d", &t), while (t--) {memset (INP, 0, sizeof (INP)); for (int i = 0; i < MAXN; i++) scanf ("%d", &AMP;INP[I][MAXN]), inp[i][i] = 1;for (int i = 0; i < MAXN; i++) {if (i% 6! = 0) Inp[i-1][i] = 1;if ( I% 6 = 5) Inp[i + 1][i] = 1;if (i > 5) inp[i-6][i] = 1;if (i <) Inp[i + 6][i] = 1;}  Equ = $, var = 30;gauss ();p rintf ("PUZZLE #%d\n", ++kase); for (int i = 0; i < MAXN; i++) printf ("%d%c", x[i], I% 6 = 5 ? ' \ n ': ');} return 0;}

Give you a 5 row 6 columns of the matrix represents 30 lights, the matrix inp[i][j]=1 indicates that the light is on, =0 that the light is not lit. Set solution for Press[][]:press[i][j]=1, =0 means no press. So that the final state for all lights is off. Through analysis, we can know: the Order of the buttons do not matter, each button at most only need to press the next time, multi-press Cancel.

When the press acts on the INP, the effect is the corresponding bit modulus 2 plus (XOR), then each point as an unknown, his upper and lower values will determine his value.

POJ 1222 Gaussian elimination method to solve off-off problem

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.