题意:给两个个01矩阵,有两种操作,(1)交换两列(2)反转某一行。求能否通过若干操作使两矩阵相等
思路:(把所有对B的操作放到A上来,这一定是可以做到一样的效果的)枚举B矩阵的第一列对应A矩阵的第几列,交换这两列,那么根据两个矩阵这一列的相等情况可以确定每一行的操作情况(操作次数实际上只有0和1两种情况),然后根据这个情况确定执行了所有行操作的A矩阵,然后从第二列开始到第m列,依次用A矩阵的某一列(这个也是枚举)去“匹配”B矩阵的当前列,匹配好了那么继续后面的匹配(任何一个可以匹配当前列的列他们是没有区别的,任取一个,所以我们取第一次出现的那一个)。这样操作直到完全匹配好。详见代码:
1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 #include <bitset> 17 #include <functional> 18 #include <numeric> 19 #include <stdexcept> 20 #include <utility> 21 22 using namespace std; 23 24 #define mem0(a) memset(a, 0, sizeof(a)) 25 #define lson l, m, rt << 1 26 #define rson m + 1, r, rt << 1 | 1 27 #define define_m int m = (l + r) >> 1 28 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 31 #define rep_down1(a, b) for (int a = b; a > 0; a--) 32 #define all(a) (a).begin(), (a).end() 33 #define lowbit(x) ((x) & (-(x))) 34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 37 #define pchr(a) putchar(a) 38 #define pstr(a) printf("%s", a) 39 #define sstr(a) scanf("%s", a); 40 #define sint(a) ReadInt(a) 41 #define sint2(a, b) ReadInt(a);ReadInt(b) 42 #define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c) 43 #define pint(a) WriteInt(a) 44 #define if_else(a, b, c) if (a) { b; } else { c; } 45 #define if_than(a, b) if (a) { b; } 46 #define test_print1(a) cout << "var1 = " << a << endl 47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = b" << ", var3 = " << c << endl 49 50 typedef double db; 51 typedef long long LL; 52 typedef pair<int, int> pii; 53 typedef multiset<int> msi; 54 typedef set<int> si; 55 typedef vector<int> vi; 56 typedef map<int, int> mii; 57 58 const int dx[8] = {0, 0, -1, 1}; 59 const int dy[8] = {-1, 1, 0, 0}; 60 const int maxn = 1e2 + 7; 61 const int maxm = 1e3 + 7; 62 const int maxv = 1e7 + 7; 63 const int max_val = 1e6 + 7; 64 const int MD = 22; 65 const int INF = 1e9 + 7; 66 const double pi = acos(-1.0); 67 const double eps = 1e-10; 68 69 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 70 template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=0;while(isdigit(c)){x=x*10+c-‘0‘;c=getchar();}} 71 template<class T>void WriteInt(T i) {int p=0;static int b[20];if(i == 0) b[p++] = 0;else while(i){b[p++]=i%10;i/=10;}for(int j=p-1;j>=0;j--)pchr(‘0‘+b[j]);} 72 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 73 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 74 template<class T>T condition(bool f, T a, T b){return f?a:b;} 75 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 76 int make_id(int x, int y, int n) { return x * n + y; } 77 78 int n, m; 79 int a[maxn][maxn], b[maxn][maxn], buf[maxn][maxn]; 80 81 int swap_col(int y1, int y2) { 82 rep_up0(i, n) { 83 swap(a[i][y1], a[i][y2]); 84 } 85 } 86 87 int equal_col(int y1, int y2) { 88 rep_up0(i, n) { 89 if (a[i][y1] != b[i][y2]) return false; 90 } 91 return true; 92 } 93 94 int copy_rec() { 95 rep_up0(i, n) { 96 rep_up0(j, m) { 97 a[i][j] = buf[i][j]; 98 } 99 } 100 } 101 int copy_rec2() { 102 rep_up0(i, n) { 103 rep_up0(j, m) { 104 buf[i][j] = a[i][j]; 105 } 106 } 107 } 108 109 int main() { 110 //freopen("in.txt", "r", stdin); 111 //freopen("out.txt", "w", stdout); 112 while (cin >> n >> m, n >= 0 || m >= 0) { 113 rep_up0(i, n) { 114 rep_up0(j, m) { 115 sint(a[i][j]); 116 } 117 } 118 rep_up0(i, n) { 119 rep_up0(j, m) { 120 sint(b[i][j]); 121 } 122 } 123 int ok = 0; 124 copy_rec2(); 125 rep_up0(i, m) { 126 copy_rec(); 127 swap_col(0, i); 128 copy_rec2(); 129 int flag[100]; 130 rep_up0(j, n) { 131 flag[j] = a[j][0] != b[j][0]; 132 } 133 rep_up0(j, n) { 134 for (int k = 1; k < m; k++) { 135 a[j][k] ^= flag[j]; 136 } 137 } 138 int yes = 1; 139 for (int j = 1; j < m; j++) { 140 int ok0 = 0; 141 for (int k = j; k < m; k++) { 142 if (equal_col(k, j)) { 143 swap_col(k, j); 144 ok0 = 1; 145 break; 146 } 147 } 148 if (!ok0) { 149 yes = 0; 150 break; 151 } 152 } 153 if (yes) { 154 ok = 1; 155 break; 156 } 157 } 158 puts(ok? "Yes" : "No"); 159 } 160 return 0; 161 }
时间: 2024-10-05 15:53:51