Title Address: POJ 1753
The third time to do the problem. The first time is just learning to do when the search, the second is just learning state compression enumeration When doing, this is just learning Gaussian elimination, and every time do very hard. There should be no other way to look at this problem ...
In addition to the Gaussian elimination, this problem requires the enumeration of the elements, which is the state compression enumeration, and then returns to the iterative solution equation.
The code is as follows:
#include <iostream> #include <string.h> #include <math.h> #include <queue> #include < algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h>using namespace std; #define LL __int64#define Pi ACOs ( -1.0) const int Mod=1e9+7;const int Inf=0x3f3f3f3f;const double eqs=1e-6;i NT Mp[5][5], a[20][20], Free_num, free_x[20], x[20];int jx[]={0,0,1,-1};int jy[]={1,-1,0,0};int gauss () {int I, J, K, free_num=0, H, TMP, T; for (i=0,j=0;i<16&&j<16;i++,j++) {if (a[i][j]==0) {for (k=i+1;k<16;k++ ) {if (a[k][j]) break; } if (k==16) {free_x[free_num++]=j; i--; Continue; } for (h=j;h<17;h++) {swap (a[i][h],a[k][h]); }} for (k=i+1;k<16;k++) {if (A[k][j]) { for (h=j;h<17;h++) {a[k][h]^=a[i][h]; }}}} tmp=i; for (j=i;j<16;j++) {if (a[j][16]) return INF; } int ans=inf; int tot=1<<free_num; printf ("%d\n", free_num); for (i=0;i<tot;i++) {int cnt=0; for (j=0;j<free_num;j++) {if (i& (1<<j)) {x[free_x[j]]=1; cnt++; } else{x[free_x[j]]=0; }}//printf ("%d\n", CNT); for (j=tmp-1;j>=0;j--) {t=0; while (a[j][t]==0) t++; X[T]=A[J][16]; for (k=t+1;k<16;k++) {if (A[j][k]) x[t]^=x[k]; } Cnt+=x[t]; }//printf ("%d\n", CNT); Ans=min (ANS,CNT); } return ans; void Init () {memset (a,0,sizeof (a)); for (int i=0;i<4;i++) {for (int. j=0;j<4;j++) {for (int k=0;k<4;k++) { int x=i+jx[k]; int y=j+jy[k]; if (x>=0&&x<4&&y>=0&&y<4) {a[4*i+j][4*x+y]=1; }} a[4*i+j][4*i+j]=1; }}}int Main () {char s[6]; int I, j, K, X, Y, ans1, ans2; for (i=0;i<4;i++) {scanf ("%s", s); for (j=0;j<4; j + +) {if (s[j]== ' B ') mp[i][j]=0; else mp[i][j]=1; }} init (); for (i=0;i<4;i++) {for (j=0;j<4;j++) {a[4*i+j][4*4]=mp[i][j]; }} Ans1=gauss (); Init (); for (i=0;i<4;i++) {for (j=0;j<4;j++) {a[4*i+j][4*4]=1-mp[i][j]; }} Ans2=gauss (); if (ans1==inf&&ans2==inf) {printf ("impossible\n"); } else{printf ("%d\n", Min (ans1,ans2)); } return 0;}
POJ 1753 Flip Game (Gaussian elimination)