Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 51056   Accepted: 21551

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

题意:给了你一个4*4的棋盘,棋盘上的每一个格子里都有一颗一面黑一面白的棋子,一开始给出你棋盘的初始状态,你可以给任何一个棋子翻面,但是如果你翻了某一个棋子,那他上下左右的棋子都要翻面。问你最少需要翻多少次才能让16枚棋子都为黑或都为白。
思路:总共有16个棋子,每个棋子只有两种状态,翻或者不翻,因为一面棋子只有两种颜色,翻基数次等同于只翻了1次,翻偶数次等同于没翻。所以可以分别枚举翻1枚,2枚,3枚,4枚......16枚的所有情况。根据排列组合,枚举完所有情况需要2^16次。那如何进行枚举呢?可以用for循环一 一枚举某一次需要翻的棋子数,然后用DFS搜索翻这个数量的棋子的所有情况。如何DFS呢?首先选择要翻的第一个棋子,然后下一个要翻的棋子从已选择的这个棋子之后进行选择,一直到选完需要数量的棋子,然后判断所有棋子是否为同一种颜色,如果是,则不用再进行搜索了,这就是答案,因为你是从1-16依次枚举,所以第一次满足条件的情况一定是次数最少的;如果不是则继续搜索。具体看代码和注释理解:
 #include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int visit[];
int map[][];
int ans; void Flip()
{
int x,y;
for(int i=; i<=; ++i)
{
if(visit[i]) //如果标记为 1,表示这个棋子要翻
{
x = i/ + ; // 计算出棋子再棋盘中的位置
y = i % ;
if(i% == )
{
x--;
y = ;
}
map[x][y] = -map[x][y]; //翻自己
map[x-][y] = -map[x-][y]; //翻上
map[x+][y] = -map[x+][y]; //翻下
map[x][y+] = -map[x][y+]; //翻左
map[x][y-] = -map[x][y-]; //翻右
}
}
return;
} int Judge() //判断是否为同一种颜色
{
for(int i=; i<=; ++i)
for(int j=; j<=; ++j)
{
if(map[i][j] != map[][])
return ;
}
return ;
} void DFS(int num,int times,int who) //num表示总共要翻几个琪,times表示已经翻了几个,who表示上一次翻的是哪一个棋
{
if(ans < inf) return; //如果已经找到答案了,就不用往下了
if(times == num) //当翻棋的数量达到了,就进行判断
{
Flip(); //翻棋
if(Judge()) //判断全为同一种颜色
{
ans = num; //记录答案
}
else Flip(); //否则将棋盘恢复原状
return;
} for(int i=who+; i<=; ++i) //从上一个翻的棋子的下一个开始选择,这样可以防止出现重复的情况
{
visit[i] = ;//选中的标记为 1
DFS(num,times+,i); //搜索翻下一个的情况,
visit[i] = ; //清除标记
}
} int main()
{
char first;
memset(map,,sizeof(map));
for(int i=; i<=; ++i)
for(int j=; j<=; ++j)
{
cin>>first;//依次输入棋子
if(first == 'w')
map[i][j] = ; //若为白棋,记为 1
else map[i][j] = -; //黑记为为 -1
} ans = inf; //初始化答案为无穷大
for(int i=; i<=; ++i) //枚举翻棋子数
{
memset(visit,,sizeof(visit)); //每种情况开始前要将标记数组初始化为 0
DFS(i,,); //搜索翻这么多棋子的所有情况
if(ans < inf) break; //如果答案不为无穷大,表示已经有结果了,不用搜了
} if(ans == inf) cout<<"Impossible"<<endl;
else cout<<ans<<endl;
return ;
}
 

poj—1753 (DFS+枚举)的更多相关文章

  1. POJ 1753 DFS

    思路: 有过两个裸搜的思路,第一个一个无限TLE,第二个还不慢... 错误思路:迭代加深搜索,枚举翻第几个棋,挂的原因:16的16次方,不挂就怪了. 错误代码见下: #include <cstd ...

  2. POJ 1753 Flip Game DFS枚举

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

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

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

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

  5. [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)

    先列出题目: 1.POJ 1753 POJ 1753  Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...

  6. 枚举 POJ 1753 Flip Game

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

  7. POJ 1753 位运算+枚举

    题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...

  8. poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

    链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...

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

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

随机推荐

  1. django-bower

    https://django-bower.readthedocs.io/en/latest/usage.html

  2. 如何查找EI 及SCI 索引

    1.EI https://www.engineeringvillage.com/search/quick.url 搜题目,然后点击搜索,然后点击细节(detailed) Accession numbe ...

  3. ztree--插件实现增删改查demo(完整版)

    ztree--插件实现增删改查demo(完整版) var setting = {                 async: {                     enable: true,  ...

  4. python学习——练习题(11)

    """ 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1 1 2 ...

  5. 避免IE在ajax请求时,返回json出现下载

    转自:https://blog.csdn.net/z521q1314/article/details/54409048

  6. C# a++ 与 ++a 的区别

    C# a++ 与 ++a 的区别 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-21 查资料时看到别人的写法 while(+ ...

  7. Tomcat ( 单机多 Tomcat 并存 )

    简介: Tomcat 扩展( 一台服务器运行多个 tomcat ) 一.安装 JDK .Tomcat shell > rpm -ivh jdk-8u25-linux-x64.rpm # 安装 j ...

  8. Ubuntu设置开机启动服务

    一.查看当前的运行级别 runlevel 二.加载服务 $ cd /etc/init.d $ sudo update-rc.d test defaults 95 三.增加默认启动脚本 sudo vi ...

  9. 网页静态化解决方案:Freemarker生成简单html页面

    FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP.它不仅 ...

  10. 【LA3523 训练指南】圆桌骑士 【双连通分量】

    题意 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防赞同和反 ...