Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30669   Accepted: 13345

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

Source

我不得不说位运算是一个强大的东西,之前从来没怎么用过。没想到这么一道本来没有头绪的题,位运算一优化成了一道裸一维的BFS,说这个题目。大意是一个翻牌游戏。牌有黑白两面,当翻到全部的牌都是黑或都是白时,满足状态,输出最小操作步数。每一个位置的牌的都能够翻,但如过翻了当前这张牌。它的相邻的位置的牌都要翻过来。对于全部的状态。一共同拥有2^16种(4*4的图)
而对于翻牌操作,如果当前状态为s 想要翻第i个位置的牌。仅仅要s^=1<<i;就可以。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
bool vis[70000];
char ma[20];
typedef struct node
{
int state,step;
};
void bfs(int s)
{
queue <node> Q;
node t;t.state=s;t.step=0;
vis[s]=1;
Q.push(t);
while(!Q.empty())
{
node v=Q.front();Q.pop();
if(v.state==0||v.state==65535)
{
cout<<v.step<<endl;
return ;
}
for(int i=0;i<16;i++)//枚举16个点
{
int tem=v.state;
tem^=1<<i;//自身翻转
if(i-4>=0) tem^=1<<(i-4);//上
if(i<12) tem^=1<<(i+4);//下
if(i%4!=0) tem^=1<<(i-1);//左
if((i+1)%4!=0) tem^=1<<(i+1);//右
if(!vis[tem])
{
vis[tem]=1;
t.state=tem;
t.step=v.step+1;
Q.push(t);
}
}
}
puts("Impossible");
}
int main()
{
int i=0,s=0;
memset(vis,0,sizeof(vis));
while(i<16)
{
cin>>ma[i];
if(ma[i]=='b')
s+=1<<i;
i++;
}
bfs(s);
return 0;
}

POj 1753--Flip Game(位运算+BFS)的更多相关文章

  1. POJ 1753 Flip Game(状态压缩+BFS)

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

  2. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

  3. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  4. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  5. POJ 1753 Flip Game(二进制枚举)

    题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...

  6. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

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

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

  8. POJ - 1753 Flip Game(状压枚举)

    https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...

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

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

随机推荐

  1. JSP,servlet和数据库之间传值出现乱码的问题

     近期困扰我非常久的一个问题最终攻克了,为他我头疼了好几天,问题是JSP通过servlet向数据库传值,查询显示在页面的时候出现了乱码,原先我数据库中有两行带有中文的数据,查询的时候倒是没有出现乱 ...

  2. 用sed替换文件中的空格

    请教sed 替换问题 请教各位如何替换多个空格为一个字符,如一个文件中间隔符有是一个空格,有的地方是多个空格,想全部用“|”替换,如何处理,请指教 请教sed 替换问题 [code]sed '/ \+ ...

  3. [转载][转]修改/proc目录下的参数优化网络性能

    原文地址:[转]修改/proc目录下的参数优化网络性能作者:雪人 网络优化 注意: 1. 参数值带有速度(rate)的参数不能在loopback接口上工作. 2.因为内核是以HZ为单位的内部时钟来定义 ...

  4. 如何判断Android手机当前是否联网?

    如果拟开发一个网络应用的程序,首先考虑是否接入网络,在Android手机中判断是否联网可以通过 ConnectivityManager 类的isAvailable()方法判断, 首先获取网络通讯类的实 ...

  5. AOE网与关键路径简介

    前面我们说过的拓扑排序主要是为解决一个工程能否顺序进行的问题,但有时我们还需要解决工程完成需要的最短时间问题.如果我们要对一个流程图获得最短时间,就必须要分析它们的拓扑关系,并且找到当中最关键的流程, ...

  6. Linux下C结构体初始化

    1.前言 今天在公司看一同事写的代码,代码中用到了struct,初始化一个struct用的是乱序格式,如下代码所示: typedef struct _data_t { int a; int b; }d ...

  7. 解决svchost占用内存过高问题

    摘抄自:https://jingyan.baidu.com/article/d169e1867cea7e436611d801.html svchost占用内存过高,会导致内存100%电脑卡住,CPU温 ...

  8. 开发Yii2过滤器并通过behaviors()行为调用(转)

    在Yii2的几乎每个controller中,我们都会看到一个函数behaviors(),通常,我们用这个函数来配置控制器的权限,例如:public function behaviors()    {  ...

  9. 【C语言】给一组组数,仅仅有两个数仅仅出现了一次,其它全部数都是成对出现的,找出这两个数。

    //给⼀组组数,仅仅有两个数仅仅出现了一次.其它全部数都是成对出现的,找出这两个数. #include <stdio.h> int find_one_pos(int num) //找一个为 ...

  10. 【Android】11.3 屏幕旋转和场景变换过程中GridView的呈现

    分类:C#.Android.VS2015: 创建日期:2016-02-21 一.简介 实际上,对于布局文件中的View来说,大多数情况下,Android都会自动保存这些状态,并不需要我们都去处理它.这 ...