[POJ1753]Flip Game(开关问题,枚举)

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

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

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &a)
 43 #define Rll(a) scanf("%lld", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(int i = 0; i < (len); i++)
 49 #define For(i, a, len) for(int i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
 53 #define lp p << 1
 54 #define rp p << 1 | 1
 55 #define pi 3.14159265359
 56 #define RT return
 57 #define lowbit(x) x & (-x)
 58 #define onenum(x) __builtin_popcount(x)
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long ULL;
 62 typedef pair<int, int> pii;
 63 typedef pair<string, int> psi;
 64 typedef map<string, int> msi;
 65 typedef vector<int> vi;
 66 typedef vector<LL> vl;
 67 typedef vector<vl> vvl;
 68 typedef vector<bool> vb;
 69
 70 const int maxn = 66;
 71 const int dx[6] = {0, 0, 0, 1, -1};
 72 const int dy[6] = {0, 1, -1, 0, 0};
 73 int G[maxn][maxn];
 74 int tmp[maxn][maxn];
 75 int ret[maxn][maxn];
 76 int n, m, ans;
 77 char oho[maxn][maxn];
 78
 79 bool ok(int x, int y) {
 80     return x >= 0 && x < m && y >= 0 && y < n;
 81 }
 82
 83 int get(int x, int y) {
 84     int c = G[x][y];
 85     Rep(i, 5) {
 86         int xx = x + dx[i];
 87         int yy = y + dy[i];
 88         if(ok(xx, yy)) c += tmp[xx][yy];
 89     }
 90     return c % 2;
 91 }
 92
 93 int calc() {
 94     For(i, 1, m) Rep(j, n) if(get(i-1, j) != 0) tmp[i][j] = 1;
 95     Rep(i, n) if(get(m-1, i) != 0) return -1;
 96     int p = 0;
 97     Rep(i, m) Rep(j, n) p += tmp[i][j];
 98     return p;
 99 }
100
101 int main() {
102     // FRead();
103     n = m = 4;
104     Cls(oho); Cls(ret); Cls(tmp); Cls(G); ans = -1;
105     Rep(i, n) Rs(oho[i]);
106     Rep(i, m) {
107         Rep(j, n) {
108             if(oho[i][j] == ‘b‘) G[i][j] = 0;
109             else G[i][j] = 1;
110         }
111     }
112     int nn = 1 << n;
113     Rep(i, nn) {
114         Cls(tmp);
115         Rep(j, n) tmp[0][n-j-1] = i >> j & 1;
116         int num = calc();
117         if(num >= 0 && (ans < 0 || ans > num)) ans = num;
118     }
119     Cls(ret); Cls(tmp); Cls(G);
120     Rep(i, m) {
121         Rep(j, n) {
122             if(oho[i][j] == ‘b‘) G[i][j] = 1;
123             else G[i][j] = 0;
124         }
125     }
126     Rep(i, nn) {
127         Cls(tmp);
128         Rep(j, n) tmp[0][n-j-1] = i >> j & 1;
129         int num = calc();
130         if(num >= 0 && (ans < 0 || ans > num)) ans = num;
131     }
132     if(ans < 0) puts("Impossible");
133     else printf("%d\n", ans);
134     RT 0;
135 }
时间: 2024-08-08 21:57:55

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

poj1753 Flip Game(枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1753 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the o

POJ1753——Flip Game

Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each r

poj1753 Flip Game DFS,枚举

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34437   Accepted: 15058 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the

POJ1753 Flip Game(bfs、枚举)

链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either i

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

题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 *

POJ 1753 Flip Game(高斯消元法,枚举自由变元)

题目: Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34731   Accepted: 15207 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and

POJ 1753 Flip Game(dfs+枚举)

POJ 1753 题意: 输入一个4*4的图像,由黑白两色组成,定义一种操作为:改变某个格子内小球的颜色(黑变白,白变黑),同时其上下左右的格子内小球也将变色.求最少多少次操作能使之成为纯色图案. 思路: 对一个格子操作偶数次等于没有操作,操作奇数次等于操作一次,所以答案在0~16以及impossible之间. 从n=0开始枚举n次操作可能的组成情况,即操作哪几个格子,若某种组合能变图案为纯色则停止. 由于总组合数达到2^16,故枚举组合情况可以用dfs来进行回溯枚举. //还有一种方法是位运算

poj 1753 Flip Game (bfs + 枚举)

链接:poj 1753 题意:这是翻棋游戏,给定4*4棋盘,棋子一面为黑色(用b表示),另一面为白色(用w表示),问至少要几步可以将棋子翻为全黑或者全白,如不能达到目的,输出"Impossible " 翻转规则:可以选定16个棋子中的任意一个,将其本身以及上下左右相邻的翻转过来 分析:其实每格棋子最多只可以翻转一次(实际是奇数次,但与翻转一次状态一样),只要其中一格重复翻了2次(不论是连续翻动还是不连翻动),那么它以及周边的棋子和没翻动时的状态是一致的,与最初状态未翻转一样,由此就可以

poj1753 Flip Game(BFS+位压缩)

题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子意味着将该格子及其上下左右格子(如果存在的话)的黑面朝上变成白面朝上,反之亦然,游戏的目标是格子全部黑面朝上或者全部白面朝上.输入棋盘的初始状态,求最少经过多少轮可以达到游戏的目标. 思路 求最少轮数,我会想到使用bfs来解决(dfs也可以解决),但使用bfs求解,如果每个状态都直接存储下当前棋盘的