#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
const ll LINF = 1e18;
const int INF = 1e9;
char target[2001][2001];
typedef vector<unordered_map<char, int>> Counter;
struct Move {
char row_or_col;
size_t idx;
char color;
};
void generate_moves(Counter &cnt_row, Counter &cnt_col, vector<Move> &moves, char row_or_col) {
for (size_t i = 0; i < cnt_row.size(); ++i) {
if (cnt_row[i].size() == 1) {
char ch = cnt_row[i].begin()->first;
cnt_row[i].clear();
moves.push_back({row_or_col, i + 1, ch});
for (size_t j = 0; j < cnt_col.size(); ++j) {
char &target_char = row_or_col == 'R' ? target[i][j] : target[j][i];
if (target_char == ch && --cnt_col[j][ch] == 0) {
cnt_col[j].erase(ch);
}
target_char = 0;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
Counter cnt_row(n);
Counter cnt_col(m);
string s;
for (int i = 0; i < n; ++i) {
cin >> s;
for (int j = 0; j < m; ++j) {
target[i][j] = s[j];
++cnt_row[i][s[j]];
++cnt_col[j][s[j]];
}
}
size_t old_size = -1;
vector<Move> moves;
while (moves.size() != old_size) {
old_size = moves.size();
generate_moves(cnt_row, cnt_col, moves, 'R');
generate_moves(cnt_col, cnt_row, moves, 'K');
}
cout << moves.size() << '\n';
for (int i = moves.size() - 1; i >= 0; --i) {
Move &m = moves[i];
cout << m.row_or_col << ' ' << m.idx << ' ' << m.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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; char target[2001][2001]; typedef vector<unordered_map<char, int>> Counter; struct Move { char row_or_col; size_t idx; char color; }; void generate_moves(Counter &cnt_row, Counter &cnt_col, vector<Move> &moves, char row_or_col) { for (size_t i = 0; i < cnt_row.size(); ++i) { if (cnt_row[i].size() == 1) { char ch = cnt_row[i].begin()->first; cnt_row[i].clear(); moves.push_back({row_or_col, i + 1, ch}); for (size_t j = 0; j < cnt_col.size(); ++j) { char &target_char = row_or_col == 'R' ? target[i][j] : target[j][i]; if (target_char == ch && --cnt_col[j][ch] == 0) { cnt_col[j].erase(ch); } target_char = 0; } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; Counter cnt_row(n); Counter cnt_col(m); string s; for (int i = 0; i < n; ++i) { cin >> s; for (int j = 0; j < m; ++j) { target[i][j] = s[j]; ++cnt_row[i][s[j]]; ++cnt_col[j][s[j]]; } } size_t old_size = -1; vector<Move> moves; while (moves.size() != old_size) { old_size = moves.size(); generate_moves(cnt_row, cnt_col, moves, 'R'); generate_moves(cnt_col, cnt_row, moves, 'K'); } cout << moves.size() << '\n'; for (int i = moves.size() - 1; i >= 0; --i) { Move &m = moves[i]; cout << m.row_or_col << ' ' << m.idx << ' ' << m.color << '\n'; } return 0; } |
English