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

  1. bwwb
  2. bbwb
  3. bwwb
  4. bwww

Sample Output

  1. 4

Source

 
题目大意:
有一个 4*4 的矩阵,然后现在每个格子有两个颜色,要么是黑色,要么是白色,当你的手按动一个格子的时候,这个格子的上、下、左、右都会
改变原来的颜色,即由黑变白,由白变黑,现在问你的是,经过最少几次翻转之后可以使得这个 4*4 的矩阵变为全是白色的或者是全是黑色的。
 
解题思路:
1)使用DFS
 每格棋子最多只可以翻转1次,
 这是因为只要其中一格重复翻了2次(不论是连续翻动还是不连翻动),
 那么它以及周边的棋子和没翻动时的状态是一致的。
算法停止条件就是当前翻转的棋子个数>=17个 以及 当前棋盘棋子为同一颜色
剪枝条件即为当前翻转棋子的个数>最优解bestx,则进行回溯
由于我们需要考虑的其实就是每一个棋子翻不翻转,所以对于已经考察过的点需要进行剪枝。
 
  1. #include<iostream>
  2. using namespace std;
  3. int color[];
  4. int a[];
  5. int bestx;
  6. bool checkColor()
  7. {
  8. int a = color[];
  9. for(int i=;i<;i++)
  10. {
  11. if(a != color[i])
  12. {
  13. return ;//存在不同色
  14. }
  15. }
  16. return ;//同色
  17. }
  18. void BackTrack(int t,int pre)
  19. {
  20. if(checkColor() || t>=)//如果同色
  21. {//更新解
  22. if(checkColor())
  23. {
  24. if(t-<bestx)
  25. bestx = t-;
  26. }
  27. return;
  28. } else{
  29. for(int i=pre;i<;i++)
  30. {
  31. if(a[i] == )//没有翻过
  32. {
  33. //进行翻牌
  34. a[i] = ;
  35. color[i] = -color[i];
  36. if(i->=) color[i-] = -color[i-];
  37. if(i+<=) color[i+] = -color[i+];
  38. if(i->= && (i-)%!=) color[i-] = -color[i-];
  39. if(i+<= && (i+)%!=) color[i+] = -color[i+];
  40.  
  41. if(t<bestx)//剪枝
  42. BackTrack(t+,i+);
  43.  
  44. a[i] = ;//回溯
  45. color[i] = -color[i];
  46. if(i->=) color[i-] = -color[i-];
  47. if(i+<=) color[i+] = -color[i+];
  48. if(i->= && (i-)%!=) color[i-] = -color[i-];
  49. if(i+<= && (i+)%!=) color[i+] = -color[i+];
  50. }
  51. }
  52.  
  53. }
  54.  
  55. }
  56. int main()
  57. {
  58. while(true)
  59. {
  60. bestx=;
  61. for(int i=;i<;i++)
  62. a[i]=;
  63. char c;
  64. for(int i = ;i<;i++)
  65. {
  66. if(i % == && i!=)
  67. {
  68. c = getchar();
  69. }
  70. c = getchar();
  71. if(c == EOF) return ;
  72. if(c == 'b') color[i] = ;
  73. else if(c == 'w') color[i] = ;
  74.  
  75. }
  76. BackTrack(,);
  77. if(bestx == )
  78. cout<<"Impossible"<<endl;
  79. else
  80. cout<<bestx<<endl;
  81. }
  82.  
  83. return ;
  84. }

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 EE,java Web中的400,404,405等各种错误介绍

    4 请求失败4xx 4xx应答定义了特定服务器响应的请求失败的情况.客户端不应当在不更改请求的情况下重新尝试同一个请求.(例如,增加合适的认证信息).不过,同一个请求交给不同服务器也许就会成功. 4. ...

  2. Windows下同时安装两个版本Jdk

    在项目开发中遇到了jdk版本切换的问题,于是尝试在电脑中安装jdk1.6和jdk1.7,话不多说马上开始 1 准备好两个版本的jdk路径 2 设置两个JAVA_HOME 3 设置总的动态切换的JAVA ...

  3. Linux目录结构以及一些常见操作

    本章内容: Linux 目录结构 远程服务器关机及重启时的注意事项 不要在服务器访问高峰运行高负载命令 远程配置防火墙时不要把自己踢出服务器 指定合理的密码规范并定期更新 合理分配权限 定期备份重要数 ...

  4. TensorFlow指定GPU/CPU进行训练和输出devices信息

    TensorFlow指定GPU/CPU进行训练和输出devices信息 1.在tensorflow代码中指定GPU/CPU进行训练 with tf.device('/gpu:0'): .... wit ...

  5. 自动化测试环境搭建(appium+selenium+python)

    一.需要安装的软件(根据你所需要的版本安装即可,不一定必须按照小编的版本来) JDK:1.8.0_171 Python:3.7.1 node.js:10.13.0 android-sdk_r24.4. ...

  6. 2019 Petrozavodsk Winter Camp, Yandex Cup C. Diverse Singing 上下界网络流

    建图一共建四层 第一层为N个歌手 第二层为{pi,li} 第三层为{si,li} 第四层为M首歌 除了S和第一层与第三层与T之间的边为[1,INF] 其他边均为[0,1] #include<bi ...

  7. Hbuilder 开发微信小程序的代码高亮

    一.点击“工具”-“选项”-搜索“文件关联” 二.点击“添加”文件类型,点击确定 三.在相关联的编辑器中点击“添加”按钮,选择CSS Editor,点击确定,重新打开 *.wxss 类型的文件即可 其 ...

  8. CF901C Bipartite Segments[点双+二分+前缀优化]

    不想翻译了,直接放luogu翻译 说了没有偶环,也就是说全是奇环,再结合二分图性质,那么暴力的话,固定左端点,增大序号,加点直到产生环就不合法了.也就是说,任何一个环,只要他上面的数全都被加了,就不合 ...

  9. IF语句及代码练习

    接着上篇的内容  ㈠ if . . . else . . .语句 ⑴语法 if(条件表达式){              语句. . . } else {             语句. . . } ...

  10. Python文本和字节序列

    ASCII码 早期人们用8位二进制来编码英文字母(最前面的一位是0) 也就是说,将英文字母和一些常用的字符和这128种二进制0.1串一一对应起来, 比如:大写字母“A”所对应的二进制位“0100000 ...