您的位置:首页 > 其它

poj-2965-The Pilots Brothers' refrigerator(巧妙的位运算)

2016-03-28 08:18 489 查看
传送门

这道题和poj1753 类似,都巧妙的运用了位运算

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <string>
#define N 1000010
#define ll long long
using namespace std;
int cs[] = {0x111f, 0x222f, 0x444f, 0x888f, 0x11f1, 0x22f2, 0x44f4, 0x88f8, 0x1f11, 0x2f22, 0x4f44, 0x8f88, 0xf111, 0xf222, 0xf444, 0xf888};
int po[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
int aa[2][20];
int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
int v1, v2, i, j, ans, tmp, t, at;
char c;
v1 = 0;
for (i = 0; i < 16; i++){
cin >> c;
if (c == '+'){
v1 ^= po[i];
}
}
ans = 999999;
t = 0;
for (i = 0; i < 65536; i++){
v2 = v1;
tmp = 0;
for (j = 0;j < 16; j++){
if (i&po[j]){
tmp++;
v2 ^= cs[j];
aa[t][tmp] = j;
}
}
if (v2 == 0){
if (ans >= tmp){
ans = tmp;
at = t;
if (t == 0){
t = 1;
}else{
t = 0;
}
}
}
}
cout << ans << endl;
for (i = 1; i <= ans; i++){
cout << aa[at][i]/4+1 << ' ' << aa[at][i]%4+1 << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: