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. iBATIS 3 试用手记 - The FUTURE - ITeye技术网站

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  2. hibernate---性能优化, 1+N问题

    session级缓存 保存一个hashmap, 读出来的对象放在里面, 如果读出来50条放50条, 如果另起session原来的50条就被清除.可以手动session.clear清除. 如果同一个se ...

  3. JS中的事件大全

    一般事件  onClick            鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick        鼠标双击事件 onMouseDown        鼠标上的按 ...

  4. List的输出方法

    1.for (int i = 0; i < list.size(); i++) {    System.out.println(list.get(i));} 2.List list = new  ...

  5. 6、手把手教你Extjs5(六)继承自定义一个控件

    Extjs的开发都可以遵循OOP的原则,其对类的封装也很完善了.自定义一个控件最简单的办法就是继承一个已有的控件.根据上一节的需要,我做了一个Button的子类.首先根据目录结构,在app目录下建立一 ...

  6. spring 自动化构建项目

    STS 3.7.0.RELEASE http://spring.io/tools/sts/legacy

  7. ServerSocketChannel

    Java NIO 中的 ServerSocketChannel 是一个可以监听新进来的 TCP 连接的通道, 就像标准 IO 中的 ServerSocket 一样.ServerSocketChanne ...

  8. 打包程序时的证书问题(上传APP就出现Missing iOS Distribution signing indetity for)

    现象: 解决办法: 1.删除本地钥匙串中的这个文件,注意“系统”中的同名文件也必须删除 2.进入http://www.apple.com/certificateauthority/ 下载新的(WWDR ...

  9. iOS 发布流程

    1.登陆苹果开发者中心http://developer.apple.com(99美元账号) 2.进入itunes connect 3.选择Manage Your Apps 4.选择Add New Ap ...

  10. Exception和RuntimeException的区别

    Exception:在程序中必须使用try...catch进行处理. RuntimeException:可以不使用try...catch进行处理,但是如果有异常产生,则异常将由JVM进行处理.