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. Java 内存机制、内存泄露

    http://wenku.baidu.com/view/61f31da6284ac850ad02423e.html 自己注:hashset和hashmap不同 JDK1.6 API 中 HashSet ...

  2. 使用国内源部署ceph

    由于网络方面的原因,Ceph的部署经常受到干扰,通常为了加速部署,基本上大家都是将Ceph的源同步到本地进行安装.根据Ceph中国社区的统计,当前已经有国内的网站定期将Ceph安装源同步,极大的方便了 ...

  3. canvas绘制矩形

    canvas绘制矩形 方法 fillRect(x, y, width, height) 画一个实心的矩形 clearRect(x, y, width, height) 清除一块儿矩形区域 stroke ...

  4. 12.solr学习速成之dataimport

    solr除了利用solrj建立索引外,还可以由列式数据库hbase触发器添加索引,自动全量或者增量索引关系数据库数据等. dataimport可以配置从任何关系数据库导入索引 1.将jar包拷贝到to ...

  5. 第6章Zabbix分布式监控

    Zabbix是一个分布式的监控系统.分布式监控适合跨机房.跨地域的网络监控.从多个Proxy收集数据,而每个Proxy可以采集多个设备的数据,从而轻松地构建分布式监控系统. ZabbixProxy可以 ...

  6. django使用mysql

    django使用mysql需要注意在project目录下的__init__.py 中添加:import pymysql pymysql.install_as_MySQLdb()

  7. maven surefire plugin介绍

    示例 <!-- 测试运行器,生成测试报告 --> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  8. loadView 和 viewDidLoad、viewDidunload 的区别

    loadView 和 viewDidLoad 是 iPhone 开发中肯定要用到的两个方法. 他们都可以用来在视图载入的时候初始化一些内容. 但是他们有什么区别呢? viewDidLoad 方法只有当 ...

  9. Linux 入门知识一(附上如何解决Ubuntu的root密码问题)

    .centos有拥有七个控制台,其中第一到第六个是字符界面,第七个是图形界面 切换的快捷键是ctrl+shift+fn(n为自然数)   输入tty的话,可以检查当前处于哪个控制台   如何在cent ...

  10. SData:优雅的数据交换方案

    SData的网址是https://github.com/knat/SData. 数据交换方案可以分为两类:有纲要(schema)的和无纲要的.有纲要的数据交换方案有Google的Protocol Bu ...