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

 #include<stdio.h>
int vis[]= {},dis[];//0-65535 vis为标记是否已经加入队列 dis表示结点所在层数
int c=,queue[*];//c用来存最终次数,queue为bfs的队列
int fz(int a,int xy)//通过位运算来翻转棋盘 xy表示翻转的中间子
{
int tmp[]= {,,,,},i;
tmp[]=tmp[]<<(xy-);
if (xy==||xy==||xy==||xy==) tmp[]=;
else tmp[]=tmp[]<<(xy-);
if (xy==||xy==||xy==||xy==) tmp[]=;
else tmp[]=tmp[]<<(xy);
if (xy==||xy==||xy==||xy==) tmp[]=;
else tmp[]=tmp[]<<(xy-);
if (xy==||xy==||xy==||xy==) tmp[]=;
else tmp[]=tmp[]<<(xy+);
for (i=; i<=; i++)
a=a^tmp[i];
return a;
}
int bfs(int a)
{
int i,t,front=,rear=,tmp=,ok;
dis[a]=,vis[a]=;
queue[front]=a;
while (front<rear)
{
for (i=; i<=; i++)
{
tmp=fz(queue[front],i);
if (vis[tmp]==)
{
queue[rear]=tmp;
vis[tmp]=;
dis[rear++]=dis[front]+;
if (tmp==||tmp==)
{
c=dis[front]+;//找到结果 将步数存入c
return ;
}
}
}
front++;
}
return ;//队列搜素完毕后 仍未找到 则impossible
}
int main()
{
char tmp[];
int k,a,i,t=;
for (i=; i<=; i++)
{
scanf("%c",&tmp[i]);
if (i%==&&i!=) getchar();
}
k=,a=;
for (i=; i>=; i--)//处理输入,转换成整数
{
if (tmp[i]=='b') a+=k;
k*=;
}
if (a==||a==)//判断初始状态是否为完成态
{
printf("0\n");
return ;
}
t=bfs(a);
if (t==) printf("Impossible\n",c);
else printf("%d\n",c);
return ;
}

POJ1753——Flip Game的更多相关文章

  1. poj1753 Flip Game(BFS+位压缩)

    题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...

  2. poj1753,Flip Game,ArrayDeque&lt;Node&gt;

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30449   Accepted: 13232 Descr ...

  3. POJ1753 Flip Game(bfs、枚举)

    链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...

  4. poj1753 Flip Game

    题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...

  5. POJ-1753 Flip Game---二进制枚举子集

    题目链接: https://vjudge.net/problem/POJ-1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...

  6. POJ-1753 Flip Game (BFS+状态压缩)

    Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...

  7. POJ1753 Flip Game(位运算+暴力枚举)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  8. poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推

    题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  9. [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)

    题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...

随机推荐

  1. (POJ 3026) Borg Maze 最小生成树+bfs

    题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...

  2. the first blog: record my coding

    try the code "hello world" #include<iostream> using namespace std; int main() { cout ...

  3. 【转载】学习C++和编程的几个要点

    1.把C++当成一门新的语言学习(和C没啥关系!真的.):2.看<ThinkingIn C++>,不要看<C++变成死相>:3.看<The C++ Programming ...

  4. HLG 1400 汽车比赛

    题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1400 结构体排序+树状数 ...

  5. echo & print

    在实际使用中, print 和 echo 两者的功能几乎是完全一样.可以这么说,凡是有一个可以使用的地方,另一个也可以使用.但是,两者之间也还是一个非常重要的区别:在 echo 函数中,可以同时输出多 ...

  6. 解决Scala异常处理java.lang.OutOfMemoryError: Java heap space error

    需求:百万.千万.4千万级日志对设备进行除重环境:设备内存64G,scala单机版运行shell文件日志:20G 48000000.log4.0G 10000000.log396M 1000000.l ...

  7. log4net基本日志使用笔记[windows application]

    Ref: http://www.cnblogs.com/wangsaiming/archive/2013/01/11/2856253.html http://www.cnblogs.com/zhouf ...

  8. redis学习系列

    redis学习系列 基本看完 最近在看redis的代码,简单记录下自己认为重要的点,自己写比较费时间的,我会把查到的资料贴出来方便查看 淘宝的redis内存分析 http://www.searchtb ...

  9. 一款兼容pc 移动端的tab切换

    利用touchslider.js插件来制作的tab切换,可任意修改很方便~~~ 样式: <style> .box-163css{ width:100%; position:relative ...

  10. hbase on spark

    1.在spark的伪分布式环境下安装HBASE (1)版本:我使用的spark版本是1.3.0,使用的hbase版本是hbase-0.94.16 (2)解压,tar zxvf  hbase-0.94. ...