题目链接:http://poj.org/problem?id=1753

和上一个题一样,将初始状态存成01矩阵,就可以用位运算优化了。黑色白色各来一遍

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; const int maxn = ;
const int dx[] = {, , , , -};
const int dy[] = {, , -, , };
int G[maxn][maxn];
int tmp[maxn][maxn];
int ret[maxn][maxn];
int n, m, ans;
char oho[maxn][maxn]; bool ok(int x, int y) {
return x >= && x < m && y >= && y < n;
} int get(int x, int y) {
int c = G[x][y];
Rep(i, ) {
int xx = x + dx[i];
int yy = y + dy[i];
if(ok(xx, yy)) c += tmp[xx][yy];
}
return c % ;
} int calc() {
For(i, , m) Rep(j, n) if(get(i-, j) != ) tmp[i][j] = ;
Rep(i, n) if(get(m-, i) != ) return -;
int p = ;
Rep(i, m) Rep(j, n) p += tmp[i][j];
return p;
} int main() {
// FRead();
n = m = ;
Cls(oho); Cls(ret); Cls(tmp); Cls(G); ans = -;
Rep(i, n) Rs(oho[i]);
Rep(i, m) {
Rep(j, n) {
if(oho[i][j] == 'b') G[i][j] = ;
else G[i][j] = ;
}
}
int nn = << n;
Rep(i, nn) {
Cls(tmp);
Rep(j, n) tmp[][n-j-] = i >> j & ;
int num = calc();
if(num >= && (ans < || ans > num)) ans = num;
}
Cls(ret); Cls(tmp); Cls(G);
Rep(i, m) {
Rep(j, n) {
if(oho[i][j] == 'b') G[i][j] = ;
else G[i][j] = ;
}
}
Rep(i, nn) {
Cls(tmp);
Rep(j, n) tmp[][n-j-] = i >> j & ;
int num = calc();
if(num >= && (ans < || ans > num)) ans = num;
}
if(ans < ) puts("Impossible");
else printf("%d\n", ans);
RT ;
}

[POJ1753]Flip Game(开关问题,枚举)的更多相关文章

  1. POJ-1753 Flip Game---二进制枚举子集

    题目链接: https://vjudge.net/problem/POJ-1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...

  2. POJ1753 Flip Game(bfs、枚举)

    链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...

  3. POJ1753 Flip Game(位运算+暴力枚举)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  4. [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)

    题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...

  5. poj1753 Flip Game(BFS+位压缩)

    题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...

  6. poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推

    题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  7. poj1753,Flip Game,ArrayDeque&lt;Node&gt;

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30449   Accepted: 13232 Descr ...

  8. poj1753 Flip Game

    题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...

  9. POJ1753——Flip Game

    Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...

随机推荐

  1. Java加解密与数字签名

    ** Java加解密 ** 实现方式:JDK实现,CC,BC JDK提供比较基础的底层的实现:CC提供一些简化的操作:BC提供补充 一.Base64加密 非常简单,加密解密就一个函数. 代码如下: 二 ...

  2. Mysql几种索引类型的区别及适用情况

    如大家所知道的,Mysql目前主要有以下几种索引类型:FULLTEXT,HASH,BTREE,RTREE. 那么,这几种索引有什么功能和性能上的不同呢? FULLTEXT 即为全文索引,目前只有MyI ...

  3. WPF简单布局 浅尝辄止

            WPF的窗口只能包含一个元素,为了在WPF窗口中放置多个元素并创建更实用的用户界面,需要在窗口上放置一个容器,然后在容器中放置其它元素. 注意:造成这一限制的原因是window类继承自 ...

  4. C# abstract function VS virtual function?

    An abstract function has to be overridden while a virtual function may be overridden. Virtual functi ...

  5. iOS$299企业账号In House ipa发布流程

    1.在Mac系统中进入“钥匙串访问”,选择“钥匙串访问”-“证书助理”-“从证书颁发机构请求证书”. 填写前两项,并保存在本地. 2.登录https://developer.apple.com,进入i ...

  6. oracle 建立主键与索引【转】

    此文转自:http://blog.sina.com.cn/s/blog_439f80c4010094n1.html 创建主键: alter table T add primary key (V) T是 ...

  7. 框架优化系列文档:SVN中非版本控制文件忽略上传的设置

    对于SVN代码库,只应该上传源代码.资源文件等内容进行版本管理,通常编译后的二进制文件.程序包等生成产物是不应该放到SVN上做版本管理的.因此在svn的客户端工具中设置svn的属性:svn:ignor ...

  8. .net发送邮件代码示例

    下面的代理已经调试过,用的是163的SMTP using System;using System.Collections.Generic;using System.Linq;using System. ...

  9. hdu 1166 树状数组 线段树入门

    点修改 区间求和 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...

  10. 【nginx网站性能优化篇(1)】gzip压缩与expire浏览器缓存

    gzip压缩 概述 网页在服务器端经过了gzip或者其他格式的压缩后的输出明显减少了content-length字节,当访问过百万时,这些减少的字节就会变为客观的流量给节约下来;从而减轻服务器的压力以 ...