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. JavaScript的语法要点 3 - Calling Context

    上一篇讲了JavaScript的Scope Chain - 每一个函数都有一个scope chain与之关联,scope chain上有第一个对象维护着本地变量作为其属性.另外我们在JavaScrip ...

  2. javascript实现暂停

    <!DOCTYPE HTML><html> <head>  <title> New Document </title>  <meta ...

  3. laravel--belongsTo关联

    1.第一个是要引入的模型类 格式这样 belongsTo 第二个参数是拿自己这个模型表的 哪个字段 去匹配 要关联的qualified表里的哪个ID 默认是拿qualified_id去匹配,前面的是对 ...

  4. 在AWS上安装laravel框架

    博客已经迁移到www.imyzf.com,本站不再更新,请谅解! Laravel是现在非常热门的PHP框架,这几天我试着在亚马逊AWS的服务器上安装Laravel,遇到很多问题,最后还是成功了.我的系 ...

  5. flex打印图片

    <?xml version="1.0" encoding="utf-8"?><s:WindowedApplication xmlns:fx=& ...

  6. WPF xmal绑定数据,当显示数据过长用省略号代替的方法

    有时候会遇到这种情况,用了数据绑定显示的数据太长时,如何让过长的数据显示规定的长度,多余的用省略号代替呢,自己写了个简单的小例子和大家分享一下^_^,我也是学习WPF不久,这是我第一次写博客,有问题还 ...

  7. mac 下 sphinx + mysql + php 实现全文搜索(xampp)(4)php api 解析

    1:function GetLastError()  // 假如报错的话,会输出报错信息 2:function GetLastWarning ()// 输出 警告信息 3:function SetSe ...

  8. Java学习之路:1、HelloWorld

    似乎每种语言都是从HelloWorld开始的,所以,我的第一个java程序,也应该是这样开始的! 1.配置好jdk后,开始编写HelloWorld.java package second;//这个应该 ...

  9. 蜗牛历险记(二) Web框架(上)

    接上篇所说,本篇主要内容是讲述如何使用Autofac来管理整个平台的生命周期(初级). 一.简述 插件式Web开发的同学应该还会记得PreApplicationStartMethod这个Assembl ...

  10. EXTJS4.2 chart 柱状图

    chart 柱状图 Ext.require('Ext.chart.*'); Ext.require(['Ext.Window', 'Ext.fx.target.Sprite', 'Ext.layout ...