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. CRC码计算

    循环冗余校验检错方案 上节介绍的奇偶校验码(PCC)只能校验一位错误,本节所要介绍的循环冗余校验码(CRC)的检错能力更强,可以检出多位错误. 1. CRC校验原理 CRC校验原理看起来比较复杂,好难 ...

  2. java练习篇 求输出最大值

    总结:没有把数据输入.是数组-----把要输入的数据放在数组里.错在这里 import java.util.Scanner; public class shibai { public static v ...

  3. 分布式缓存系统 Memcached slab和item的主要操作

    上节在分析slab内存管理机制时分析Memcached整个Item存储系统的初始化过程slabs_init()函数:分配slabclass数组空间,到最后将各slab划分为各种级别大小的空闲item并 ...

  4. chrome浏览器手动添加印象笔记剪藏插件

    标签(空格分隔): chrome浏览器,日常办公 一直为每次从网页上复制内容之后,还需要再去复制对应的网址,倍感麻烦.之前偶尔这样操作还可以,最近在学习新东西,要保留下来的网页实在太多,而且不利于分类 ...

  5. 23 mysql怎么保证数据不丢失?

    MySQL的wal机制,得到的结论是:只要redo log和binlog 持久化到磁盘,就能确保mysql异常重新启动后,数据是可以恢复的. binlog的写入机制 其实,binlog的写入逻辑比较简 ...

  6. JavaScript笔记——正则表达式

    正则表达式(regular expression)是一个描述字符模式的对象.JavaScript的 RegExp 类 表示正则表达式,而 String 和 RegExp 都定义了使用正则表达式进行强大 ...

  7. 给安卓端调用的短信发送接口demo

    package com.js.ai.modules.pointwall.action; import java.io.IOException; import java.util.HashMap; im ...

  8. Tulpar-web渗透小工具

    首先git clone一下,项目地址:https://github.com/anilbaranyelken/tulpar 下载完后打开文件 然后安装所需的Python模块 安装完成后先看一下帮助 命令 ...

  9. python's thirteenth day for me 迭代器 生成器

    迭代器: for 循环可以循环的就是可迭代对象. 可迭代对象:str, list, tuple, dict, set, range. 迭代器:f1文件句柄. 可迭代协议: 可以被迭代要满足的要求就叫做 ...

  10. 修改Ubuntu默认apt下载源

    修改Ubuntu默认apt下载源 默认下载源很慢,改成阿里的下载速度超快 sudo vim /etc/apt/sources.list 将文件内容替换成 deb http://mirrors.aliy ...