POJ 1753 Flip Game(高斯消元)
Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20103 | Accepted: 8710 |
Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
![](http://poj.org/images/1753_1.jpg)
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
这题直接dfs,或者是状态压缩去做可以的。
下面是高斯消元法。
高斯消元法要找最小的步数很难。、
尤其是有自由变元时,要枚举所以自由变元的取值情况来做。
1 /* 2 POJ 1753 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<iostream> 7 #include<algorithm> 8 #include<math.h> 9 using namespace std; 10 const int MAXN=30; 11 const int INF=0x3fffffff; 12 int a[MAXN][MAXN];//增广矩阵 13 int x[MAXN]; 14 int free_x[MAXN]; 15 16 // 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解, 17 //-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数) 18 //有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var. 19 int Gauss(int equ,int var) 20 { 21 int i,j,k; 22 int max_r;// 当前这列绝对值最大的行. 23 int col;//当前处理的列 24 int ta,tb; 25 int LCM; 26 int temp; 27 int free_index; 28 int num=0; 29 for(int i=0;i<=var;i++) 30 { 31 x[i]=0; 32 free_x[i]=0; 33 } 34 //转换为阶梯阵. 35 col=0; // 当前处理的列 36 for(k = 0;k < equ && col < var;k++,col++) 37 {// 枚举当前处理的行. 38 // 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差) 39 max_r=k; 40 for(i=k+1;i<equ;i++) 41 { 42 if(abs(a[i][col])>abs(a[max_r][col])) max_r=i; 43 } 44 if(max_r!=k) 45 {// 与第k行交换. 46 for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]); 47 } 48 if(a[k][col]==0) 49 {// 说明该col列第k行以下全是0了,则处理当前行的下一列. 50 k--; 51 free_x[num++]=col; 52 continue; 53 } 54 for(i=k+1;i<equ;i++) 55 {// 枚举要删去的行. 56 if(a[i][col]!=0) 57 { 58 // LCM = lcm(abs(a[i][col]),abs(a[k][col])); 59 // ta = LCM/abs(a[i][col]); 60 // tb = LCM/abs(a[k][col]); 61 // if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加 62 for(j=col;j<var+1;j++) 63 { 64 a[i][j] ^= a[k][j]; 65 } 66 } 67 } 68 } 69 70 // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0). 71 for (i = k; i < equ; i++) 72 { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换. 73 if (a[i][col] != 0) return -1; 74 } 75 int stat=1<<(var-k);//自由变元有 var-k 个 76 int res=INF; 77 for(i=0;i<stat;i++)//枚举所有变元 78 { 79 int cnt=0; 80 int index=i; 81 for(j=0;j<var-k;j++) 82 { 83 x[free_x[j]]=(index&1); 84 if(x[free_x[j]]) cnt++; 85 index>>=1; 86 } 87 for(j=k-1;j>=0;j--) 88 { 89 int tmp=a[j][var]; 90 int t=0; 91 while(a[j][t]==0)t++; 92 for(int l=t+1;l<var;l++) 93 if(a[j][l]) tmp^=x[l]; 94 x[t]=tmp; 95 if(x[t])cnt++; 96 } 97 if(cnt<res)res=cnt; 98 } 99 return res; 100 } 101 102 void init() 103 { 104 memset(a,0,sizeof(a)); 105 for(int i=0;i<4;i++) 106 for(int j=0;j<4;j++) 107 { 108 int t=i*4+j; 109 a[t][t]=1; 110 if(i>0)a[(i-1)*4+j][t]=1; 111 if(i<3)a[(i+1)*4+j][t]=1; 112 if(j>0)a[i*4+j-1][t]=1; 113 if(j<3)a[i*4+j+1][t]=1; 114 } 115 } 116 char str[20][20]; 117 int main() 118 { 119 // freopen("in.txt","r",stdin); 120 // freopen("out.txt","w",stdout); 121 while(scanf("%s",&str[0])!=EOF) 122 { 123 for(int i=1;i<4;i++)scanf("%s",&str[i]); 124 init(); 125 for(int i=0;i<4;i++) 126 for(int j=0;j<4;j++) 127 { 128 if(str[i][j]=='b')a[i*4+j][16]=0; 129 else a[i*4+j][16]=1; 130 } 131 int ans1=Gauss(16,16); 132 init(); 133 for(int i=0;i<4;i++) 134 for(int j=0;j<4;j++) 135 { 136 if(str[i][j]=='b')a[i*4+j][16]=1; 137 else a[i*4+j][16]=0; 138 } 139 int ans2=Gauss(16,16); 140 if(ans1==-1&&ans2==-1) 141 { 142 printf("Impossible\n"); 143 continue; 144 } 145 else if(ans1==-1&&ans2!=-1)printf("%d\n",ans2); 146 else if(ans1!=-1&&ans2==-1)printf("%d\n",ans1); 147 printf("%d\n",min(ans1,ans2)); 148 } 149 return 0; 150 }
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想