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.
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
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).
bwwb bbwb bwwb bwww
4
给个4*4的01序列,每次翻转当前格子会翻转上下左右格子,求全部为0或1 的最小翻转步数
#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
using namespace std;
const int N = 110;
char str[N];
int dp[N][N], s[N][N], f[N][N];
int getlen(int k)
{
int cnt=0;
while(k)
{
if(k&1) cnt++;
k>>=1;
}
return cnt;
}
int judge(int k,int flag)
{
int cnt=0;
memset(f,0,sizeof(f));
for(int i=0; i<4; i++)
{
if(k&(1<<i))
{
f[1][i]+=1,f[2][i]+=1,f[1][i+1]+=1;
if(i!=0) f[1][i-1]+=1;
}
}
for(int i=2; i<=4; i++)
{
for(int j=0; j<4; j++)
{
int sum=s[i-1][j]+f[i-1][j];
sum%=2;
if(sum!=flag)
{
f[i][j]+=1,f[i][j+1]+=1,f[i-1][j]+=1,f[i+1][j]+=1;
if(j!=0) f[i][j-1]+=1;
cnt++;
}
}
}
for(int i=0; i<4; i++)
{
int sum=s[4][i]+f[4][i];
sum%=2;
if(sum!=flag)
return -1;
}
return cnt;
}
int main()
{
for(int i=1; i<=4; i++)
{
scanf("%s",str);
for(int j=0; j<4; j++)
{
if(str[j]=='b') s[i][j]=1;
else s[i][j]=0;
}
}
int ans=INT_MAX;
for(int i=0; i<(1<<4); i++)
{
int sum=getlen(i), cnt1=judge(i,0), cnt2=judge(i,1);
if(cnt1!=-1)
{
cnt1+=sum;
ans=min(ans,cnt1);
}
if(cnt2!=-1)
{
cnt2+=sum;
ans=min(ans,cnt2);
}
}
if(ans==INT_MAX) printf("Impossible\n");
else printf("%d\n",ans);
return 0;
}