Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28805   Accepted: 12461

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

 //164K    125MS    C++    1211B    2014-04-26 11:02:12
/* 题意:
问最少翻几步可以使棋盘棋子一样,不可能就输出Impossible 搜索枚举:
枚举全部状态,每个位置的棋子有翻或不翻两种状态,枚举全部状态。
注意一个棋子翻两次则和没翻一样,所以一种有2^16种情况,用dfs枚举全部状态。 */
#include<stdio.h>
#include<string.h>
int g[][];
int flag;
int judge(int tg[][]) //判断
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(g[i][j]!=g[][]) return ;
return ;
}
void flip(int i,int j) //翻棋
{
g[i][j]^=;
g[i-][j]^=;
g[i+][j]^=;
g[i][j-]^=;
g[i][j+]^=;
}
void dfs(int x,int y,int cnt,int n)
{
if(cnt==n){
flag=judge(g);
return;
}
if(flag || y>) return;
flip(x,y);
if(x<) dfs(x+,y,cnt+,n);
else dfs(,y+,cnt+,n);
flip(x,y);
if(x<) dfs(x+,y,cnt,n);
else dfs(,y+,cnt,n);
}
int main(void)
{
char c[];
while(scanf("%s",c)!=EOF)
{
memset(g,,sizeof(g));
for(int i=;i<;i++) g[][i+]=c[i]=='b'?:;
for(int i=;i<;i++){
scanf("%s",c);
for(int j=;j<;j++)
g[i+][j+]=c[j]=='b'?:;
}
flag=;
int cnt=-;
for(int i=;i<;i++){
dfs(,,,i);
if(flag){
cnt=i;break;
}
}
if(cnt==-) puts("Impossible");
else printf("%d\n",cnt);
}
return ;
}
/* bwwb
bbwb
bwwb
bwww bwbw
bwww
wwwb
wwwb bwww
wwww
wwww
wwww */

poj 1753 Flip Game (dfs)的更多相关文章

  1. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

  2. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  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(高斯消元+状压枚举)

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

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

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  7. OpenJudge/Poj 1753 Flip Game

    1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...

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

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

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

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

随机推荐

  1. Mysql升级过程的问题

    升级安装5.6版本mysql linux环境下的yum默认mysql版本是5.1的,由于项目需要保存表情等4个字节的数据,版本受限,需要升级到5.6版本支持utf8mb4格式的编码. 升级过程大概就是 ...

  2. 【bind服务简单发布及优化部署】

    主DNS 1:安装bind服务包 2:vim  /etc/named.conf区域解析控制文件 3:vim /etc/named.rfc1912.zones解析方向文件 4:vim var/named ...

  3. Hadoop(10)-HDFS的DataNode详解

    1.DataNode工作机制 1)一个数据块在DataNode上以文件形式存储在磁盘上,包括两个文件,一个是数据本身,一个是元数据包括数据块的长度,块数据的校验和,以及时间戳. 2)DataNode启 ...

  4. python学习之字符串常用方法和格式化字符串

    Python中的字符串同样适用标准的序列操作(索引,分片,乘法,成员判断,求长度,取最小值和最大值),但因为字符串是不可变的,因此字符串不支持分片赋值. s='http://www.baidu.com ...

  5. 第一次学习tornado小练习

    内容 这次是python的一个web框架,tornado,这个web框架在python的几个web框架中一个比较简单的web框架,刚开始接触python的时候就知道python有两个比较常用的web框 ...

  6. (数据科学学习手札22)主成分分析法在Python与R中的基本功能实现

    上一篇中我们详细介绍推导了主成分分析法的原理,并基于Python通过自编函数实现了挑选主成分的过程,而在Python与R中都有比较成熟的主成分分析函数,本篇我们就对这些方法进行介绍: R 在R的基础函 ...

  7. R语言学习笔记(二): 类与泛型函数

    类 大多数R对象都是基于S3类(来源于第三代S语言),例如直方图函数hist()输出是一个包含多个组件的列表,它还有一个属性(attribute),用来指定列表的类,即histogram类. 泛型函数 ...

  8. 販売管理(SD)

    SD(販売管理)系のSAP DBテーブル. 随時更新していきます. [得意先マスタ]KNA1: 一般データ KNB1: 会計データ KNBK: 銀行データ KNVV: 販売データ KNVP: 取引先機 ...

  9. SSM框架学习思维导图

    SSM框架学习思维导图 2017年08月11日 20:17:28 阅读数:1141 放上前段时间学习SSM框架以及Spring.SpringMVC.MyBatis的学习结果,输出思维导图一共四幅图.这 ...

  10. ExtJs4.1目录结构介绍和使用说明[转]

    一.在做ExtJs开发之前首先要到网站上下载ExtJs的开发包,我用的最新版本是4.1.1.此版本相对于之前的版本目录结构发生了一些变化,没有了adapter目录, 目录结构如下 文件/文件夹名的作用 ...