题目大意:(熄灯问题),就是把1全变为0,变的时候附近的都要变
解题思路:我用的枚举,建议用高斯消元,这道题Kuangbin划分在简单搜索里的,搜索应该也能过
注:有IMPOSSIBLE的情况,开始交的时候忘考虑了
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 8 int OriginTile[16]; 9 int Tiling[16]; 10 int Res[16]; 11 12 int GetBit(int & n, int i) 13 { 14 return (n >> i) & 1; 15 } 16 17 void SetBit(int & n, int i, int d) 18 { 19 if (d) 20 n |= (1 << i); 21 else 22 n &= ~(1 << i); 23 } 24 25 void ReverseBit(int & n, int i) 26 { 27 n ^= (1 << i); 28 } 29 30 int main() 31 { 32 ios::sync_with_stdio(false); 33 34 int M, N; 35 cin >> M >> N; 36 for (int i = 0; i < M; ++i) 37 { 38 for (int j = 0; j < N; ++j) 39 { 40 int t; 41 cin >> t; 42 SetBit(OriginTile[i], j, t); 43 } 44 } 45 46 int times = 1 << N; 47 for (int k = 0; k < times; ++k) 48 { 49 memcpy(Tiling, OriginTile, sizeof(OriginTile)); 50 Res[0] = k; 51 52 for (int i = 0; i < M; ++i) 53 { 54 for (int j = 0; j < N; ++j) 55 { 56 if (GetBit(Res[i], j)) 57 { 58 if (j > 0) 59 ReverseBit(Tiling[i], j-1); 60 ReverseBit(Tiling[i], j); 61 if (j < N - 1) 62 ReverseBit(Tiling[i], j+1); 63 } 64 } 65 if (i < M-1) 66 { 67 Tiling[i+1] ^= Res[i]; 68 Res[i+1] = Tiling[i]; 69 } 70 } 71 72 if (Tiling[M-1] == 0) 73 { 74 for (int i = 0; i < M; ++i) 75 { 76 for (int j = 0; j < N; ++j) 77 { 78 cout << GetBit(Res[i], j); 79 if (j < N-1) 80 cout << ' '; 81 } 82 cout << endl; 83 } 84 85 return 0; 86 } 87 } 88 89 cout << "IMPOSSIBLE" << endl; 90 91 return 0; 92 }