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. LEANGOO用户设置

    转自:https://www.leangoo.com/leangoo_guide/leangoo_guide_kanban_user.html#toggle-id-7 1. 点击屏幕右上角头像或用户名 ...

  2. 5.移动端自动化测试-小知识 import和from...import的区别

    一.import   1 import导入的时,需要使用模块名的限定. 举个例子,我们首先创建一个md.py文件,里面有一个函数 2 然后在1.py文件中引用这个函数. 注意,我们需要使用md.的方式 ...

  3. host缓存,浏览器缓存---解决host缓存带来的伤

    1.缓存 缓存,对应工程师来讲简直太熟悉了,太方便了,省略到资源或数据的获取方式,直接缓存到离用户访问最快的地方,也降低服务器的压力,比如: (1)静态文件获取 服务器->cdn->本地磁 ...

  4. vue项目中图片预览旋转功能

    最近项目中需要在图片预览时,可以旋转图片预览,在网上找了下,发现有一款功能强大的图片组件:viewerjs. git-hup: https://github.com/fengyuanchen/view ...

  5. 我理解的epoll(三)多线程模式下的ET

    ET模式下,需要循环从缓存中读取,直到返回EAGAIN没有数据可读后,一个被通知的事件才算结束.如果还读取过程中,同一个连接又有新的事件到来,触发其他线程处理同一个socket,就乱了.EPOLL_O ...

  6. cv2.videocapture()失败,无法读取视频

    原因:缺少ffmpeg的支持 解决:一般opencv3.3版本及以上支持ffmpeg,实验4.1.0成功 pip install opencv-python pip install opencv-co ...

  7. PAT Basic 1082 射击比赛 (20 分)

    本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军:谁差得最远,谁就是菜鸟.本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟.我们假设靶心在原点(0,0). 输入 ...

  8. metal docs--Synchronization&memory management

    https://developer.apple.com/documentation/metal/heaps/image_filter_graph_with_heaps_and_fences?langu ...

  9. PHP流协议

    目前对PHP流协议的定义是数据传输过程中,不同数据类型采用不同处理函数的技术规范(个人理解)这一规范比起传统文件处理函数来的更规范(诸如fget,fwrite,fopen,fclose等) $opts ...

  10. Qt 工程文件(.pro)

    qmake –project 这个命令是用来生成QT的工程文件(.pro)的,这个文件是用来设置编译或者链接的变量,以便用qmake生成相对应的Makefile文件 TEMPLATE:这个变量是用来定 ...