题目地址: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的更多相关文章

  1. [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)

    先列出题目: 1.POJ 1753 POJ 1753  Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...

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

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

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

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

  4. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

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

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

  6. POJ 1753 Flip Game(二进制枚举)

    题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...

  7. POJ 1753 Flip Game【枚举】

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

  8. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

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

随机推荐

  1. 3月23.CSS表格布局

    360表格布局: CSS定义标签: @charset "utf-8";/* CSS Document */.bt1{ border:#309 solid 1px; height:1 ...

  2. [转]uses-permission权限列表

    android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问”properties”表在checkin数据库中,改值可以修改上传 android.permiss ...

  3. 在Sharepoint 2010中启用Session功能的说明文档

    在Sharepoint 2010中启用Session功能的说明文档 开发环境:Windows 7系统,SharePoint Server 2010,Visual Studio 2010 按以下步骤进行 ...

  4. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  5. discuz 帖子模块用到的表及自动发帖函数

    最近在做一个discuz的插件,由于需要程序自动生成并调用discuz已经存在插件的帖子.然而这就相当于自动发帖的功能了.网上找了一下,大部分都是通过curl模拟登陆,模拟发帖的,这显然不满足我的要求 ...

  6. JavaScript或jQuery模拟点击超链接和按钮

    有时候我们需要页面自动点击超链接或者按钮,可以用js或者jQuery利用程序去点击,方法很简单,按钮或超链接代码如下: <a href="url" target=" ...

  7. Java for LeetCode 067 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  8. Android下利用Bitmap切割图片

    在自己自定义的一个组件中由于需要用图片显示数字编号,而当前图片就只有一张,上面有0-9是个数字,于是不得不考虑将其中一个个的数字切割下来,需要显示什么数字,只需要组合一下就好了. 下面是程序的关键代码 ...

  9. Linux命令自己总结

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...

  10. 查看CPU是否支持虚拟化

    参考:http://www.cnblogs.com/jankie/archive/2012/07/04/2575695.html 一.Windows平台:使用cpu-Z即可查看. 二.Linux平台: ...