//GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define all(x) x.begin(), x.end() //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 2000 + 10; int n, m; char s[nax][nax]; int row[nax][26], col[nax][26]; int row0[nax], col0[nax]; bool visR[nax], visC[nax]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) { string b; cin >> b; for (int j = 0; j < m; ++j) { s[i][j] = b[j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { row[i][s[i][j] - 'A']++; col[j][s[i][j] - 'A']++; } } for (int i = 0; i < n; ++i) { for (int l = 0; l < 26; ++l) { if (row[i][l] != 0) row0[i]++; } } for (int i = 0; i < m; ++i) { for (int l = 0; l < 26; ++l) { if (col[i][l] != 0) col0[i]++; } } queue<int>qr, qc; for (int i = 0; i < n; ++i) if (row0[i] == 1) { qr.push(i); visR[i] = true; } for (int i = 0; i < m; ++i) if (col0[i] == 1) { qc.push(i); visC[i] = true; } vector<tuple<char, int, char>> events; while (!qr.empty() || !qc.empty()) { while (!qr.empty()) { int i = qr.front(); qr.pop(); char col1 = 'A'; for (int j = 0; j < m; ++j) { if (s[i][j] != '*') { col1 = s[i][j]; row[i][s[i][j] - 'A']--; col[j][s[i][j] - 'A']--; if (col[j][s[i][j] - 'A'] == 0) col0[j]--; if (!visC[j] && col0[j] <= 1) { visC[j] = true; qc.push(j); } s[i][j] = '*'; } } events.emplace_back('R', i + 1, col1); } while (!qc.empty()) { int j = qc.front(); qc.pop(); char col1 = 'A'; for (int i = 0; i < n; ++i) { if (s[i][j] != '*') { col1 = s[i][j]; row[i][s[i][j] - 'A']--; col[j][s[i][j] - 'A']--; if (row[i][s[i][j] - 'A'] == 0) row0[i]--; if (!visR[i] && row0[i] <= 1) { visR[i] = true; qr.push(i); } s[i][j] = '*'; } } events.emplace_back('K', j + 1, col1); } } reverse(all(events)); cout << (int)events.size() << "\n"; for (auto [a,b,c] : events) { cout << a << " " << b << " " << c << "\n"; } }
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 118 | //GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define all(x) x.begin(), x.end() //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 2000 + 10; int n, m; char s[nax][nax]; int row[nax][26], col[nax][26]; int row0[nax], col0[nax]; bool visR[nax], visC[nax]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) { string b; cin >> b; for (int j = 0; j < m; ++j) { s[i][j] = b[j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { row[i][s[i][j] - 'A']++; col[j][s[i][j] - 'A']++; } } for (int i = 0; i < n; ++i) { for (int l = 0; l < 26; ++l) { if (row[i][l] != 0) row0[i]++; } } for (int i = 0; i < m; ++i) { for (int l = 0; l < 26; ++l) { if (col[i][l] != 0) col0[i]++; } } queue<int>qr, qc; for (int i = 0; i < n; ++i) if (row0[i] == 1) { qr.push(i); visR[i] = true; } for (int i = 0; i < m; ++i) if (col0[i] == 1) { qc.push(i); visC[i] = true; } vector<tuple<char, int, char>> events; while (!qr.empty() || !qc.empty()) { while (!qr.empty()) { int i = qr.front(); qr.pop(); char col1 = 'A'; for (int j = 0; j < m; ++j) { if (s[i][j] != '*') { col1 = s[i][j]; row[i][s[i][j] - 'A']--; col[j][s[i][j] - 'A']--; if (col[j][s[i][j] - 'A'] == 0) col0[j]--; if (!visC[j] && col0[j] <= 1) { visC[j] = true; qc.push(j); } s[i][j] = '*'; } } events.emplace_back('R', i + 1, col1); } while (!qc.empty()) { int j = qc.front(); qc.pop(); char col1 = 'A'; for (int i = 0; i < n; ++i) { if (s[i][j] != '*') { col1 = s[i][j]; row[i][s[i][j] - 'A']--; col[j][s[i][j] - 'A']--; if (row[i][s[i][j] - 'A'] == 0) row0[i]--; if (!visR[i] && row0[i] <= 1) { visR[i] = true; qr.push(i); } s[i][j] = '*'; } } events.emplace_back('K', j + 1, col1); } } reverse(all(events)); cout << (int)events.size() << "\n"; for (auto [a,b,c] : events) { cout << a << " " << b << " " << c << "\n"; } } |