标签:des style class blog c code
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
题目大意:
一个四乘四的棋盘,上面填满了白或黑子,每次可以选择一个子翻转颜色,翻转的同时其上下左右也同时翻转,输出最少次数,使棋盘所有颜色相同。(考虑不可能的情况)
大体思路:
有16个格子,每个格子有黑白两种状态。所以可以用一串二进制来表示状态。
转换成十进制的后,每种状态都可表示为范围为0-65535的唯一整数。
用bfs来进行搜索,第一次出现0或65535(全白或全黑)就停止搜索,返回dis。
使用位运算异或来翻转棋子,0^1=1,1^1=0
1 #include<stdio.h> 2 int vis[70000]= {0},dis[70000];//0-65535 vis为标记是否已经加入队列 dis表示结点所在层数 3 int c=1,queue[65535*2];//c用来存最终次数,queue为bfs的队列 4 int fz(int a,int xy)//通过位运算来翻转棋盘 xy表示翻转的中间子 5 { 6 int tmp[5]= {1,1,1,1,1},i; 7 tmp[0]=tmp[0]<<(xy-1); 8 if (xy==1||xy==2||xy==3||xy==4) tmp[1]=0; 9 else tmp[1]=tmp[1]<<(xy-5); 10 if (xy==16||xy==12||xy==8||xy==4) tmp[2]=0; 11 else tmp[2]=tmp[2]<<(xy); 12 if (xy==13||xy==9||xy==5||xy==1) tmp[3]=0; 13 else tmp[3]=tmp[3]<<(xy-2); 14 if (xy==16||xy==15||xy==14||xy==13) tmp[4]=0; 15 else tmp[4]=tmp[4]<<(xy+3); 16 for (i=0; i<=4; i++) 17 a=a^tmp[i]; 18 return a; 19 } 20 int bfs(int a) 21 { 22 int i,t,front=0,rear=1,tmp=5,ok; 23 dis[a]=0,vis[a]=1; 24 queue[front]=a; 25 while (front<rear) 26 { 27 for (i=1; i<=16; i++) 28 { 29 tmp=fz(queue[front],i); 30 if (vis[tmp]==0) 31 { 32 queue[rear]=tmp; 33 vis[tmp]=1; 34 dis[rear++]=dis[front]+1; 35 if (tmp==0||tmp==65535) 36 { 37 c=dis[front]+1;//找到结果 将步数存入c 38 return 0; 39 } 40 } 41 } 42 front++; 43 } 44 return 1;//队列搜素完毕后 仍未找到 则impossible 45 } 46 int main() 47 { 48 char tmp[20]; 49 int k,a,i,t=0; 50 for (i=1; i<=16; i++) 51 { 52 scanf("%c",&tmp[i]); 53 if (i%4==0&&i!=16) getchar(); 54 } 55 k=1,a=0; 56 for (i=16; i>=1; i--)//处理输入,转换成整数 57 { 58 if (tmp[i]==‘b‘) a+=k; 59 k*=2; 60 } 61 if (a==0||a==65535)//判断初始状态是否为完成态 62 { 63 printf("0\n"); 64 return 0; 65 } 66 t=bfs(a); 67 if (t==1) printf("Impossible\n",c); 68 else printf("%d\n",c); 69 return 0; 70 }
POJ1753——Flip Game,布布扣,bubuko.com
标签:des style class blog c code
原文地址:http://www.cnblogs.com/Enumz/p/3750424.html