#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
void run() {
int n, m;
cin >> n >> m;
vector<string> grid(n);
each(row, grid) cin >> row;
auto get = [&](int i, int j) { return grid[i][j] - 'A'; };
vector<tuple<char, int, int>> ans;
vector<array<int, 26>> rowCnt(n), colCnt(m);
vi rowBad(n), colBad(m);
rep(i, 0, n) {
rep(j, 0, m) {
rowCnt[i][get(i,j)]++;
colCnt[j][get(i,j)]++;
}
}
rep(i, 0, n) rep(j, 0, 26) rowBad[i] += (rowCnt[i][j] > 0);
rep(i, 0, m) rep(j, 0, 26) colBad[i] += (colCnt[i][j] > 0);
for (bool ok = 1; ok;) {
ok = 0;
rep(i, 0, n) if (rowBad[i] == 1) {
int c = 0;
while (!rowCnt[i][c]) c++;
ans.pb({'R', i, c});
rowBad[i] = 0;
rep(j, 0, m) colBad[j] -= (--colCnt[j][get(i,j)] == 0);
ok = 1;
break;
}
if (!ok) rep(i, 0, m) if (colBad[i] == 1) {
int c = 0;
while (!colCnt[i][c]) c++;
ans.pb({'K', i, c});
colBad[i] = 0;
rep(j, 0, n) rowBad[j] -= (--rowCnt[j][get(j,i)] == 0);
ok = 1;
break;
}
}
reverse(all(ans));
cout << sz(ans) << '\n';
for (auto [t, i, j] : ans) {
cout << t << ' ' << i+1 << ' ' << char(j+'A') << '\n';
}
}
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(10);
run();
cout << flush; _Exit(0);
}
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 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run() { int n, m; cin >> n >> m; vector<string> grid(n); each(row, grid) cin >> row; auto get = [&](int i, int j) { return grid[i][j] - 'A'; }; vector<tuple<char, int, int>> ans; vector<array<int, 26>> rowCnt(n), colCnt(m); vi rowBad(n), colBad(m); rep(i, 0, n) { rep(j, 0, m) { rowCnt[i][get(i,j)]++; colCnt[j][get(i,j)]++; } } rep(i, 0, n) rep(j, 0, 26) rowBad[i] += (rowCnt[i][j] > 0); rep(i, 0, m) rep(j, 0, 26) colBad[i] += (colCnt[i][j] > 0); for (bool ok = 1; ok;) { ok = 0; rep(i, 0, n) if (rowBad[i] == 1) { int c = 0; while (!rowCnt[i][c]) c++; ans.pb({'R', i, c}); rowBad[i] = 0; rep(j, 0, m) colBad[j] -= (--colCnt[j][get(i,j)] == 0); ok = 1; break; } if (!ok) rep(i, 0, m) if (colBad[i] == 1) { int c = 0; while (!colCnt[i][c]) c++; ans.pb({'K', i, c}); colBad[i] = 0; rep(j, 0, n) rowBad[j] -= (--rowCnt[j][get(j,i)] == 0); ok = 1; break; } } reverse(all(ans)); cout << sz(ans) << '\n'; for (auto [t, i, j] : ans) { cout << t << ' ' << i+1 << ' ' << char(j+'A') << '\n'; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(0); } |
English