看题传送门: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. 为root账户更名

    为root账户更名 处于安全考虑许多管理员想把root更名,具体方法如下: 1.先以root登陆系统 2.用vi 编辑/etc/passwd文件,将第一行的第一个root修改为你想要的账户名,然后保存 ...

  2. Exercise: PCA in 2D

    Step 0: Load data The starter code contains code to load 45 2D data points. When plotted using the s ...

  3. SQL 锁 lock

    http://www.cnblogs.com/huangxincheng/p/4292320.html 关于sql 中的锁. 1 排他锁 sql中在做 insert update delete 会存在 ...

  4. JS错误记录 - 右侧悬浮框 - 缓冲运动

    本次练习错误总结: 1.  正确: startMove( document.documentElement.clientHeight - oDiv.offsetHeight + scrollTop); ...

  5. shell项目-分发系统-expect讲解

    shell项目-分发系统-expect讲解 yum install -y expect 1. 自动远程登录 #! /usr/bin/expect set host "192.168.133. ...

  6. RecyclerView具体解释

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

  7. SDUT--Pots(二维BFS)

    Pots Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 You are given two pots, having the ...

  8. js05---js实现Map

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  9. @property 和@synthesize

    xcode4.4之后,@property包括了@synthesize的功能. 这是编译器的升级. @property有几个作用:1)默认生成一个私有成员变量,并有一个带下划线的别名如_age   2) ...

  10. activity-栈相关属性

    1.启动任务栈 第一种,动作设置为“android.intent.action.MAIN”,类别设置为“android.intent.category.LAUNCHER”,可以使这个ACT(activ ...