#include<bits/stdc++.h> using namespace std; int n, m; char t[2007]; string tab[2004]; int rows[2004][27]; set<int> row_colors[2004]; int columns[2004][27]; set<int> column_colors[2004]; int tmp_char; queue<pair<int, int>> q; vector<pair<char, pair<int, char>>> results; char rowcol; int main() { scanf("%d%d",&n, &m); for (int i = 0; i < n; i++) { scanf("%s", t); tab[i] = t; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++){ tmp_char = tab[i][j] - 'A'; if (rows[i][tmp_char] == 0){ row_colors[i].insert(tmp_char); } rows[i][tmp_char] += 1; if (columns[j][tmp_char] == 0) { column_colors[j].insert(tmp_char); } columns[j][tmp_char] += 1; } } for (int i = 0; i < n; i++){ if (row_colors[i].size() == 1){ const int& x = *(row_colors[i].begin()); q.push(make_pair<int,int>(i + 1, (int)(x))); } } for (int i = 0; i < m; i++){ if (column_colors[i].size() == 1){ const int& x = *(column_colors[i].begin()); q.push(make_pair<int,int>(-(i + 1), (int)(x))); } } while (q.size()) { pair<int, int> a = q.front(); q.pop(); if (a.first > 0) { a.first -= 1; rowcol = 'R'; } else { a.first = -a.first; a.first -= 1; rowcol = 'K'; } results.push_back(make_pair<char, pair<int, char>>( (char)(rowcol), make_pair<int, char>(a.first + 1, a.second + 'A'))); if (rowcol == 'R'){ for (int i = 0; i < m; i++) { columns[i][a.second] -= 1; if (columns[i][a.second] == 0) { column_colors[i].erase(a.second); if (column_colors[i].size() == 1) { const int& x = *column_colors[i].begin(); q.push(make_pair<int, int>(-(i+1), (int)(x))); } } } } else { for (int i = 0; i < n; i++) { rows[i][a.second] -= 1; if (rows[i][a.second] == 0) { row_colors[i].erase(a.second); if (row_colors[i].size() == 1) { const int& x = *row_colors[i].begin(); q.push(make_pair<int, int>(i+1, (int)(x))); } } } } } printf("%d\n", results.size()); for (int i = results.size() - 1; i >= 0; i--) { printf("%c %d %c\n", results[i].first, results[i].second.first, results[i].second.second); } }
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 | #include<bits/stdc++.h> using namespace std; int n, m; char t[2007]; string tab[2004]; int rows[2004][27]; set<int> row_colors[2004]; int columns[2004][27]; set<int> column_colors[2004]; int tmp_char; queue<pair<int, int>> q; vector<pair<char, pair<int, char>>> results; char rowcol; int main() { scanf("%d%d",&n, &m); for (int i = 0; i < n; i++) { scanf("%s", t); tab[i] = t; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++){ tmp_char = tab[i][j] - 'A'; if (rows[i][tmp_char] == 0){ row_colors[i].insert(tmp_char); } rows[i][tmp_char] += 1; if (columns[j][tmp_char] == 0) { column_colors[j].insert(tmp_char); } columns[j][tmp_char] += 1; } } for (int i = 0; i < n; i++){ if (row_colors[i].size() == 1){ const int& x = *(row_colors[i].begin()); q.push(make_pair<int,int>(i + 1, (int)(x))); } } for (int i = 0; i < m; i++){ if (column_colors[i].size() == 1){ const int& x = *(column_colors[i].begin()); q.push(make_pair<int,int>(-(i + 1), (int)(x))); } } while (q.size()) { pair<int, int> a = q.front(); q.pop(); if (a.first > 0) { a.first -= 1; rowcol = 'R'; } else { a.first = -a.first; a.first -= 1; rowcol = 'K'; } results.push_back(make_pair<char, pair<int, char>>( (char)(rowcol), make_pair<int, char>(a.first + 1, a.second + 'A'))); if (rowcol == 'R'){ for (int i = 0; i < m; i++) { columns[i][a.second] -= 1; if (columns[i][a.second] == 0) { column_colors[i].erase(a.second); if (column_colors[i].size() == 1) { const int& x = *column_colors[i].begin(); q.push(make_pair<int, int>(-(i+1), (int)(x))); } } } } else { for (int i = 0; i < n; i++) { rows[i][a.second] -= 1; if (rows[i][a.second] == 0) { row_colors[i].erase(a.second); if (row_colors[i].size() == 1) { const int& x = *row_colors[i].begin(); q.push(make_pair<int, int>(i+1, (int)(x))); } } } } } printf("%d\n", results.size()); for (int i = results.size() - 1; i >= 0; i--) { printf("%c %d %c\n", results[i].first, results[i].second.first, results[i].second.second); } } |