#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef pair <int,ii> iii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 2005; int n, m, i, j, r[N][26], c[N][26], tab[N][N], visR[N], visC[N], ind, w; char t[N]; queue<int> qR, qC; vector<iii> res; bool isRowOk(int a) { int l0 = 0; for (int i=0;i<26;i++) if (r[a][i] == 0) l0++; return l0 == 25; } bool isColOk(int a) { int l0 = 0; for (int i=0;i<26;i++) if (c[a][i] == 0) l0++; return l0 == 25; } int main() { scanf("%d %d", &n, &m); for (i=0;i<n;i++) { scanf("%s", t); for (j=0;j<m;j++) tab[i][j] = int(t[j]) - 'A'; } for (i=0;i<n;i++) for (j=0;j<m;j++) { r[i][tab[i][j]]++; c[j][tab[i][j]]++; } for (i=0;i<n;i++) if (isRowOk(i)) { visR[i] = 1; qR.push(i); } for (i=0;i<m;i++) if (isColOk(i)) { visC[i] = 1; qC.push(i); } while (qR.size() > 0 || qC.size() > 0) { while (qR.size() > 0) { w = qR.front(); qR.pop(); ind = -1; for (i=0;i<26;i++) if (r[w][i] != 0) ind = i; if (ind == -1) continue; res.pb(iii(0, ii(w, ind))); for (i=0;i<m;i++) { c[i][tab[w][i]]--; if (!visC[i] && isColOk(i)) { visC[i] = 1; qC.push(i); } } } while (qC.size() > 0) { w = qC.front(); qC.pop(); ind = -1; for (i=0;i<26;i++) if (c[w][i] != 0) ind = i; if (ind == -1) continue; res.pb(iii(1, ii(w, ind))); for (i=0;i<n;i++) { r[i][tab[i][w]]--; if (!visR[i] && isRowOk(i)) { visR[i] = 1; qR.push(i); } } } } reverse(res.begin(), res.end()); printf("%d\n", res.size()); for (auto &w : res) { if (w.first == 0) printf("R "); else printf("K "); printf("%d %c\n", w.second.first + 1, char(w.second.second + 65)); } return 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 80 81 82 83 84 85 86 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef pair <int,ii> iii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 2005; int n, m, i, j, r[N][26], c[N][26], tab[N][N], visR[N], visC[N], ind, w; char t[N]; queue<int> qR, qC; vector<iii> res; bool isRowOk(int a) { int l0 = 0; for (int i=0;i<26;i++) if (r[a][i] == 0) l0++; return l0 == 25; } bool isColOk(int a) { int l0 = 0; for (int i=0;i<26;i++) if (c[a][i] == 0) l0++; return l0 == 25; } int main() { scanf("%d %d", &n, &m); for (i=0;i<n;i++) { scanf("%s", t); for (j=0;j<m;j++) tab[i][j] = int(t[j]) - 'A'; } for (i=0;i<n;i++) for (j=0;j<m;j++) { r[i][tab[i][j]]++; c[j][tab[i][j]]++; } for (i=0;i<n;i++) if (isRowOk(i)) { visR[i] = 1; qR.push(i); } for (i=0;i<m;i++) if (isColOk(i)) { visC[i] = 1; qC.push(i); } while (qR.size() > 0 || qC.size() > 0) { while (qR.size() > 0) { w = qR.front(); qR.pop(); ind = -1; for (i=0;i<26;i++) if (r[w][i] != 0) ind = i; if (ind == -1) continue; res.pb(iii(0, ii(w, ind))); for (i=0;i<m;i++) { c[i][tab[w][i]]--; if (!visC[i] && isColOk(i)) { visC[i] = 1; qC.push(i); } } } while (qC.size() > 0) { w = qC.front(); qC.pop(); ind = -1; for (i=0;i<26;i++) if (c[w][i] != 0) ind = i; if (ind == -1) continue; res.pb(iii(1, ii(w, ind))); for (i=0;i<n;i++) { r[i][tab[i][w]]--; if (!visR[i] && isRowOk(i)) { visR[i] = 1; qR.push(i); } } } } reverse(res.begin(), res.end()); printf("%d\n", res.size()); for (auto &w : res) { if (w.first == 0) printf("R "); else printf("K "); printf("%d %c\n", w.second.first + 1, char(w.second.second + 65)); } return 0; } |