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. 学习OpenCV——Kalman滤波

    背景: 卡尔曼滤波是一种高效率的递归滤波器(自回归滤波器), 它能够从一系列的不完全及包含噪声的测量中,估计动态系统的状态.卡尔曼滤波的一个典型实例是从一组有限的,包含噪声的,对物体位置的观察序列(可 ...

  2. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5502  The 12th Zhejiang Provincial ...

  3. 2013年各大小IT公司待遇

    2013年各大小IT公司待遇(初版   摘自好网)本人西电硕士,根据今年找工作的情况以及身边同学的汇总,总结各大公司的待遇如下,吐血奉献给各位学弟学妹,公司比较全,你想去的公司不在这里面,基本上是无名 ...

  4. CSS_01_CSS和html结合的方式3、4

    第01步:编写第01个css样式:div.css @charset "utf-8"; /*第01步:定义div:背景色.字体颜色*/ div{ background-color:# ...

  5. Android -- 背景虚化

    1,在项目中我们常有这样的需求,就是在个人中心的时候,当用户登录后,要显示用户登陆后的头像,然后背景是用户头像的虚化 ,接下来就来实现一下这个功能,先看一下效果 2,实现起来也挺简单的,没什么难度 , ...

  6. (转)oracle 查看表所占用的空间大小

    1.查看表所占空间 SELECT   TABLESPACE_NAME,TO_CHAR(SUM(BYTES)/(1024*1024),'999G999D999')   CNT_MB     FROM   ...

  7. ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境(转)

    原文地址:http://blog.csdn.net/shangyuan21/article/details/18153605 我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是M ...

  8. 解决vim无法返回上次的位置

    就是在vim的配置文件 ~/.vimrc 中添加一行这个: au BufReadPost * |if line("'\"") <= line("$&quo ...

  9. [记录]firefox繁体转换成简体的油猴脚本

    // ==UserScript== // @name 繁简转换 // @include *.* // @author yecao // @version 0.1 // @include * // @e ...

  10. [div+css]竖排菜单

             }          #box{              width:120px;              font-size: 12px;              font- ...