题目:

Flip game is played on a rectangular 4×4 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:

  1. Choose any one of the 16 pieces.
  2. 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

链接:http://acm.sdibt.edu.cn:8080/judge/contest/view.action?cid=353#problem/A
题目大意:本题大意是翻转棋,一个四*四的翻转棋,如果翻动一个棋子,则跟他相关联的上下左右都要翻转。则,翻转两次相当于不翻转。就是用以个BFS+位运算+状态压缩。
代码如下:
#include
#include
typedef struct node
{
int box;
int time;
int max;
}NODE;
turn(int bo,int positio)
{
int k,t;
t=1<=0)
{
k=t>>4;
bo=bo^k;
}
if(positio%4!=0)
{
k=t>>1;
bo=bo^k;
}
if(positio%4!=3)
{
k=t<<1; bo=bo^k; } return bo; } NODE a[70000]; int main() { int i,position,mar; long k,j; char bcd[10]; a[0].box=0; a[0].time=0; for(i=0;i<4;i++) { scanf("%s",bcd); for(j=0;j<4;j++) { if(bcd[j]=='b') { position=1<<(j+i*4); a[0].box=a[0].box+position; } } } if(a[0].box==0||a[0].box==65535) printf("0\n"); else { k=0; j=1; mar=0; a[0].max=-1; while(1) { for(i=a[k].max+1;i<16;i++) { a[j].box=turn(a[k].box,i); a[j].time=a[k].time+1; a[j].max=i; if(a[j].box==0||a[j].box==65535) { mar=1; break; } j++; } k++; if(a[j-1].time==16) break; if(mar==1) break; } if(mar==1) printf("%d\n",a[j].time); else printf("Impossible\n"); } return 0; }

发表评论

电子邮件地址不会被公开。

Post Navigation