#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, j) for (int i = 0; i < (int)(j); i++) #define st first #define nd second const int N = 2005; int row_cnt[N][30]; int col_cnt[N][30]; void TEST([[maybe_unused]] int testcase_i) { int n, m; cin >> n >> m; vector<string> board(n); rep(i, n) { cin >> board[i]; } const char EMPTY = 'Z' + 1; rep(i, n) { rep(j, m) { row_cnt[i][board[i][j] - 'A']++; col_cnt[j][board[i][j] - 'A']++; } } vector<tuple<char, int, char>> ops; int letters = n * m; while (letters > 0) { bool fd = false; rep(i, n) { rep(j, 26) { if (row_cnt[i][j] and row_cnt[i][j] + row_cnt[i][EMPTY - 'A'] == m) { rep(k, m) { if (board[i][k] != EMPTY) { row_cnt[i][j]--; col_cnt[k][j]--; row_cnt[i][EMPTY - 'A']++; col_cnt[k][EMPTY - 'A']++; board[i][k] = EMPTY; letters--; } } ops.emplace_back('R', i + 1, 'A' + j); fd = true; break; } } if (fd) { break; } } if (fd) { continue; } rep(i, m) { rep(j, 26) { if (col_cnt[i][j] and col_cnt[i][j] + col_cnt[i][EMPTY - 'A'] == n) { rep(k, n) { if (board[k][i] != EMPTY) { row_cnt[k][j]--; col_cnt[i][j]--; row_cnt[k][EMPTY - 'A']++; col_cnt[i][EMPTY - 'A']++; board[k][i] = EMPTY; letters--; } } ops.emplace_back('K', i + 1, 'A' + j); fd = true; break; } } if (fd) { break; } } assert(fd); } reverse(ops.begin(), ops.end()); cout << ops.size() << "\n"; for (auto [a, b, c] : ops) { cout << a << " " << b << " " << c << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << setprecision(20) << fixed; // srand(time(NULL)); int t = 1; // cin >> t; rep(i, t) { TEST(i); } 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, j) for (int i = 0; i < (int)(j); i++) #define st first #define nd second const int N = 2005; int row_cnt[N][30]; int col_cnt[N][30]; void TEST([[maybe_unused]] int testcase_i) { int n, m; cin >> n >> m; vector<string> board(n); rep(i, n) { cin >> board[i]; } const char EMPTY = 'Z' + 1; rep(i, n) { rep(j, m) { row_cnt[i][board[i][j] - 'A']++; col_cnt[j][board[i][j] - 'A']++; } } vector<tuple<char, int, char>> ops; int letters = n * m; while (letters > 0) { bool fd = false; rep(i, n) { rep(j, 26) { if (row_cnt[i][j] and row_cnt[i][j] + row_cnt[i][EMPTY - 'A'] == m) { rep(k, m) { if (board[i][k] != EMPTY) { row_cnt[i][j]--; col_cnt[k][j]--; row_cnt[i][EMPTY - 'A']++; col_cnt[k][EMPTY - 'A']++; board[i][k] = EMPTY; letters--; } } ops.emplace_back('R', i + 1, 'A' + j); fd = true; break; } } if (fd) { break; } } if (fd) { continue; } rep(i, m) { rep(j, 26) { if (col_cnt[i][j] and col_cnt[i][j] + col_cnt[i][EMPTY - 'A'] == n) { rep(k, n) { if (board[k][i] != EMPTY) { row_cnt[k][j]--; col_cnt[i][j]--; row_cnt[k][EMPTY - 'A']++; col_cnt[i][EMPTY - 'A']++; board[k][i] = EMPTY; letters--; } } ops.emplace_back('K', i + 1, 'A' + j); fd = true; break; } } if (fd) { break; } } assert(fd); } reverse(ops.begin(), ops.end()); cout << ops.size() << "\n"; for (auto [a, b, c] : ops) { cout << a << " " << b << " " << c << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << setprecision(20) << fixed; // srand(time(NULL)); int t = 1; // cin >> t; rep(i, t) { TEST(i); } return 0; } |