标签:open brothers c代码 blank shu line space 直接 art
这题有人用毒瘤的高斯消元做,也有人DFS,BFS,还有人证明性质然后直接计算,而我选择了最暴力的方法:
2^16枚举!
一算复杂度发现最坏情况是65536×16×4,无压力......
秒A
1 #include <cstdio> 2 using namespace std; 3 4 bool a[4][4], d[4][4]; 5 6 inline bool open() { 7 for(int i = 0; i <= 3; i++) { 8 for(int j = 0; j <= 3; j++) { 9 if(a[i][j]) { 10 return 0; 11 } 12 } 13 } 14 return 1; 15 } 16 17 inline void click(int k) { 18 int x = k / 4, y = k % 4; 19 for(int i = 0; i <= 3; i++) { 20 a[i][y] ^= 1; 21 a[x][i] ^= 1; 22 } 23 a[x][y] ^=1; 24 return; 25 } 26 27 inline void out(int k) { 28 int ans = 0; 29 for(int i = 0; i <= 15; i++) { 30 if(k & (1 << i)) { 31 ans++; 32 } 33 } 34 printf("%d\n", ans); 35 for(int i = 0; i <= 15; i++) { 36 if(k & (1 << i)) { 37 printf("%d %d\n", i / 4 + 1, i % 4 + 1); 38 } 39 } 40 return; 41 } 42 43 int main() { 44 for(int i = 0; i <= 3; i++) { 45 for(int j = 0; j <= 3; j++) { 46 char c = getchar(); 47 while(c != ‘+‘ && c != ‘-‘) { 48 c = getchar(); 49 } 50 d[i][j] = (c == ‘+‘); 51 } 52 } 53 for(int i = 0; i < (1 << 16); i++) { 54 for(int j = 0; j <= 3; j++) { 55 for(int k = 0; k <= 3; k++) { 56 a[j][k] = d[j][k]; 57 } 58 } 59 for(int j = 0; j <= 15; j++) { 60 if(i & (1 << j)) { 61 click(j); 62 } 63 } 64 if(open()) { 65 out(i); 66 return 0; 67 } 68 } 69 printf("No solution!"); 70 return 0; 71 }
poj2965 The Pilots Brothers' refrigerator
标签:open brothers c代码 blank shu line space 直接 art
原文地址:https://www.cnblogs.com/huyufeifei/p/9019104.html