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

题意:有一个4*4的棋盘,给你它的初始状态,每次可以翻动一个棋子,但翻动的同时也必须翻动它上下左右的另外四个棋子,求将棋盘翻成全黑和全白的最小次数

题解:这题有各种做法,dfs,高斯消元……
然而我比较懒,喜欢直线型思维,所以就打了个状压枚举状态的暴力
很显然,把同一个棋子翻动两次与不翻动没有任何区别
所以从不翻棋子到十六个全翻总共也就是2^16次
完全可以枚举!
然后就糊出来了!
代码如下:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int sta,ans=,tmp,flag,flag1,a[][],b[][],dx[]= {,,-,},dy[]= {,,,-}; void flip(int sta)
{
int x=,y=;
tmp=;
flag=;
flag1=;
while(sta)
{
if(sta%==)
{
tmp++;
b[x][y]=!b[x][y];
for(int i=; i<=; i++)
{
if(x+dx[i]<=&&x+dx[i]>&&y+dy[i]<=&&dy[i]+y>)
{
b[x+dx[i]][y+dy[i]]=!b[x+dx[i]][y+dy[i]];
}
}
}
y++;
if(y>)
{
x++;
y=;
}
sta>>=;
}
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(b[i][j])
{
flag=;
}
}
}
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(!b[i][j])
{
flag1=;
}
}
}
if(!flag||!flag1)
{
ans=min(ans,tmp);
}
} int main()
{
char c[];
for(int i=; i<=; i++)
{
scanf("%s",c+);
for(int j=; j<=; j++)
{
if(c[j]=='b')
{
a[i][j]=;
}
else
{
a[i][j]=;
}
}
}
for(int sta=; sta<=(<<)-; sta++)
{
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
b[i][j]=a[i][j];
}
}
flip(sta);
}
if(ans==)
{
puts("Impossible");
return ;
}
printf("%d\n",ans);
return ;
}



POJ1753 Flip Game(位运算+暴力枚举)的更多相关文章

  1. POJ 2531 Network Saboteur 位运算子集枚举

    题目: http://poj.org/problem?id=2531 这个题虽然是个最大割问题,但是分到dfs里了,因为节点数较少.. 我试着位运算枚举了一下,开始超时了,剪了下枝,1079MS过了. ...

  2. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  3. POJ1753 Flip Game(bfs、枚举)

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

  4. C#枚举中的位运算权限分配浅谈

    常用的位运算主要有与(&), 或(|)和非(~), 比如: 1 & 0 = 0, 1 | 0 = 1, ~1 = 0 在设计权限时, 我们可以把权限管理操作转换为C#位运算来处理. 第 ...

  5. C#位运算讲解与示例2

    在C#中可以对整型运算对象按位进行逻辑运算.按位进行逻辑运算的意义是:依次取被运算对象的每个位,进行逻辑运算,每个位的逻辑运算结果是结果值的每个位.C#支持的位逻辑运算符如表2.9所示. 运算符号 意 ...

  6. 【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)

    这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...

  7. POJ 1753 位运算+枚举

    题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...

  8. C#学习笔记-----C#枚举中的位运算权限分配

    一.基础知识 什么是位运算? 用二进制来计算,1&2:这就是位运算,其实它是将0001与0010做位预算   得到的结果是 0011,也就是3  2.位预算有多少种?(我们就将几种我们权限中会 ...

  9. POJ 2436 二进制枚举+位运算

    题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...

随机推荐

  1. Git&Repo 命令大全 ***

    首先理解几个基本概念: origin:默认远程版本库: master:默认开发分支: 查看本地更新状态: git status jiangzhaowei@ubuntu$ git status # On ...

  2. Struts2接受页面传值过程中出现input的问题

    其实我在使用Struts2的时候,遇到要求返回input的时候不算少.一般我们在使用Struts2的时候,都会返回SUCCESS/ERROR,或者是NONE以到Strtuts的配置文件中再进行相应的处 ...

  3. iOS平台下闪退原因汇总(一):"Ran out of trampolines of type 0/1/2" 运行时间错误

    "Ran out of trampolines of type 0/1/2" 运行时间错误通常出现在使用大量递归泛型时.要看到这个错误需要连接着设备直接将项目build到设备里运行 ...

  4. 什么是Ajax和JSON,它们的优缺点

    什么是Ajax??? 术语Ajax用来描述一组技术,它使浏览器可以为用户提供更为自然的浏览体验. Ajax它是“Asynchronous JavaScript + XML的简写” 定义Ajax: Aj ...

  5. oracle中,约束、表名、Index等的名称长度限制最大只能30个字符

    oracle中,约束.表名.Index等的名称长度限制最大只能30个字符

  6. 强大的HTML5开发工具推荐

    HTML5被看做是Web开发者创建流行Web应用的利器,增加了对视频和Canvas 2D的支持.HTML5的诞生还让人们重新审视浏览器专用多媒体插件的未来,如Adobe的Flash和微软的Silver ...

  7. POJ1657

    1.题目链接地址 http://poj.org/problem?id=1657 2.源代码 #include<iostream> using namespace std; int main ...

  8. 【HDU 6031]】 Innumerable Ancestors

    题意 有一棵有n个结点的树,这里有m个询问,每个询问给出两个非空的结点集合A和B,有些结点可能同时在这两个集合当中.你需要从A和B中分别选择一个节点x和y(可以是同一个结点)你的目标是使LCA(x,y ...

  9. QPS、PV和需要部署机器数量计算公式

    QPS:Queries Per Second意思是“每秒查询率”,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. TPS是 Transactions ...

  10. springBoot集成 quartz动态定时任务

    项目中需要用到定时任务,考虑了下java方面定时任务无非就三种: 用Java自带的timer类.稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和 ...