POJ 3185 the water bowls

Source: Internet
Author: User

The water bowls

Test instructions: given 20 01 strings (The final state), each point changes to affect the left and right points, the question is finally 20 0 required minimum number of operations?

Water problem. Modify the augmented matrix directly; it seems that the optimal solution is not Gaussian elimination (if there is a Gauss 0ms a message ~ ~ ~), a lot of 0ms, I used 32ms;

#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<map>#include<queue>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineINF 0x3f3f3f3fintdir[2][4] = {{0,1,0,-1},{1,0,-1,0}};inta[ at][ at];intEquvar;intx[ at],free_var[ -];voidDebug () {Puts ("********"); inti,j; Rep0 (i,0, Equ) {Rep1 (J,0,var) cout<<a[i][j]<<" "; cout<<Endl; }puts ("********");}intGauss () {inti,j,k,row,col,cnt =0;  for(row =0, col =0; row < equ && Col <var; row++,col++){        intMX =Row; Rep0 (J,row+1, equ)if(ABS (A[j][col]) > abs (A[mx][col])) MX =J; if(A[mx][col] = =0) {row--;//The number of free arguments cannot be recorded here, only the useless col can be recorded .free_var[cnt++] = col;//recording the marking of free variable elements;            Continue; }        if(MX! =row) Rep1 (K,col,var) Swap (a[row][k],a[mx][k]); Rep0 (J,row+1, Equ) {            if(A[j][col]) {REP1 (K,col,var) A[j][k]^=A[row][k]; }        }    }    //debug (); //rep0 (i,row,equ)//if (A[i][var]! = 0) return-1; //No solution//The free variable is enumerated, row represents a useful equation number equation, but we can say that there are many groups of solutions under the premise of judging the solution;//if (Row < Var) return var-row; //returns the number of free arguments directly when no enumeration is required    intAns = Inf,tot =1<< (var-row); Rep0 (i,0, tot) {        intCNT =0, TMP =i; Rep0 (J,0,var-row) {X[free_var[j]]= (tmp&1); if(X[free_var[j]]) cnt++;//**TMP >>=1; } rep_1 (I,row-1,0) {X[i]= a[i][var];//now assigned to A[i][var], if the free variable is still equal to 0, will not be repeated calculation;Rep0 (j,i+1, Equ) {X[i]^= (A[i][j] && x[j]);//The J Lantern will affect the first lamp, and the first J lamp is also lit            }            if(X[i]) cnt++; } ans=min (ans,cnt); }    returnans;}voidInitintN) {Rep0 (i,0, N) {        intID =i; A[id][id]=1; if(Id >0) a[id-1][id] =1; if(ID <var-1) a[id+1][id] =1; }}inttmp[ -];intMain () {intn = -, I,id =0; Equ=var=N; Rep0 (i,0, -) {scanf ("%d", &a[i][var]);    } init (n); //debug ();printf"%d\n", Gauss ()); return 0;}
View Code

POJ 3185 the water bowls

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.