枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753
/*
这题几乎和POJ 2965一样,DFS函数都不用修改
只要修改一下change规则。。。
注意:是否初始已经ok了要先判断
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
int a[][];
bool flag; bool ok(void)
{
int tmp = a[][];
for (int i=; i<=; ++i)
{
for (int j=; j<=; ++j)
if (a[i][j] != tmp) return false;
} return true;
} void change(int x, int y)
{
a[x][y] = !a[x][y];
if (x > ) a[x-][y] = !a[x-][y];
if (x < ) a[x+][y] = !a[x+][y];
if (y > ) a[x][y-] = !a[x][y-];
if (y < ) a[x][y+] = !a[x][y+];
} void DFS(int x, int y, int num, int cnt)
{
if (num == cnt)
{
flag = ok ();
return ;
}
for (int i=x; i<=; ++i)
{
int j;
if (i == x) j = y + ;
else j = ;
for (; j<=; ++j)
{
change (i, j);
DFS (i, j, num+, cnt);
if (flag) return ;
change (i, j);
}
}
} void work(void)
{
if (ok ())
{
printf ("%d\n", ); return ;
}
int cnt;
for (cnt=; cnt<=; ++cnt) //最多16次,可以暴力枚举
{
flag = false;
DFS (, , , cnt);
if (flag) break;
}
(cnt <= ) ? printf ("%d\n", cnt) : puts ("Impossible");
} int main(void) //POJ 1753 Flip Game
{
//freopen ("A.in", "r", stdin); char ch;
for (int i=; i<=; ++i)
{
for (int j=; j<=; ++j) //b-0 w-1
{
scanf ("%c", &ch);
a[i][j] = (ch == 'b') ? : ;
}
getchar ();
} work (); return ;
} /*
Impossible
*/
枚举 POJ 1753 Flip Game的更多相关文章
- [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)
先列出题目: 1.POJ 1753 POJ 1753 Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...
- 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 ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- POJ 1753 Flip Game(二进制枚举)
题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...
- POJ 1753 Flip Game【枚举】
题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- 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 ...
随机推荐
- 在C#中开启事务
1.为什么要开启事务: 举一个简单的例子:在银行业务中,有一条记账原则,即又借有贷.为了保证这种原则,每发生一笔银行业务,就必须保证会计账目上借方科目和贷方科目至少个少一笔,并且这两笔要么同时成功,要 ...
- memcached工作原理
1.Memcached处理的原子是每一个(key,value)对(以下简称kv对),key会通过一个hash算法转化成hash-key,便于查找.对比以及做到尽可能的散列.同时,memcached用的 ...
- c++中的srand()和rand() 转载 自:http://blog.sina.com.cn/s/blog_624c2c4001012f67.html
今天看了同事写的小程序,发现了其中出现了srand()和rand()这两个我以前没有用过的函数,当然从名字可以看出肯定能随机数有关,于是网查资料知这两个函数配合一起使用来产生随机数的,哈哈,又长知识了 ...
- FastMM的安装方法
FastMM 快速在D2006和2007中已代替了原来的内存管理器.D7也可以使用,而且很方便哦.请看步骤: 1. FastMM是开源项目,去她老家先拖个来. http://sourceforge.n ...
- Linux 在 i 节点表中的磁盘地址表中,若一个文件的长度是从磁盘地址表的第 1 块到第 11 块 解析?
面试题: 在 i 节点表中的磁盘地址表中,若一个文件的长度是从磁盘地址表的第 1 块到第 11块,则该文件共占有 B 块号.A 256 B 266 C 11 D 256×10 linux文件系统是L ...
- Controller之间传递数据:Block传值
http://itjoy.org/?p=420 前边我们介绍过属性传值和协议传值,这里介绍一下块传值,块类似于C中的函数指针.在Controller中传递数据非常方便,还是继续上一章的例子,将数据从S ...
- string s = null 和 string s = “”的区别
string s = null; 表示一个空串,没有占用了空间,不在内存中开辟空间 string s = "";在内存中开辟空间,但空间中没有值(""也是一个字 ...
- 《ASP.NET1200例》未能找到元数据文件解决办法
今天在做一个项目的时候,遇到这样的一个问题,我用的是三层结构的.未能找到元数据文件“D:\SYSTEM\桌面\MyExam\MyExam\MyExamBLL\bin\Debug\BLL.dl ...
- MYSQL 删除字段值为NULL的语法
2014年9月1日 15:11:05 delete form your_table where your_field is null and your_field1 = '123' ...
- POJ1704 Georgia and Bob (阶梯博弈)
Georgia and Bob Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Subm ...