看题传送门:http://poj.org/problem?id=1753

DFS枚举的应用。

基本上是参考大神的。。。。

学习学习。。

#include<cstdio>
#include<iostream>
using namespace std;
int n,s,d,ans;
bool a[5][5],flag=false; //判断全部一样的情况
bool alllike()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(a[i][j]!=a[0][0])
return false; return true;
} //翻转棋
void flip(int r,int c)
{
if(r>0)
a[r-1][c]=!a[r-1][c]; if(c>0)
a[r][c-1]=! a[r][c-1]; if(r<3)
a[r+1][c]=!a[r+1][c]; if(c<3)
a[r][c+1]=!a[r][c+1]; a[r][c]=!a[r][c];
} void dfs(int r,int c,int deep)
{
if(deep==ans)
{
flag=alllike();
return;
} if(r==4||flag)
return; flip(r,c); //翻转棋 if(c<3)
dfs(r,c+1,deep+1);
else
dfs(r+1,0,deep+1); flip(r,c); //把棋翻回来 if(c<3) //如果不满足则不翻转该棋
dfs(r,c+1,deep);
else
dfs(r+1,0,deep); return;
} int main()
{
char c;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
scanf("%c",&c);
if(j==3)
getchar();
// cin>>c; //直接用cin也可以
if(c=='b')
a[i][j]=true;
else
a[i][j]=false;
} for(ans=0;ans<=16;ans++)
{
dfs(0,0,0);
if(flag)
break;
} if(flag)
printf("%d\n",ans);
else
printf("Impossible\n"); }

POJ 1753 Flip Game DFS枚举的更多相关文章

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

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

  2. poj 1753 Flip Game (dfs)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28805   Accepted: 12461 Descr ...

  3. POJ 1753 Flip Game【枚举】

    题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...

  4. poj 1753 Flip Game(暴力枚举)

    Flip Game   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52279   Accepted: 22018 Des ...

  5. POJ 1753 Flip Game (枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26492   Accepted: 11422 Descr ...

  6. 枚举 POJ 1753 Flip Game

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

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

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

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

  9. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

随机推荐

  1. Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.

    1.错误描写叙述 八月 14, 2015 4:22:45 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error 严重: Excepti ...

  2. vim状态保存跟恢复

    当我们结束了一天的工作的时候,可能手头的工作仅仅进行了一半,比如我们正在用vim修改一个android 问题,我们定位了问题关键,牵扯到了好几个类,如果这时候我们直接把vim关闭了,那我们下次还要重新 ...

  3. buffer--cache 详解

  4. View_01_LayoutInflater的原理、使用方法

    View_01_LayoutInflater的原理.使用方法 本篇博客是郭神博客Android视图状态及重绘流程分析,带你一步步深入了解View(一)的读书笔记的笔记. LayoutInflater简 ...

  5. RecyclerView具体解释

    public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild { 由上面的继承结 ...

  6. Trafodion:Transactional SQL on HBase

    Trafodion: Transactional SQL on HBase HBase上实时分布式事务处理 介绍 HBase的SQL能力一直不足.Phoenix缺乏Join能力,eBay提出的kyli ...

  7. Vue 打包后报错 Uncaught TypeError: Cannot redefine property: $router

    原因:就如报错提示所描述的,不能重新定义$router,说明是重复定了$router.通常是因为在项目中安装了vue-router的依赖并且用Vue.use()使用了vue-router,还在inde ...

  8. BZOJ3676: [Apio2014]回文串(SAM+Manacher/PAM)

    Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...

  9. 趣闻|Python之禅(The Zen of Python)

    在Python解释器中输入“import this”会发生什么?如果你不知道这个彩蛋,推荐继续阅读这篇文章. 2001年秋,Foretec(一家会议组织公司)正在准备召开第十届Internationa ...

  10. 七、Docker+nginx

    原文:七.Docker+nginx docker run -p 80:80 --name nginx-v1.0.0 -v /usr/nginx/www:/www -v /home/docker/ngi ...