标签:
题目链接: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 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/5539351.html