Flip Game
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:
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
分析:
枚举题,水题水多了,刚开始完全没有思路,还没做过多少dfs,bfs的题。百科给这题的归类是枚举,这该怎么枚举。
后来把这题放下,过了一天想了想在网上找的一些能过的代码。看别人的结题报告看到一段话超过四行就没想细看,这算强迫症么。
后来还是勉强在这位同学的博客里找到了思路http://blog.sina.com.cn/s/blog_68034261010156k2.html
对于每种输入,颜色用1,0表示,这样就可以用一个16位2进制数表示。翻转操作就能通过位运算来实现(仰慕)。。。
对于棋盘的每一个状态,枚举十六个格子翻转之后能不能达到全黑全白,如果没有,就对十六个状态再以此类推。这里用BFS的队列实现,并且如果一个新的状态之前已经出现过,则不入队。
code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define MAX 65536
int que[MAX];
int front,rear;
int step[MAX];//每个状态的需要的步数
bool IsVis[MAX];//记录状态是否出现过
using namespace std;
void bfs()
{
int state=0,tmp,i,j;
char ch;
for(i=0;i<4;i++)//将每个状态转换为一个16位2进制数
for(j=0;j<4;j++)
{
state<<=1;
cin>>ch;
if(ch=='b') state++;
}
if(state==0||state==65535)//特殊情况
{
printf("%d\n",0);
return;
}
que[rear++]=state;//bfs,慢慢体会
IsVis[state]=true;
step[state]=0;
while(front<rear)
{
tmp=que[front++];//队头元素出列
state=tmp;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
tmp=state;//每次都要还原成出列的元素
if(i==0) tmp^=1<<(11-4*i-j);//翻转的位置为15-(4*(i+1)+j)
else if(i==3) tmp^=1<<(19-4*i-j);//翻转的位置为15-(4*(i-1)+j)
else
{
tmp^=1<<(11-4*i-j);
tmp^=1<<(19-4*i-j);
}
if(j==0) tmp^=3<<(14-4*i-j);//翻转的位置为15-(4*i+j)、15-(4*i+j+1)
else if(j==3)tmp^=3<<(15-4*i-j);//翻转的位置为15-(4*i+j-1)、15-(4*i+j)
else tmp^=7<<(14-4*i-j);//翻转的位置为15-(4*i+j-1)、15-(4*i+j)、15-(4*i+j+1)
if(tmp==0||tmp==65535)
{
printf("%d\n",step[state]+1);
return;
}
if(!IsVis[tmp])//新状态,入队
{
que[rear++]=tmp;
IsVis[tmp]==true;
step[tmp]=step[state]+1;//步数加1
}
}
}
printf("Impossible\n");
return;
}
int main()
{
bfs();
return 0;
}