#include <iostream>
#include <vector>
struct Turn {
char direction;
int id;
char color;
};
const int N = 2007;
const int ROW = 0;
const int COL = 1;
int count[2][26][N];
int diff[2][N];
int lookup_diff(int j, int n) {
for (int i = 0; i < n; i++)
if (diff[j][i] == 1)
return i;
return -1;
}
int lookup_color(int j, int i) {
for (int k = 0; k < 26; k++)
if (count[j][k][i] > 0)
return k;
return -1;
}
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
int n[2];
std::cin >> n[0] >> n[1];
for (int i = 0; i < n[ROW]; i++) {
for (int j = 0; j < n[COL]; j++) {
char c;
std::cin >> c;
int k = c - 'A';
count[ROW][k][i]++;
count[COL][k][j]++;
if (count[ROW][k][i] == 1)
diff[ROW][i]++;
if (count[COL][k][j] == 1)
diff[COL][j]++;
}
}
std::vector<Turn> turns;
for (;;) {
int i = lookup_diff(ROW, n[ROW]);
int d = ROW;
if (i < 0) {
i = lookup_diff(COL, n[COL]);
d = COL;
if (i < 0)
break;
}
int k = lookup_color(d, i);
diff[d][i] = 0;
Turn t = {d == ROW ? 'R' : 'K', i + 1, static_cast<char>(k + 'A')};
turns.push_back(t);
for (int j = 0; j < n[1 - d]; j++) {
count[1 - d][k][j]--;
if (count[1 - d][k][j] == 0)
diff[1 - d][j]--;
}
}
std::cout << turns.size() << '\n';
for (int i = turns.size() - 1; i >= 0; i--)
std::cout << turns[i].direction << ' ' << turns[i].id << ' ' << turns[i].color << '\n';
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 87 88 89 90 91 92 | #include <iostream> #include <vector> struct Turn { char direction; int id; char color; }; const int N = 2007; const int ROW = 0; const int COL = 1; int count[2][26][N]; int diff[2][N]; int lookup_diff(int j, int n) { for (int i = 0; i < n; i++) if (diff[j][i] == 1) return i; return -1; } int lookup_color(int j, int i) { for (int k = 0; k < 26; k++) if (count[j][k][i] > 0) return k; return -1; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); int n[2]; std::cin >> n[0] >> n[1]; for (int i = 0; i < n[ROW]; i++) { for (int j = 0; j < n[COL]; j++) { char c; std::cin >> c; int k = c - 'A'; count[ROW][k][i]++; count[COL][k][j]++; if (count[ROW][k][i] == 1) diff[ROW][i]++; if (count[COL][k][j] == 1) diff[COL][j]++; } } std::vector<Turn> turns; for (;;) { int i = lookup_diff(ROW, n[ROW]); int d = ROW; if (i < 0) { i = lookup_diff(COL, n[COL]); d = COL; if (i < 0) break; } int k = lookup_color(d, i); diff[d][i] = 0; Turn t = {d == ROW ? 'R' : 'K', i + 1, static_cast<char>(k + 'A')}; turns.push_back(t); for (int j = 0; j < n[1 - d]; j++) { count[1 - d][k][j]--; if (count[1 - d][k][j] == 0) diff[1 - d][j]--; } } std::cout << turns.size() << '\n'; for (int i = turns.size() - 1; i >= 0; i--) std::cout << turns[i].direction << ' ' << turns[i].id << ' ' << turns[i].color << '\n'; return 0; } |
English