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 ;
}

最新文章

  1. Android自定义控件6--轮播图广告的实现
  2. CSS的重要性
  3. jQuery 的属性
  4. Spring配置文件详解 – applicationContext.xml文件路径
  5. uiwebview的基本使用
  6. spring mvc + freemarker优雅的实现邮件定时发送
  7. android中数据存储的contentprovider的使用方法
  8. java之内存管理
  9. Linux 软连接与硬连接
  10. 深入理解JVM垃圾收集机制(JDK1.8)
  11. 【Python3爬虫】常见反爬虫措施及解决办法(二)
  12. CS 2505 Computer Organization I C09: Rational Number
  13. 12.QT4.7.4-解决WIN平台和Linux平台中文乱码,QLineEdit右击菜单中文显示
  14. thinkphp 实现分页
  15. 论文阅读笔记二十六:Fast R-CNN (ICCV2015)
  16. DbContext SQLite配置文件
  17. 全屏加载loading显示的解决方法
  18. 图解SSH原理
  19. Inception
  20. 打开/查找xcode6的沙盒地目录

热门文章

  1. django inclusion用法
  2. 安卓开发之sql语句增删改查2(利用谷歌封装好的API进行增删改查)
  3. android程序复制数据库
  4. tensorflow保存数据为.pb格式和加载pb文件
  5. namespace&quot; 或The content of element type &quot;mapper&quot; must match &quot;EMPTY&quot;
  6. Linux学习笔记(三)Linux常用命令:链接命令和文件查找命令
  7. idea仿eclipse的export导出功能
  8. CodeForcs 1169B Good Triple
  9. 01 js数据类型
  10. BZOJ4383 [POI2015]Pustynia[线段树优化建边+拓扑排序+差分约束]