题目链接 :
POJ1753
Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23784 | Accepted: 10245 |
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
- 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.
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
比较水的一道搜索题 可以用特别暴力的直接枚举每位上的操作O(4*4*2^16)的算法搞(才不会告诉你能直接AC呢)
不过通过观察 可以发现 只要第一列(或行)的操作确定了以后 为了达到所给形态 后面的每列(或行)的操作也就确定了
用样例来说
wwww (初始态)
wwww
wwww
wwww 枚举第一列 假设是 操作第1和第2个
得到
wbww
wbww
bwww
wwww
此时比较目标矩阵的第一列
第1 2 4个不同 那么对第二列的操作就必须是反第1 24个
否则第一列将不能满足。
于是可以直接枚举第一列(或行) 的操作 然后检查是否能达到目的 并记下步数
复杂度 O(4*4*2^4) 矩阵再大一好都可以 余裕~
附一个暴力的代码 太懒了 不想写第二个思路了~
两份代码一起附上
暴力 ver
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define maxn 65536
int dx[5]={0,0,-1,1,0},dy[5]={1,-1,0,0,0},ans=-1;
void aoe(bool pic[][6],int x,int y)
{
for(int i=0;i<5;i++)
pic[x+dx[i]][y+dy[i]]=!pic[x+dx[i]][y+dy[i]];
}
bool exam(bool pic[][6])
{
for(int i=1;i<5;i++)
for(int j=1;j<5;j++)
if((pic[i][j])!=(pic[1][1]))
return 0;
return 1;
}
void gank(bool map[][4])
{
bool pic[6][6]={0};
for(int i=0;i<maxn;i++)
{
int step=0;
for(int j=1;j<5;j++)
for(int k=1;k<5;k++)
pic[j][k]=map[j-1][k-1];
for(int j=0;j<16;j++)
if((i>>j)&1)
{
aoe(pic,j/4+1,j%4+1);
step++;
}
if(exam(pic))
if(ans==-1 || ans>step) ans=step;
}
}
int main()
{
bool map[4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
char ch=getchar();
if(ch=='w') map[i][j]=1;
else map[i][j]=0;
}
getchar();
}
gank(map);
if(ans==-1) printf("Impossible\n");
else printf("%d\n",ans);
return 0;
}
略暴力 ver
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
char str[8];
bool s[8][8],fa[8][8];
void filp(int x,int y)
{
fa[x][y]=!fa[x][y];
fa[x-1][y]=!fa[x-1][y];
fa[x][y-1]=!fa[x][y-1];
fa[x+1][y]=!fa[x+1][y];
fa[x][y+1]=!fa[x][y+1];
}
int main()
{
freopen("1.txt","r",stdin);
for(int i=1;i<5;i++)
{
scanf("%s",str+1);
for(int j=1;str[j];j++)
s[i][j]=(str[j]=='b')?1:0;
}
int ans=500,temp,fil;
for(int i=0;i<16;i++)
{
temp=0;
for(int j=1;j<5;j++)
for(int k=1;k<5;k++)
fa[j][k]=s[j][k];
for(int j=1;j<14;j<<=1)
if(j&i) temp++,filp(1,j/2);
for(int j=1;j<4;j++)
for(int k=1;k<5;k++)
if(fa[j][k]) temp++,filp(j+1,k);
fil=0;
for(int j=1;j<5;j++)
for(int k=1;k<5;k++)
fil+=fa[j][k];
if(fil==0) ans=min(ans,temp);
temp=0;
for(int j=1;j<5;j++)
for(int k=1;k<5;k++)
fa[j][k]=s[j][k];
for(int j=1;j<14;j<<=1)
if(j&i) temp++,filp(1,j/2);
for(int j=1;j<4;j++)
for(int k=1;k<5;k++)
if(fa[j][k]==0) temp++,filp(j+1,k);
fil=0;
for(int j=1;j<5;j++)
for(int k=1;k<5;k++)
fil+=fa[j][k];
if(fil==16) ans=min(ans,temp);
}
if(ans<400) printf("%d\n",ans);
else printf("Impossible\n");
return 0;
}