Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 48663   Accepted: 20724

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

 
题目大意:
有一个 4*4 的矩阵,然后现在每个格子有两个颜色,要么是黑色,要么是白色,当你的手按动一个格子的时候,这个格子的上、下、左、右都会
改变原来的颜色,即由黑变白,由白变黑,现在问你的是,经过最少几次翻转之后可以使得这个 4*4 的矩阵变为全是白色的或者是全是黑色的。
 
解题思路:
1)使用DFS
 每格棋子最多只可以翻转1次,
 这是因为只要其中一格重复翻了2次(不论是连续翻动还是不连翻动),
 那么它以及周边的棋子和没翻动时的状态是一致的。
算法停止条件就是当前翻转的棋子个数>=17个 以及 当前棋盘棋子为同一颜色
剪枝条件即为当前翻转棋子的个数>最优解bestx,则进行回溯
由于我们需要考虑的其实就是每一个棋子翻不翻转,所以对于已经考察过的点需要进行剪枝。
 
 #include<iostream>
using namespace std;
int color[];
int a[];
int bestx;
bool checkColor()
{
int a = color[];
for(int i=;i<;i++)
{
if(a != color[i])
{
return ;//存在不同色
}
}
return ;//同色
}
void BackTrack(int t,int pre)
{
if(checkColor() || t>=)//如果同色
{//更新解
if(checkColor())
{
if(t-<bestx)
bestx = t-;
}
return;
} else{
for(int i=pre;i<;i++)
{
if(a[i] == )//没有翻过
{
//进行翻牌
a[i] = ;
color[i] = -color[i];
if(i->=) color[i-] = -color[i-];
if(i+<=) color[i+] = -color[i+];
if(i->= && (i-)%!=) color[i-] = -color[i-];
if(i+<= && (i+)%!=) color[i+] = -color[i+]; if(t<bestx)//剪枝
BackTrack(t+,i+); a[i] = ;//回溯
color[i] = -color[i];
if(i->=) color[i-] = -color[i-];
if(i+<=) color[i+] = -color[i+];
if(i->= && (i-)%!=) color[i-] = -color[i-];
if(i+<= && (i+)%!=) color[i+] = -color[i+];
}
} } }
int main()
{
while(true)
{
bestx=;
for(int i=;i<;i++)
a[i]=;
char c;
for(int i = ;i<;i++)
{
if(i % == && i!=)
{
c = getchar();
}
c = getchar();
if(c == EOF) return ;
if(c == 'b') color[i] = ;
else if(c == 'w') color[i] = ; }
BackTrack(,);
if(bestx == )
cout<<"Impossible"<<endl;
else
cout<<bestx<<endl;
} return ;
}

1753 -- Flip Game的更多相关文章

  1. 枚举 POJ 1753 Flip Game

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

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

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

  3. OpenJudge/Poj 1753 Flip Game

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

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

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

  5. POJ 1753 Flip Game DFS枚举

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

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

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

  7. POJ 1753 Flip Game 状态压缩,暴力 难度:1

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

  8. poj 1753 Flip Game

    点击打开链接 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25674   Accepted: 1109 ...

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

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

  10. 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 ...

随机推荐

  1. java Calendar Date 获取指定日期所在月或年的第一天和最后一天

    一.获取传入日期所在月的第一天 public static Date getFirstDayDateOfMonth(final Date date) { final Calendar cal = Ca ...

  2. github 提交和更新代码

    …or create a new repository on the command line   echo "# flutterPluginsWorks" >> RE ...

  3. 12.SpringMVC核心技术-请求转发和重定向

    默认情况下,跳转到指定的View,使用的是请求转发.也可以显示的进行指出 此时,需在setViewName()  指定的视图前添加 forword: , 且此时的视图不会再与视图解析器中的前缀和后缀进 ...

  4. Spring Cloud(二)服务提供者 Eureka + 服务消费者(rest + Ribbon)

    Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超时 ...

  5. Flutter——GridView组件(网格列表组件)

    GridView组件的常用参数: 名称 类型 说明 scrollDirection Axis 滚动方法 padding EdgeInsetsGeometry 内边距 resolve bool 组件反向 ...

  6. tempfile:临时文件系统对象

    介绍 想要安全的创建名字唯一的临时文件,以防止被试图破坏应用或窃取数据的人猜出,这很有难度.tempfile模块提供了多个函数来安全创建临时文件系统资源.TemporaryFile函数打开并返回一个未 ...

  7. redis—持久化操作

    简介 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(d ...

  8. MySQL中添加、删除约束

    MySQL中6种常见的约束:主键约束(primary key).外键约束(foreign key).非空约束(not null).唯一性约束(unique).默认值约束(defualt).自增约束(a ...

  9. PAT Advanced 1154 Vertex Coloring (25 分)

    A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...

  10. Ubuntu系统---EasyECD安装记录

    说明:因解决Ubuntu花屏和频繁死机的问题(后来证实本人的电脑显卡驱动有问题),手残毁坏了系统,需重装.之前从未装过系统,经过三天,反复折腾装了近十次的系统,现总结如下. 第一步:Windows 系 ...