位运算应用(记录)
作者:
Type-umr
,
2024-07-25 00:59:35
,
所有人可见
,
阅读 3
POJ1753 Flip Game
#include<iostream>
using namespace std;
int arr[5];//黑色状态
int brr[5];//白色状态
char ch;
int ans = 1e9 + 7;
int sum (int x) {
int ans = 0;
while (x) {
if (x & 1) ans++;
x >>= 1;
}
return ans;
}
void solved (int a[]) {
int crr[5];
int cnt = 0;
for (int i = 0; i <= ((1 << 4) - 1); i++) {//枚举第一行2^4种翻转情况,用01序列代表黑白情况
cnt = sum(i);
crr[0] = a[0] ^ i ^ (i >> 1) ^ ((i << 1) & ((1 << 4) - 1));
crr[1] = a[1] ^ i;
for (int j = 1; j < 4; j++) {
int tt = crr[j - 1];
cnt += sum(tt);
crr[j] = crr[j] ^ tt ^ (tt >> 1) ^ ((tt << 1) & ((1 << 4) - 1));
crr[j + 1] = a[j + 1] ^ tt;
}
if (crr[3] == 0) ans = min(ans, cnt);//若最后一行所有都是同一种颜色,即为正确方案
}
}
int main() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cin >> ch;
if (ch == 'b') arr[i] |= (1 << j);
else brr[i] |= (1 << j);
}
}
solved(arr);
solved(brr);
if (ans > 16) cout << "Impossible" << "\n";
else cout << ans << "\n";
return 0;
}