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. 根据段落编号自动添加书签的VBA

    Sub 宏1() ' ' 宏1 宏 ' '    Dim myRange As Word.Range Dim num As String, content As String Selection.Ho ...

  2. 广义线性模型(Generalized Linear Models)

    在线性回归问题中,我们假设,而在分类问题中,我们假设,它们都是广义线性模型的例子,而广义线性模型就是把自变量的线性预测函数当作因变量的估计值.很多模型都是基于广义线性模型的,例如,传统的线性回归模型, ...

  3. Disconf实践指南:使用篇

    在上一篇文章Disconf实践指南:安装篇介绍了如何在本地搭建Disconf环境,下面我们介绍如何在项目中使用Disconf.由于某些功能特性对源码做了修改,所以在官方文档并没有提及. 环境基于mac ...

  4. 11.redis连接

    转自:http://www.runoob.com/redis/redis-tutorial.html Redis 客户端连接 Redis 通过监听一个 TCP 端口或者 Unix socket 的方式 ...

  5. sql常用优化

    最近因工作需要,查找资料整理了一下sql的常见优化,具体如下: 1.尽量避免全表扫描,一般在where或order by 涉及的列上建立索引: 2.尽量避免在where子句中对索引字段进行null值判 ...

  6. mediawiki的安装

    1. yum install httpd php pcre php-mysql php-pear php-pecl-apc mysql-server ImageMagick sendmail php- ...

  7. Git(三):Git 使用规范流程

    转:http://www.ruanyifeng.com/blog/2015/08/git-use-process.html 团队开发中,遵循一个合理.清晰的Git使用流程,是非常重要的. 否则,每个人 ...

  8. python:while 语句的使用方法

    while语句: count = 0 while True: print(count) count += 1 if count == 10: break 实例: 计算n!,若:n = 5:则:n! = ...

  9. AOP的MethodBeforeAdvice

    使用Spring自动生成代理类,spring利用的是动态代理机制 接口 Java代码 public interface UserDao { void addUser(); void deleteUse ...

  10. webService 入门级

    1,建立一个项目名为Trans,web项目,普通java项目都可以!这里我们就以简单的java应用程序来作为示范吧! 1.1在建立一个方法属于com.shu.function.Function类: / ...