Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 45149 | Accepted: 19339 |
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).
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.
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
题意:有4×4的棋盘,上面的棋子一面是黑的,一面是白的。规定翻转一个棋子的同时也要翻转它的上、下、左、右的棋子,问给定一个棋盘的棋子状态,至少需要翻转多少个棋子,能使得所有棋子都是白的或黑的
解题思路:枚举第一行翻转的方案,然后逐行向下更新,如果上一行是1的话,那么下面一行肯定要翻转,最后判断一下,最后一行是不是都是0,如果都是,则维护最小的翻转次数
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int a[20][20],x[20][20];
char ch[10][10];
int dir[5][2]= {{1,0},{-1,0},{0,1},{0,-1},{0,0}};
int mi;
int check(int xx,int yy)
{
int res=a[xx][yy];
for(int i=0; i<5; i++)
{
int x1=xx+dir[i][0];
int y1=yy+dir[i][1];
if(x1<0||y1<0||x1>=4||y1>=4) continue;
res+=x[x1][y1];
}
return res%2;
}
int solve(int k)
{
for(int i=1; i<4; i++)
for(int j=0; j<4; j++)
if(check(i-1,j)!=k) x[i][j]=1;
for(int i=0; i<4; i++)
if(check(3,i)!=k) return -1;
int res=0;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
res+=x[i][j];
return res;
}
int main()
{
while(~scanf("%s",ch[0]))
{
for(int i=1; i<4; i++)
scanf("%s",ch[i]);
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
a[i][j]=ch[i][j]=='w'?1:0;
mi=INF;
for(int i=0; i<(1<<4); i++)
{
memset(x,0,sizeof x);
for(int j=0; j<4; j++)
x[0][3-j]=i>>j&1;
int res=solve(0);
if(res==-1) continue;
mi=min(mi,res);
}
for(int i=0; i<(1<<4); i++)
{
memset(x,0,sizeof x);
for(int j=0; j<4; j++)
x[0][3-j]=i>>j&1;
int res=solve(1);
if(res==-1) continue;
mi=min(mi,res);
}
if(mi==INF) printf("Impossible\n");
else printf("%d\n",mi);
}
return 0;
}