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: 
  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
 
 
思路:用数字来记录棋盘的状态,1代表黑子,0代表白子,可用16位的二进制数表示棋盘状态。则对每个位置的翻子操作结果可以用翻子周围数字之和去异或原数字来表示。
eg:原数字为32如下:  翻子(3,1)即32^(2^3+2^6+2^7+2^11)           结果:
wwww                                        wwww
wwww                                        bwww
wwbw                                        bbbw
wwww                                        bwww
目标数字为0或65535.
#include "cstdio"
#include "algorithm"
#include "queue"
#include "cmath"
#include "cstring" #define inf 0x3f3f3f
using namespace std;
int g[];
char s[][];
bool use[];
typedef struct {
int st,s;
}N;
int bfs(int s){
queue<N>q;
N no,ne;
memset(use, false, sizeof(use));
no.st=s,no.s=;
q.push(no);
use[s]=true;
while (q.size()){
no=q.front();
q.pop();
if(no.st==||no.st==){
return no.s;
}
for(int i=;i<;i++){
ne.st=no.st^g[i];
ne.s=no.s+;
if(use[ne.st]){
continue;
}
if(ne.st==||ne.st==){
return ne.s;
}
use[ne.st]= true;
q.push(ne);
}
}
return -;
}
int main()
{
memset(g,, sizeof(g));
for(int i=;i<;i++){//存储16个翻子操作
int n=-i;
g[i]=pow(,n);
if(n+<=n/*+){
g[i]+=pow(,n+);
}
if(n->=n/*){
g[i]+=pow(,n-);
}
if(n+<=){
g[i]+=pow(,n+);
}
if(n->=){
g[i]+=pow(,n-);
}
}
while (scanf("%s",s[])!=EOF){
for(int i=;i<;i++){
scanf("%s",s[i]);
}
int st=;
for(int i=;i<;i++){//将初始状态转为数字
for(int j=;j<;j++){
st<<=;
if(s[i][j]=='b'){
st++;
}
}
}
int x=bfs(st);
if(x==-){
printf("Impossible\n");
}else{
printf("%d\n",x);
}
}
return ;
}

POJ1753 搜索的更多相关文章

  1. poj1753 bfs+奇偶性减枝//状压搜索

    http://poj.org/problem?id=1753 题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右) ...

  2. SQLSERVER走起微信公众帐号已经开通搜狗微信搜索

    SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...

  3. solr_架构案例【京东站内搜索】(附程序源代码)

    注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...

  4. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  5. HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置

    在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...

  6. bzoj1079--记忆化搜索

    题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...

  7. bzoj3208--记忆化搜索

    题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目.    我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...

  8. Android中通过ActionBar为标题栏添加搜索以及分享视窗

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...

  9. 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

随机推荐

  1. 计算机学院大学生程序设计竞赛(2015’12) 1009 The Magic Tower

    #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...

  2. CentOS6.6部署OpenStack Havana(Nova-Network版)

    CentOS6.4部署OpenStack Havana(Nova-Network版) 一 基本设备介绍 测试环境 CentOS6.4 x64 OpenStack 服务 介绍 计算 (Compute) ...

  3. 控制流之for

    for..in是另外一个循环语句,它在一序列的对象上 递归 即逐一使用队列中的每个项目.我们会在后面的章节中更加详细地学习序列.使用for语句~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  4. [Unity AssetBundle]Asset资源处理

    什么是AssetBundle 在很多类型游戏的制作过程中,开发者都会考虑一个非常重要的问题,即如何在游戏运行过程中对资源进行动态的下载和加载.因此,Unity引擎引入了AssetBundle这一技术来 ...

  5. Ext实现简单计算器

    以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!少帅的博客 使用Ext实现简单计算器,网页版实现 1.页面部分calculator. ...

  6. CodeForces 625D Finals in arithmetic

    神奇的构造题,我的思路比较奇葩.搞了好久,看到WA on 91我绝望了,然后自己造数据,找到了错误,总算是AC了,现在是凌晨0:24分,看到AC之后,感动China! 我写的代码无比的长.....应该 ...

  7. Undefined symbols for architecture i386: "_crc32", referenced from:——crc链接错误

    有时候用别人的框架,你会碰到下面的错误,很是吓人,什么玩意,我怎么看不懂!!! Undefined symbols for architecture i386:  "_RELEASE&quo ...

  8. REST API设计规范

    完全面向资源,API以复数形式表示 路径(Endpoint) http://example.com/libraries //列出所有图书馆 http://example.com/books //列出所 ...

  9. C# 调用外部dll(转)

    C# 调用外部dll   一.      DLL与应用程序 动态链接库(也称为DLL,即为"Dynamic Link Library"的缩写)是Microsoft Windows最 ...

  10. 【java基础】内部类,局部内部类,匿名内部类、静态内部类、接口中的内部类

    内部类: 1.定义在一个类中的内部类,内部类的实例化伴随着外围类所定义的方法来构造,内部类对象有外围类的隐式引用,所以内部类可以直接访问外围类的外围类的域,包括私有的,这是内部类的访问特权,所以比常规 ...