POJ 1753 Flip Game DFS枚举
看题传送门: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枚举的更多相关文章
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- poj 1753 Flip Game (dfs)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28805 Accepted: 12461 Descr ...
- POJ 1753 Flip Game【枚举】
题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...
- poj 1753 Flip Game(暴力枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52279 Accepted: 22018 Des ...
- POJ 1753 Flip Game (枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26492 Accepted: 11422 Descr ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- 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 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
随机推荐
- js---14公有私有成员方法
var ns1 = {}; //命名空间 ns1.ns11 = {};//子命名空间 ns1.module1 = {name:"a",m:function(){}}; consol ...
- ASP.Net MVC默认目录结构
1.Controllers 保存处理URL请求的Controller类 2.Models 保存操纵业务与数据对象的类 3.Views 保存UI模板页面 4.Scripts 保存Javascript库文 ...
- 关于exports 和 module.exports
本文来源为node.js社区附上链接 http://cnodejs.org/topic/5231a630101e574521e45ef8 require 用来加载代码,而 exports 和 modu ...
- idea配置spark运行模式
1. 配置运行参数: Menu -> Run -> Edit Configurations -> 选择 + -> Application -Dspark.master=lo ...
- 图片上传5-多个图片上传,独立项目Demo和源码
图片上传,一次性可以上传多个图片,每个图片可以有名字.URL.排序.备注等字段.这是区别于使用百度WebUploader等多图上传工具的地方. 项目相关图片 Jar包管理:Maven用到的框架:Spr ...
- c#程序打包、机器代码生成(Ngen.exe)
深入本机影像生成器(Ngen.exe)工具使用方法详解 先介绍一点背景知识:.Net程序在运行时会实时(JIT)编译,将.Net程序文件编译成cpu认识的汇编机器码.实时编译需要消耗额外的cpu和内存 ...
- Scala——构造函数
Scala的构造函数分为主构造函数和辅助构造函数. 辅助构造函数 辅助构造函数比较容易理解,它们同C++和Java的构造函数十分类似,只有两处不同: 1.辅助构造函数的名称为this,这主要是考虑到在 ...
- x64系统下,InpOutx64有数字签名而WinIO3.0无数字签名
参考文档 http://www.highrez.co.uk/Downloads/InpOut32/ //可以下载InpOutx64的驱动程序及DLL,还有驱动主板硬件IO的例程 https://www ...
- cocos2d-x嵌入移动MM短代支付IAP2.4的SDK,点击支付崩溃的解决的方法
凡是用IAP2.4,调用purchase.order 游戏崩溃.logcat报错是: 06-16 18:43:42.944: E/AndroidRuntime(8526): FATAL EXCEPTI ...
- 转换PHP脚本成为windows的执行程序
转换PHP脚本成为windows的执行程序 Convert a PHP script into a stand-alone windows executable I want to automate ...