POJ1753 Flip Game(位运算+暴力枚举)
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).
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
题意:有一个4*4的棋盘,给你它的初始状态,每次可以翻动一个棋子,但翻动的同时也必须翻动它上下左右的另外四个棋子,求将棋盘翻成全黑和全白的最小次数
题解:这题有各种做法,dfs,高斯消元……
然而我比较懒,喜欢直线型思维,所以就打了个状压枚举状态的暴力
很显然,把同一个棋子翻动两次与不翻动没有任何区别
所以从不翻棋子到十六个全翻总共也就是2^16次
完全可以枚举!
然后就糊出来了!
代码如下:
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int sta,ans=9999999,tmp,flag,flag1,a[5][5],b[5][5],dx[]= {1,0,-1,0},dy[]= {0,1,0,-1}; void flip(int sta) { int x=1,y=1; tmp=0; flag=0; flag1=0; while(sta) { if(sta%2==1) { tmp++; b[x][y]=!b[x][y]; for(int i=0; i<=3; i++) { if(x+dx[i]<=4&&x+dx[i]>0&&y+dy[i]<=4&&dy[i]+y>0) { b[x+dx[i]][y+dy[i]]=!b[x+dx[i]][y+dy[i]]; } } } y++; if(y>4) { x++; y=1; } sta>>=1; } for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { if(b[i][j]) { flag=1; } } } for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { if(!b[i][j]) { flag1=1; } } } if(!flag||!flag1) { ans=min(ans,tmp); } } int main() { char c[5]; for(int i=1; i<=4; i++) { scanf("%s",c+1); for(int j=1; j<=4; j++) { if(c[j]=='b') { a[i][j]=1; } else { a[i][j]=0; } } } for(int sta=0; sta<=(1<<16)-1; sta++) { for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { b[i][j]=a[i][j]; } } flip(sta); } if(ans==9999999) { puts("Impossible"); return 0; } printf("%d\n",ans); return 0; }