Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
位运算#include <stdio.h> #include <iostream> #include <cstring> using namespace std; int main() { int t; int min = -1; scanf("%d", &t); int tempwall[20]; while (t--) { min = -1; int n; scanf("%d", &n); int wall[20] = { 0 }; char c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> c; wall[i] <<= 1; wall[i] += (c == 'y'); } } int sum; int change[20]; for (int i = 0; i < (1 << n); i++) { memcpy(tempwall, wall, sizeof(wall)); change[1] = i; for (int j = 2; j <= n; j++) { change[j] = (~(change[j - 1] ^ tempwall[j - 1] ^ (change[j - 1] << 1) ^ (change[j - 1] >> 1))) &~(~0 << n); tempwall[j] = tempwall[j] ^ change[j - 1]; } if (((change[n] ^ tempwall[n] ^ (change[n] << 1) ^ (change[n] >> 1))&~(~0 << n)) == ((1 << n) - 1)) { sum = 0; for (int j = 1; j <= n; j++) for (int k = 0; k < n; k++) sum += (change[j] >> k) % 2; if (min == -1) min = sum; else min = min < sum ? min : sum; } } if (min != -1) cout << min << endl; else cout << "inf" << endl; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator