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.
Input
Thefirst line is a positive integer m , indicating the number of test cases . Then m test cases .Each case 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
1
bwwb
bbwb
bwwb
bwww
Sample Output
4
package OJ;
import java.io.IOException;
import java.util.Scanner;
public class P6_temp { //Flip Game
public static void main(String[] args) throws IOException {
int MAX = 65536;
int[] que = new int[MAX*2];
int front = 0;
int rear = 0;
boolean[] isVis = new boolean[MAX]; //记录状态是否已经出现过
int[] step = new int[MAX]; //记录到达id==i的状态所需翻转次数
char color;
int id=0;
int tmp = 0;
int i,j;
int inputNum = 0;
Scanner in = new Scanner(System.in);
inputNum = in.nextInt();
for(int k=0; k<inputNum; k++){
for(i=0;i<4;i++){
String colorS = in.next();
for(j=0;j<4;j++){ //输入初始状态并转换为id
color = colorS.charAt(j);
// 先左移,后加1,保证加的1在最后一位,便于移位操作
id<<=1;
if(color=='b')
id+=1;
}
}
}
if(id==0||id==65535) { //如果初始状态已经满足要求直接输出
System.out.println(0);
System.exit(0);
}
else {
que[rear++]=id; //将初始状态id入队
isVis[id]=true;
step[id]=0; //到达初始状态所需次数为0
while(front<rear){ //如果队列不为空,继续操作
tmp=que[front++]; //将队头元素出队
id=tmp;
for(i=0;i<4;i++)
for(j=0;j<4;j++){
tmp=id; //需要遍历队头元素的16种操作,每次都要还原tmp
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){
// cout<<step[id]+1<<endl;
// return 0;
int sp = step[id] + 1;
System.out.println(sp);
System.exit(0);
}
if(!isVis[tmp]){ //如果是新的状态,入队
que[rear++]=tmp;
isVis[tmp]=true;
step[tmp]=step[id]+1; //当前次数=到达队头元素所需次数+1
}
}
}
System.out.println("Impossible");
System.exit(0);
}
}
}