# 题解 POJ 2985 The Pilots Brothers' refrigerator

1 bool PopcountComp(int a, int b) {
2    return popcount(a) < popcount(b);
3 }


1 int Stable[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
2 int popcountS(int a) { return Stable[a >> 4] + Stable[a & 15]; }
3 int popcount(int a) { return popcountS(a >> 8) + popcountS(a & 255); }


## 代码

//     自己选择的路，跪着也要走完。朋友们，虽然这个世界日益浮躁起来，只
// 要能够为了当时纯粹的梦想和感动坚持努力下去，不管其它人怎么样，我们也
// 能够保持自己的本色走下去。                               ——陈立杰
/*************************************
* @problem:      2965: The Pilots Brothers' refrigerator.
* @user_name:    HKXA0933.
* @time:         2020-06-02.
* @language:     C++.
*************************************/

char t[5];
bool status[4][4], tmp[4][4];
int way[65536];

int Stable[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
int popcountS(int a) { return Stable[a >> 4] + Stable[a & 15]; }
int popcount(int a) { return popcountS(a >> 8) + popcountS(a & 255); }
bool PopcountComp(int a, int b) { return popcount(a) < popcount(b); }

bool check(int way) {
memcpy(tmp, status, sizeof(status));
for (int i = 0; i < 16; i++)
if (way & (1 << i)) {
int x = i >> 2, y = i & 3;
tmp[x][0] ^= 1; tmp[x][1] ^= 1; tmp[x][2] ^= 1; tmp[x][3] ^= 1;
tmp[0][y] ^= 1; tmp[1][y] ^= 1; tmp[2][y] ^= 1; tmp[3][y] ^= 1;
tmp[x][y] ^= 1;
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (!tmp[i][j]) return false;
return true;
}

signed main() {
for (int i = 0; i < 4; i++) {
scanf("%s", t);
for (int j = 0; j < 4; j++)
status[i][j] = t[j] == '-';
}
for (int i = 0; i < 65536; i++) way[i] = i;
sort(way, way + 65536, PopcountComp);
for (int i = 0; i < 65536; i++)
if (check(way[i])) {
printf("%d\n", popcount(way[i]));
for (int j = 0; j < 16; j++)
if (way[i] & (1 << j))
printf("%d %d\n", (j >> 2) + 1, (j & 3) + 1);
break;
}
return 0;
}

// Create File Date : 2020-06-02

posted @ 2020-06-03 08:21  航空信奥  阅读(170)  评论(0编辑  收藏  举报