#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2,tune=native") #pragma GCC target("sse,sse2,abm,mmx,sse3,tune=native") using namespace std; typedef long long lld; typedef pair<int, int> pii; typedef pair<lld, lld> pll; #define ff first #define dd second #define mp make_pair #define pb push_back #define sz size() #define For(i, s, a) for(int i = s; i < a; ++i) #define all(x) (x).begin(), (x).end() #define make_unique(x) (x).erase(unique(all(x)), (x).end()) const int N = 2e3 + 1; char s[N][N]; int ctx[128][N], cty[128][N], ilex[N], iley[N]; vector<tuple<char, int, char>> out; bool checkdone(int n, int m) { bool xd = 0; For (i, 0, n) xd |= ilex[i]; For (j, 0, m) xd |= iley[j]; return !xd; } int simple(int n, int m, int x, int y) { char z = 0; const int xb = x == -1 ? 0 : x, xe = x == -1 ? n : x + 1; const int yb = y == -1 ? 0 : y, ye = y == -1 ? m : y + 1; For (i, xb, xe) For (j, yb, ye) { z = s[i][j] ? s[i][j] : z; } if (!z) return 0; bool ok = 1; For (i, xb, xe) For (j, yb, ye) { ok &= (!s[i][j]) || (s[i][j] == z); } return ok ? z : -1; } int main(void) { int n, m; scanf("%d%d", &n, &m); For (i, 0, n) scanf("%s", s[i]); For (i, 0, n) For (j, 0, m) ctx[s[i][j]][i]++, cty[s[i][j]][j]++; For (i, 0, n) For (k, 0, 128) ilex[i] += ctx[k][i] > 0; For (j, 0, m) For (k, 0, 128) iley[j] += cty[k][j] > 0; while (true) { if (checkdone(n, m)) break; For (i, 0, n) if (ilex[i] == 1) { char xd = 0; For (k, 0, 128) if (ctx[k][i]) { xd = k; break; } out.push_back({'R', i + 1, xd}); For (j, 0, m) { cty[s[i][j]][j]--; if (!cty[s[i][j]][j]) iley[j]--; s[i][j] = 0; } ilex[i] = 0; } For (j, 0, m) if (iley[j] == 1) { char xd = 0; For (k, 0, 128) if (cty[k][j]) { xd = k; break; } out.push_back({'K', j + 1, xd}); For (i, 0, n) { ctx[s[i][j]][i]--; if (!ctx[s[i][j]][i]) ilex[i]--; s[i][j] = 0; } iley[j] = 0; } } reverse(all(out)); printf("%d\n", (int)out.size()); for (auto ss : out) printf("%c %d %c\n", get<0>(ss), get<1>(ss), get<2>(ss)); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2,tune=native") #pragma GCC target("sse,sse2,abm,mmx,sse3,tune=native") using namespace std; typedef long long lld; typedef pair<int, int> pii; typedef pair<lld, lld> pll; #define ff first #define dd second #define mp make_pair #define pb push_back #define sz size() #define For(i, s, a) for(int i = s; i < a; ++i) #define all(x) (x).begin(), (x).end() #define make_unique(x) (x).erase(unique(all(x)), (x).end()) const int N = 2e3 + 1; char s[N][N]; int ctx[128][N], cty[128][N], ilex[N], iley[N]; vector<tuple<char, int, char>> out; bool checkdone(int n, int m) { bool xd = 0; For (i, 0, n) xd |= ilex[i]; For (j, 0, m) xd |= iley[j]; return !xd; } int simple(int n, int m, int x, int y) { char z = 0; const int xb = x == -1 ? 0 : x, xe = x == -1 ? n : x + 1; const int yb = y == -1 ? 0 : y, ye = y == -1 ? m : y + 1; For (i, xb, xe) For (j, yb, ye) { z = s[i][j] ? s[i][j] : z; } if (!z) return 0; bool ok = 1; For (i, xb, xe) For (j, yb, ye) { ok &= (!s[i][j]) || (s[i][j] == z); } return ok ? z : -1; } int main(void) { int n, m; scanf("%d%d", &n, &m); For (i, 0, n) scanf("%s", s[i]); For (i, 0, n) For (j, 0, m) ctx[s[i][j]][i]++, cty[s[i][j]][j]++; For (i, 0, n) For (k, 0, 128) ilex[i] += ctx[k][i] > 0; For (j, 0, m) For (k, 0, 128) iley[j] += cty[k][j] > 0; while (true) { if (checkdone(n, m)) break; For (i, 0, n) if (ilex[i] == 1) { char xd = 0; For (k, 0, 128) if (ctx[k][i]) { xd = k; break; } out.push_back({'R', i + 1, xd}); For (j, 0, m) { cty[s[i][j]][j]--; if (!cty[s[i][j]][j]) iley[j]--; s[i][j] = 0; } ilex[i] = 0; } For (j, 0, m) if (iley[j] == 1) { char xd = 0; For (k, 0, 128) if (cty[k][j]) { xd = k; break; } out.push_back({'K', j + 1, xd}); For (i, 0, n) { ctx[s[i][j]][i]--; if (!ctx[s[i][j]][i]) ilex[i]--; s[i][j] = 0; } iley[j] = 0; } } reverse(all(out)); printf("%d\n", (int)out.size()); for (auto ss : out) printf("%c %d %c\n", get<0>(ss), get<1>(ss), get<2>(ss)); } |