【题目链接】
http://poj.org/problem?id=2965
【算法】
位运算
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; const int MASK = 65535; int i,j,tmp,s,mn,p,sum; int a[16]; char c; inline int calc(int x,int y) { int pos = 4 * x + y - 5; int ret = 0; int tmp = pos; while (tmp < 4 * x) { ret |= (1 << tmp); tmp++; } tmp = pos; while (tmp >= 4 * x - 4) { ret |= (1 << tmp); tmp--; } tmp = pos; while (tmp < 16) { ret |= (1 << tmp); tmp += 4; } tmp = pos; while (tmp >= y - 1) { ret |= (1 << tmp); tmp -= 4; } return ret; } int main() { for (i = 1; i <= 4; i++) { for (j = 1; j <= 4; j++) { a[4*i+j-5] = calc(i,j); } } for (i = 0; i < 16; i++) { scanf("%c",&c); if (c == '-') s |= (1 << i); if (i % 4 == 3) scanf("%c",&c); } mn = MASK; for (i = 0; i <= MASK; i++) { tmp = s; sum = 0; for (j = 0; j < 16; j++) { if (i & (1 << j)) { sum++; tmp ^= a[j]; } } if (tmp == MASK && sum < mn) { mn = sum; p = i; } } printf("%d ",mn); for (i = 0; i < 16; i++) { if (p & (1 << i)) printf("%d %d ",i/4+1,i%4+1); } return 0; }