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. javascript技巧大全套

    事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture();  event.srcE ...

  2. Android 的Camera架构介绍

    http://java-admin.iteye.com/blog/452464   第一部分 Camera概述Android的Camera包含取景器(viewfinder)和拍摄照片的功能.目前And ...

  3. linux下 mysql 学习(一)

    1.登录mysql [root@test1 local]# mysql  Welcome to the MySQL monitor. Commands end with ; or g. Your My ...

  4. hadoop+海量数据面试题汇总(一)

    hadoop面试题 Q1. Name the most common InputFormats defined in Hadoop? Which one is default ? Following  ...

  5. DateTime.CompareTo方法

    DateTime.CompareTo(value)方法,与一个时间比较,返回整数,含义如下: 值 说明 小于零 此实例早于 value. 零 此实例与 value 相同. 大于零 此实例晚于 valu ...

  6. 对float的理解

    从IE6下的双边距引出 对一个div设置float:left;同时设置了margin-left:100px时在IE6下会出现双边距. 有两种解决办法: 1,推荐办法.加display:inline 2 ...

  7. sqlserver merge into

    create table #ttt(id int,name nvarchar(10));merge into #ttt t using (select 1 as id ,'eee' as name ) ...

  8. Nodejs之发送邮件nodemailer

    nodejs邮件模块nodemailer的使用说明 1.介绍 nodemailer是node的一个发送邮件的组件,其功能相当强大,普通邮件,传送附件,邮件加密等等都能实现,而且操作也十分方便. nod ...

  9. 获取IE浏览器关闭事件

    //关闭浏览器时才会触发此操作,刷新页面不执行 //n 检测鼠标相对于用户屏幕的水平位置 - 网页正文部分左:求出鼠标在当前窗口上的水平位置(参照:当前窗口右上角为0.0坐标) //m 网页正文全文宽 ...

  10. CocoaPods ReactiveCocoa 学习实践一 之 配置环境

    1.安装CocoaPods 1.00.参考 CocoaPods 文档 1.01.是否已安装 which pod 1.1.升级gem命令 sudo gem update --system 1.2.切换C ...