[POJ3279]Fliptile(开关问题,枚举)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ3279]Fliptile(开关问题,枚举)相关的知识,希望对你有一定的参考价值。

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

题解:http://www.cnblogs.com/helenawang/p/5538547.html

  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 
 78 bool ok(int x, int y) {
 79     return x >= 0 && x < m && y >= 0 && y < n;
 80 }
 81 
 82 int get(int x, int y) {
 83     int c = G[x][y];
 84     Rep(i, 5) {
 85         int xx = x + dx[i];
 86         int yy = y + dy[i];
 87         if(ok(xx, yy)) c += tmp[xx][yy];
 88     }
 89     return c % 2;
 90 }
 91 
 92 int calc() {
 93     For(i, 1, m) Rep(j, n) if(get(i-1, j) != 0) tmp[i][j] = 1;
 94     Rep(i, n) if(get(m-1, i) != 0) return -1;
 95     int p = 0;
 96     Rep(i, m) Rep(j, n) p += tmp[i][j];
 97     return p;
 98 }
 99 
100 int main() {
101     // FRead();
102     while(~Rint(m) && ~Rint(n)) {
103         Cls(ret); Cls(tmp); Cls(G); ans = -1;
104         Rep(i, m) Rep(j, n) Rint(G[i][j]);
105         int nn = 1 << n;
106         Rep(i, nn) {
107             Cls(tmp);
108             Rep(j, n) tmp[0][n-j-1] = i >> j & 1;
109             int num = calc();
110             if(num >= 0 && (ans < 0 || ans > num)) {
111                 ans = num;
112                 memcpy(ret, tmp, sizeof(tmp));
113             }
114         }
115         if(ans < 0) puts("IMPOSSIBLE");
116         else {
117             Rep(i, m) {
118                 Rep(j, n) {
119                     printf("%d%c", ret[i][j], j+1==n?\\n: );
120                 }
121             }
122         }
123     }
124     RT 0;
125 }

 

以上是关于[POJ3279]Fliptile(开关问题,枚举)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3279 Fliptile开关问题,模拟

(POJ-3279)Fliptile (dfs经典---也可以枚举)

poj3279 Fliptile

poj 3279 Fliptile(关灯问题)

POJ 3279 Fliptile

POJ 3279 Fliptile(翻转)