#include <bits/stdc++.h>
using namespace std;
int main(const int, char const * const * const) {
uint16_t n;
uint16_t m;
cin >> n;
cin >> m;
vector<char> board(n * m, 0);
vector<unordered_map<char, uint16_t>> colorsRows(n);
vector<unordered_map<char, uint16_t>> colorsCols(m);
for(uint16_t i = 0; i < n; ++i) {
for(uint16_t j = 0; j < m; ++j) {
char color;
cin >> color;
board[((size_t)i) * ((size_t)m) + ((size_t)j)] = color;
++colorsRows[i][color];
++colorsCols[j][color];
}
}
vector<pair<int16_t, char>> ans;
uint16_t doneRowsCount = 0;
uint16_t doneColsCount = 0;
vector<bool> doneRows(n, false);
vector<bool> doneCols(m, false);
while(doneRowsCount < n || doneColsCount < m) {
for(uint16_t row = 0; row < n; ++row) {
if(doneRows[row]) {
continue;
}
if(colorsRows[row].size() <= 1) {
++doneRowsCount;
doneRows[row] = true;
if(colorsRows[row].empty()) {
continue;
}
ans.push_back({row + 1, colorsRows[row].begin()->first});
const size_t rowNumber = ((size_t)row) * ((size_t)n);
for(uint16_t col = 0; col < m; ++col) {
const char color = board[rowNumber + ((size_t)col)];
if(!color) {
continue;
}
board[rowNumber + ((size_t)col)] = 0;
if((--colorsCols[col][color]) == 0) {
colorsCols[col].erase(color);
}
}
}
}
for(uint16_t col = 0; col < m; ++col) {
if(doneCols[col]) {
continue;
}
if(colorsCols[col].size() <= 1) {
++doneColsCount;
doneCols[col] = true;
if(colorsCols[col].empty()) {
continue;
}
ans.push_back({-(col + 1), colorsCols[col].begin()->first});
for(uint16_t row = 0; row < n; ++row) {
const char color = board[((size_t)row) * ((size_t)n) + ((size_t)col)];
if(!color) {
continue;
}
board[((size_t)row) * ((size_t)n) + ((size_t)col)] = 0;
if((--colorsRows[row][color]) == 0) {
colorsRows[row].erase(color);
}
}
}
}
}
cout << ans.size() << '\n';
for(auto iter = ans.rbegin(); iter != ans.rend(); ++iter) {
cout << (iter->first < 0 ? 'K' : 'R')<< ' ' << abs(iter->first)<< ' ' << iter->second<<'\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 | #include <bits/stdc++.h> using namespace std; int main(const int, char const * const * const) { uint16_t n; uint16_t m; cin >> n; cin >> m; vector<char> board(n * m, 0); vector<unordered_map<char, uint16_t>> colorsRows(n); vector<unordered_map<char, uint16_t>> colorsCols(m); for(uint16_t i = 0; i < n; ++i) { for(uint16_t j = 0; j < m; ++j) { char color; cin >> color; board[((size_t)i) * ((size_t)m) + ((size_t)j)] = color; ++colorsRows[i][color]; ++colorsCols[j][color]; } } vector<pair<int16_t, char>> ans; uint16_t doneRowsCount = 0; uint16_t doneColsCount = 0; vector<bool> doneRows(n, false); vector<bool> doneCols(m, false); while(doneRowsCount < n || doneColsCount < m) { for(uint16_t row = 0; row < n; ++row) { if(doneRows[row]) { continue; } if(colorsRows[row].size() <= 1) { ++doneRowsCount; doneRows[row] = true; if(colorsRows[row].empty()) { continue; } ans.push_back({row + 1, colorsRows[row].begin()->first}); const size_t rowNumber = ((size_t)row) * ((size_t)n); for(uint16_t col = 0; col < m; ++col) { const char color = board[rowNumber + ((size_t)col)]; if(!color) { continue; } board[rowNumber + ((size_t)col)] = 0; if((--colorsCols[col][color]) == 0) { colorsCols[col].erase(color); } } } } for(uint16_t col = 0; col < m; ++col) { if(doneCols[col]) { continue; } if(colorsCols[col].size() <= 1) { ++doneColsCount; doneCols[col] = true; if(colorsCols[col].empty()) { continue; } ans.push_back({-(col + 1), colorsCols[col].begin()->first}); for(uint16_t row = 0; row < n; ++row) { const char color = board[((size_t)row) * ((size_t)n) + ((size_t)col)]; if(!color) { continue; } board[((size_t)row) * ((size_t)n) + ((size_t)col)] = 0; if((--colorsRows[row][color]) == 0) { colorsRows[row].erase(color); } } } } } cout << ans.size() << '\n'; for(auto iter = ans.rbegin(); iter != ans.rend(); ++iter) { cout << (iter->first < 0 ? 'K' : 'R')<< ' ' << abs(iter->first)<< ' ' << iter->second<<'\n'; } return 0; } |
English