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的方格,每个方格中放一粒棋子,这个棋子一面是白色,一面是黑色。游戏规则为每次任选16颗中的一颗,把选中的这颗以及它四周的棋子一并反过来,当所有的棋子都是同一个颜色朝上时,游戏就完成了。现在给定一个初始状态,要求输出能够完成游戏所需翻转的最小次数,如果初始状态已经达到要求输出0。如果不可能完成游戏,输出Impossible。

分析:

主要思想:

1、如果用一个4*4的数组存储每一种状态,不但存储空间很大,而且在穷举状态时也不方便记录。因为每一颗棋子都只有两种状态,所以可以用二进制0和1表示每一个棋子的状态,则棋盘的状态就可以用一个16位的整数唯一标识。而翻转的操作也可以通过通过位操作来完成。显然当棋盘状态id为0(全白)或65535(全黑)时,游戏结束。

2、对于棋盘的每一个状态,都有十六种操作,首先要判断这十六种操作之后是否有完成的情况,如果没有,则再对这十六种操作的结果分别再进行上述操作,显然这里就要用到队列来存储了。而且在翻转的过程中有可能会回到之前的某种状态,而这种重复的状态是不应该再次入队的,所以维护 Visit[i]数组来判断 id==i 的状态之前是否已经出现过,如果不是才将其入队。如果游戏无法完成,状态必定会形成循环,由于重复状态不会再次入队,所以最后的队列一定会是空队列。

3、由于0^1=1,1^1=0,所以翻转的操作可以通过异或操作来完成,而翻转的位置可以通过移位来确定。

注意:有16个棋子,所以用2进制状态压缩,用^=进行翻棋子的操作, 当然要判断一下在不在棋盘内,接着就是枚举每个状态16个棋子翻的状态,进行判断,如果超了16步就是不可能了,因为相当于一个棋子翻过去又翻回来,没有意义了。

第二个位运算的题,还得继续加油啊

code:

#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
using namespace std;
const int max_v=(<<)-;
int vis[max_v+];
int step;
queue<int> q;
int change(unsigned int x,int i,int j)//记住x是无符号数 执行翻转当前棋子和当前棋子周围四个棋子的操作
{
int k;
k=(<<(i*+j));//当前位置
x^=k;//翻转
if(i->=)
{
k=(<<(*(i-)+j));//当前位置的上面一个位置
x^=k;//翻转棋子
}
if(i+<)
{
k=(<<(*(i+)+j));//下
x^=k;//翻转
}
if(j+<)
{
k=(<<(*i+j+));//右
x^=k;//翻转
}
if(j->=)
{
k=(<<(*i+j-));//左
x^=k;//翻转
}
return x;
}
int bfs(int state)//state代表当前棋盘的状态
{
step=;
while(!q.empty()) q.pop();
q.push(state); while(!q.empty())
{
int t=q.size();
for(int i=;i<t;i++)
{
int k=q.front();
q.pop(); if(k==||k==max_v||step>)//全黑 或 全白 或 步数大于16(因为相当于一个棋子翻过去又翻回来,没有意义了。)
return step;
for(int x=;x<;x++)//16个点,对应16种操作
for(int y=;y<;y++)
{
int w;
w=change(k,x,y);//翻转棋盘
if(!vis[w])//判断此状态的棋盘有没有算过
q.push(w);
vis[w]=;//标记
}
}
step++;
}
}
int main()
{
int state=;
char s[];
int k=;
for(int i=;i<;i++)
{
scanf("%s",s);
for(int j=;j<;j++)
{
if(s[j]=='b')
state+=(<<k);
k++;
}
}
int ans=bfs(state);
if(ans>)
cout<<"Impossible"<<endl;
else
cout<<ans<<endl;
return ;
}

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 状态压缩,暴力 难度:1

    Flip Game Time Limit: 1000MS  Memory Limit: 65536K  Total Submissions: 4863  Accepted: 1983 Descript ...

  3. POJ - 1324 Holedox Moving (状态压缩+BFS/A*)

    题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...

  4. POJ 3411 Paid Roads (状态压缩+BFS)

    题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...

  5. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

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

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

  7. 枚举 POJ 1753 Flip Game

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

  8. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. Vijos 1206 CoVH之再破难关 [BFS] [位运算]

    1.题意:一个由01组成的4*4的矩阵,可以实现相邻元素交换位置的操作,给出初试状态和目标状态,试求最少操作数的方案: 2.输入输出:输入给出初试矩阵和目标矩阵:要求输出最小操作的次数: 3.分析:输 ...

随机推荐

  1. 第八章.Java集合

    Java集合类是一种特别有用的工具类,可用于存储数量不等的对象.Java集合大致可分为Set.List.Queue和Map四种体系 Set代表无序.不可重复的集合 List代表有序.重复的集合 Map ...

  2. SASS和SCSS标签详解与scoped局部和全局的使用

    首先,学会使用sass: 1.先下载和安装node-sass和一些加载器 $ cnpm install sass-loader node-sass vue-style-loader --D 2.配置w ...

  3. vue的事件

    vue的事件: vue事件简写: vue中事件是 v-on:click=' show()'    但是我嫌弃它写太长每次都要 v-on: 事件 vue中就有事件简写   @click='show()' ...

  4. 001Spring 定时任务 Scheduled

    01.@Scheduled注解参数 @Scheduled支持fixedRate.fixedDelay.cron表达式参数.其中,fixedRate和fixedDelay没有区别,都是启动时执行1次,每 ...

  5. Tesseract-OCR-03-图片文字识别

    Tesseract-OCR-03-图片文字识别 本篇介绍使用 Tesseract-OCR 做图片文字识别,识别手写文字的时候,正确率能达到 90%,当训练后正确率是极高的.这里介绍的图片文字识别,可以 ...

  6. 139.00.006 Git学习-标签管理Tag

    @(139 - Environment Settings | 环境配置) 一.Why 发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取 ...

  7. javascript实现的动态添加表单元素input,button等(appendChild)

    写一个小系统时,需要动态添加表单元素,按自己的实现方法写了这篇教程! 我想各位在很多网站上都看到过类似的效果! 1.先用document.createElement方法创建一个input元素! 代码如 ...

  8. DEV控件之ChartControl用法 z

    一.总体概述 这个控件包含3层,最外面的chartControl层.中间的XYDiagram层.最里面的Series层.功能非常强大,但同时使用起来也相对复杂,需要各个层之间相互协调设置才能达到自己想 ...

  9. C#中的多线程 - 同步基础 z

    原文:http://www.albahari.com/threading/part2.aspx 专题:C#中的多线程 1同步概要Permalink 在第 1 部分:基础知识中,我们描述了如何在线程上启 ...

  10. .net 面向对象程序设计深入](3)UML

    1.活动图简介 定义:是阐明了业务用例实现的工作流程. 业务工作流程说明了业务为向所服务的业务主角提供其所需的价值而必须完成的工作. 业务用例由一系列活动组成,它们共同为业务主角生成某些工件. 工作流 ...