Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 33519   Accepted: 14642

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:

  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
 
/*
枚举第一行的情况(用二进制),进行反转,然后反转后面的行以保正前面的行符合要求
最后看最后一行是不是符合要求
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
int a[5][5],b[5][5];
int Arr[5];
char s[5][5]; void Trans()//转化成数字
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
a[i][j]=s[i][j]=='b'?1:0;
}
}
}
void tran(int n)//二进制转化
{
memset(Arr,0,sizeof(Arr));
int top=3;
while(n)
{
Arr[top--]=n&1;
n=n>>1;
}
}
void Creat()
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
b[i][j]=a[i][j];
}
}
}
void change(int i,int j)//反转
{
b[i][j]=b[i][j]==0?1:0;
if(j>0)
{
b[i][j-1]=b[i][j-1]==0?1:0;
}
if(j<3)
{
b[i][j+1]=b[i][j+1]==0?1:0;
}
if(i<3)
{
b[i+1][j]=b[i+1][j]==0?1:0;
}
if(i>0)
{
b[i-1][j]=b[i-1][j]==0?1:0;
}
}
int main()
{
int sum;
int Max=1000;
for(int i=0; i<4; i++)
{
scanf("%s",s[i]);
}
Trans();
for(int i=0; i<=15; i++)
{
tran(i);
for(int j=0; j<2; j++)
{
Creat();
sum=0;
for(int k=0; k<4; k++)
{
if(Arr[k])
{
change(0,k);
sum++;
}
}
for(int k=1; k<4; k++)
{
for(int kk=0; kk<4; kk++)
{
if(b[k-1][kk]!=j)
{
change(k,kk);
sum++;
}
}
}
bool flag=true;
for(int k=0; k<4; k++)
{
if(b[3][k]!=j)
{
flag=false;
break;
}
}
if(flag)
{
if(Max>sum)
{
Max=sum;
}
if(Max==0)
{
break;
}
}
else//如果不能反转成则就不可能反转成功
{
break;
}
}
}
if(Max!=1000)
{
printf("%d\n",Max);
}
else
{
printf("Impossible\n");
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Flip Game 分类: POJ 2015-06-15 14:59 22人阅读 评论(0) 收藏的更多相关文章

  1. 【solr专题之一】Solr快速入门 分类: H4_SOLR/LUCENCE 2014-07-02 14:59 2403人阅读 评论(0) 收藏

    一.Solr学习相关资料 1.官方材料 (1)快速入门:http://lucene.apache.org/solr/4_9_0/tutorial.html,以自带的example项目快速介绍发Solr ...

  2. A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏

    A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...

  3. iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  4. iOS中UITextField 使用全面解析 分类: ios技术 2015-04-10 14:37 153人阅读 评论(0) 收藏

    //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 13 ...

  5. Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. printf "%.*s" 分类: 小细节 2015-07-04 14:36 2人阅读 评论(0) 收藏

    ref : http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html 小数点.后"*"表示输出位数,具体的数据来自 ...

  7. 读取Webpage表中的内容 分类: H3_NUTCH 2015-02-10 14:59 418人阅读 评论(0) 收藏

    nutch将从网页中抓取到的信息放入hbase数据库中,默认情况下表名为$crawlId_webpage,但表中的内容以16进制进行表示,直接scan或者通过Java API进行读取均只能读取到16进 ...

  8. IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏

    IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...

  9. C++实现不能被继承的类——终结类 分类: C/C++ 2015-04-06 14:48 64人阅读 评论(0) 收藏

    1.       问题 C++如何实现不能被继承的类,即终结类.Java中有final关键字修饰,C#中有sealed关键字修饰,而C++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...

随机推荐

  1. CentOS6.5 安装HAProxy 1.5.20

    [在CentOS下安装haproxy] sudo yum install haproxy 编辑配置文件 vim /etc/haproxy/haproxy.cfg #------------------ ...

  2. ssh两台机器建立信任关系无密码登陆

    在建立信任关系之前先看看基于公钥.私钥的加密和认证. 私钥签名过程 消息-->[私钥]-->签名-->[公钥]-->认证 私钥数字签名,公钥验证 Alice生成公钥和私钥,并将 ...

  3. Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  4. Python高频技巧总结[基础篇]

    0. 概要说明 python应用最多的场景还是web快速开发.爬虫.自动化运维:简单网站.自动Fuzz脚本.收发邮件脚本.简单验证码识别脚本. 爬虫在开发过程中也有很多复用的过程,这里总结一下,以后也 ...

  5. mysql错误日志路径

    造成该问题的原因有很多,单纯的百度google这个问题,很难找到正确的解决办法,要对症下药测才能解决:1.查看具体错误信息:  按:计算机管理——>系统工具——>事件查看器——>Wi ...

  6. 封装page分页类

    类: <?php //分页工具类 class Page{ /*         * 获取分页字符串         * @param1 string $uri,分页要请求的脚本url       ...

  7. Android -- 自定义View小Demo(一)

    1,现在要实现下图的简单效果,很简单  ,就是使用paint在canvas上绘制5中不同颜色的圆圈,效果图如下: 这是绘制基本图形一种最简单的方法,下面是它的代码 ,注释写的很详细,也就不去讲解了 M ...

  8. Mysql自定义函数总结

    存储函数 创建存储函数,需要使用CREATE FUNCTION语句,基本语法如下: CREATE FUNCTION func_name([func_parameter]) RETURNS TYPE [ ...

  9. 5. 星际争霸之php设计模式--抽象工厂模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  10. 图像处理之常用颜色RGB、灰度值

    128/0/0       深红         255/0/0       红           255/0/255     粉红        255/153/204 玫瑰红       153 ...