枚举 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 ...
随机推荐
- ListView异步加载网络图片完美版之双缓存技术
本示例参考学习了一个国外的示例:http://code.google.com/p/android-imagedownloader/,有兴趣的同学下载研究一下. 问题描述:在这一篇博客中将会为大家讲解如 ...
- scrollview里面多张图片,每张都能放大缩小
http://blog.sina.com.cn/s/blog_5d68044001018s1n.html scrollview里面多张图片,每张都能放大缩小 - (void)viewDidLoad{ ...
- DButils实现查询和新增
public static Adttendance DBSql(String data) throws SQLException { String url = "j ...
- 代码风格与树形DP
Streaming很惨,不过因为比赛之间没有提交过就没掉(或掉了)rating.第二题是一个树形DP,但是我都在想第一题了,简直作死. 看着神犇的代码我也是醉了...各种宏,真是好好写会死系列. 看到 ...
- 用JAVA代码实现验证邮箱地址是否符合
public class Test{ public static void main(String[] args){ Test t = new Test(); String email = " ...
- java调用matlab函数
如何将实验结果在matlab中可视化呢,下面使用java语言编程,调用matlab中的函数: 本人安装的是Matlab7.11.0 (R2010a)和 Eclipse 4.2 : 1)首先设置环境变量 ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- js 函数定义三种方式
<p>Js 函数定义的三种方式:</p> <br> <p>方式一:function</p> <script type="te ...
- android.mk文件里的通配符
比方你有如下目录,要编译Classes目录和Code目录下所有cpp src |-android.mk |-Classes |-A.cpp |-B.cpp |-....cpp |-Code |-E.c ...
- Java for LeetCode 035 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...