#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int M = 2013; char s[M][M]; int u[M][2][26]; struct ruch { int t, i, k; }; int main() { int n,m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", s[i]); for (int j = 0; j < m; j++) { u[i][0][s[i][j]-'A']++; u[j][1][s[i][j]-'A']++; } } vector<ruch> ret; for(bool on = true; on; ) { on = false; for (int i = 0; i < n; i++) { vector<int> q; for (int k = 0; k < 26 && q.size() < 2; k++) { if (u[i][0][k]) q.push_back(k); } if (q.size() == 2) on = true; if (q.size() == 1) { ret.push_back({0, i, q[0]}); u[i][0][q[0]] = 0; for (int j = 0; j < m; j++) if(s[i][j] == 'A' + q[0]) { s[i][j] = '.'; u[j][1][q[0]]--; } } } for (int j = 0; j < m; j++) { vector<int> q; for (int k = 0; k < 26 && q.size() < 2; k++) { if (u[j][1][k]) q.push_back(k); } if (q.size() == 2) on = true; if (q.size() == 1) { ret.push_back({1, j, q[0]}); u[j][1][q[0]] = 0; for (int i = 0; i < n; i++) if (s[i][j] == 'A' + q[0]) { s[i][j] = '.'; u[i][0][q[0]]--; } } } } printf("%d\n", (int)ret.size()); reverse(ret.begin(), ret.end()); for (auto r : ret) { printf("%c %d %c\n", r.t ? 'K' : 'R', r.i+1, 'A'+r.k); } 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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int M = 2013; char s[M][M]; int u[M][2][26]; struct ruch { int t, i, k; }; int main() { int n,m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", s[i]); for (int j = 0; j < m; j++) { u[i][0][s[i][j]-'A']++; u[j][1][s[i][j]-'A']++; } } vector<ruch> ret; for(bool on = true; on; ) { on = false; for (int i = 0; i < n; i++) { vector<int> q; for (int k = 0; k < 26 && q.size() < 2; k++) { if (u[i][0][k]) q.push_back(k); } if (q.size() == 2) on = true; if (q.size() == 1) { ret.push_back({0, i, q[0]}); u[i][0][q[0]] = 0; for (int j = 0; j < m; j++) if(s[i][j] == 'A' + q[0]) { s[i][j] = '.'; u[j][1][q[0]]--; } } } for (int j = 0; j < m; j++) { vector<int> q; for (int k = 0; k < 26 && q.size() < 2; k++) { if (u[j][1][k]) q.push_back(k); } if (q.size() == 2) on = true; if (q.size() == 1) { ret.push_back({1, j, q[0]}); u[j][1][q[0]] = 0; for (int i = 0; i < n; i++) if (s[i][j] == 'A' + q[0]) { s[i][j] = '.'; u[i][0][q[0]]--; } } } } printf("%d\n", (int)ret.size()); reverse(ret.begin(), ret.end()); for (auto r : ret) { printf("%c %d %c\n", r.t ? 'K' : 'R', r.i+1, 'A'+r.k); } return 0; } |