Hdoj 4200 bad wiring Gaussian elimination

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = contents by --- cxlove

The painful memory is that some questions in the 4th HDU warm-up match soon had an algorithm to complete the code. It took three hours to debug the question during the competition, and more than two hours to debug the question against official data at night. Finally, I found the problem.

It is also a classic switch problem. Due to the recent impact, it is necessary to enumerate free yuan for the least number of times to directly remove the differences or equations from Gauss. The previous poj 3185 is similar. However, poj's data was weak, and no problems were revealed at the time. As a result, today's competition was so bad. The infinite wa made me very depressed.

The previous method was to find out the number of free variable elements, simplify the matrix, and then search for enumeration free variable elements. The result showed that the column where the free variable is located is not in the last few columns, this causes a problem when enumeration is free to change the Yuan. Then, each time you move all the columns that are currently 0 to the end, that is, free to change the yuan. Then enumerate

/* ID: cxloveprob: poj 4200 data: 2012.4.3hint: Gaussian elimination method */# include <iostream> # include <cstdio> # include <cstring> using namespace STD; int N, D; int A [105] [105], CNT; int ta [105] [105], X [105], ANS [105], var0; void debug () {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= n + 1; j ++) printf ("% d", a [I] [J]); printf ("\ n") ;}} void DFS (INT v) {If (V> N) {int temp = 0; For (INT I = 1; I <= N; I ++) x [I] = ans [I]; for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= n + 1; j ++) Ta [I] [J] = A [I] [J]; for (INT I = var0-1; I> = 1; I --) {for (Int J = I + 1; j <= N; j ++) ta [I] [n + 1] ^ = (X [J] & TA [I] [J]); X [I] = TA [I] [n + 1];} For (INT I = 1; I <= N; I ++) if (X [I]) temp ++; CNT = min (temp, CNT);} ans [v] = 0; DFS (V + 1); ans [v] = 1; DFS (V + 1);} void Gauss () {int I, j, JJ; for (I = 1, j = 1, JJ = 1; I <= N & JJ <= N; JJ ++) {int K = I; for (; k <= N; k ++) if (A [k] [J]) break; if (a [k] [J]) {for (INT r = 1; r <= n + 1; r ++) Swap (A [I] [r], a [k] [r]); For (k = 1; k <= N; k + +) If (K! = I & A [k] [J]) {for (INT r = 1; r <= n + 1; r ++) A [k] [r] ^ = A [I] [r];} I ++; j ++;} else {int val = N-JJ + J; for (INT cow = 1; cow <= N; cow ++) Swap (A [Cow] [J], a [Cow] [Val]);} if (I = n + 1) {CNT = 0; For (INT r = 1; r <= N; r ++) if (A [R] [N + 1]) CNT ++; printf ("% d \ n", CNT); return;} if (I <= N) {for (INT r = I; r <= N; r ++) if (a [R] [N + 1]) {printf ("impossible \ n "); return ;}} var0 = I; CNT = 1 <30; DFS (I); printf ("% d \ n", CNT);} int main () {int t; // freopen ("D. in "," r ", stdin); // freopen (" test. out "," W ", stdout); scanf (" % d ", & T); While (t --) {scanf (" % d ", & N, & D); memset (A, 0, sizeof (a); For (INT I = 1; I <= N; I ++) scanf ("% d ", & A [I] [n + 1]); For (INT I = 1; I <= N; I ++) for (Int J = max (1, i-d); j <= min (n, I + d); j ++) A [I] [J] = 1; 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.