博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1753——Flip Game
阅读量:5162 次
发布时间:2019-06-13

本文共 2973 字,大约阅读时间需要 9 分钟。

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
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
=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 }

 

 

转载于:https://www.cnblogs.com/Enumz/p/3750424.html

你可能感兴趣的文章
4.AE中的缩放,书签
查看>>
CVE-2014-6321 && MS14-066 Microsoft Schannel Remote Code Execution Vulnerability Analysis
查看>>
给一次重新选择的机会_您还会选择程序员吗?
查看>>
Mysql MHA高可用集群架构
查看>>
心急的C小加
查看>>
编译原理 First,Follow,select集求法
查看>>
java 浅拷贝和深拷贝
查看>>
vue实例中中data属性三种写法
查看>>
uva1636 - Headshot(条件概率)
查看>>
iOS开发 runtime实现原理以及实际开发中的应用
查看>>
488 - Triangle Wave
查看>>
BZOJ2437 NOI2011兔兔与蛋蛋(二分图匹配+博弈)
查看>>
android 学习资源网址
查看>>
shell基础
查看>>
2018.1.15
查看>>
[集合DP] UVA 10651 Pebble Solitaire
查看>>
测试成长之路
查看>>
jquary常见问题总结
查看>>
Javascript中eval解析的json的几种用法
查看>>
Dashbroad 展现数据
查看>>