#include <cstdio> #include <cstdint> #include <vector> #include <map> #include <queue> using namespace std; int main () { int n, m; scanf("%d %d\n", &n, &m); vector<vector<char> > board(n, vector<char>(m, 0)); vector<map<char, int> > rows(n, map<char, int>()); vector<map<char, int> > cols(m, map<char, int>()); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { char c; scanf("%c", &c); board[i][j] = c; if (rows[i].find(c) == rows[i].end()) { rows[i][c] = 1; } else { ++rows[i][c]; } if (cols[j].find(c) == cols[j].end()) { cols[j][c] = 1; } else { ++cols[j][c]; } } scanf("\n"); } queue<pair<bool, int> > q; for (int i = 0; i < n; ++i) { if (rows[i].size() == 1) { q.push(make_pair(true, i)); } } for (int j = 0; j < m; ++j) { if (cols[j].size() == 1) { q.push(make_pair(false, j)); } } vector<pair<bool, pair<int, char> > > res; while (!q.empty()) { pair<bool, int> item = q.front(); q.pop(); if (item.first) { int row = item.second; int color = rows[row].begin()->first; if (color == 0) { continue; } res.push_back(make_pair(true, make_pair(row, color))); for (int j = 0; j < m; ++j) { board[row][j] = 0; --cols[j][color]; if (cols[j][color] == 0) { cols[j].erase(color); if (cols[j].size() == 1) { q.push(make_pair(false, j)); } } } } else { int col = item.second; int color = cols[col].begin()->first; if (color == 0) { continue; } res.push_back(make_pair(false, make_pair(col, color))); for (int i = 0; i < n; ++i) { board[i][col] = 0; --rows[i][color]; if (rows[i][color] == 0) { rows[i].erase(color); if (rows[i].size() == 1) { q.push(make_pair(true, i)); } } } } } printf("%d\n", res.size()); for (vector<pair<bool, pair<int, char> > >::reverse_iterator it = res.rbegin(); it != res.rend(); it++) { printf("%c %d %c\n", it->first ? 'R' : 'K', it->second.first + 1, it->second.second); } 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 <cstdio> #include <cstdint> #include <vector> #include <map> #include <queue> using namespace std; int main () { int n, m; scanf("%d %d\n", &n, &m); vector<vector<char> > board(n, vector<char>(m, 0)); vector<map<char, int> > rows(n, map<char, int>()); vector<map<char, int> > cols(m, map<char, int>()); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { char c; scanf("%c", &c); board[i][j] = c; if (rows[i].find(c) == rows[i].end()) { rows[i][c] = 1; } else { ++rows[i][c]; } if (cols[j].find(c) == cols[j].end()) { cols[j][c] = 1; } else { ++cols[j][c]; } } scanf("\n"); } queue<pair<bool, int> > q; for (int i = 0; i < n; ++i) { if (rows[i].size() == 1) { q.push(make_pair(true, i)); } } for (int j = 0; j < m; ++j) { if (cols[j].size() == 1) { q.push(make_pair(false, j)); } } vector<pair<bool, pair<int, char> > > res; while (!q.empty()) { pair<bool, int> item = q.front(); q.pop(); if (item.first) { int row = item.second; int color = rows[row].begin()->first; if (color == 0) { continue; } res.push_back(make_pair(true, make_pair(row, color))); for (int j = 0; j < m; ++j) { board[row][j] = 0; --cols[j][color]; if (cols[j][color] == 0) { cols[j].erase(color); if (cols[j].size() == 1) { q.push(make_pair(false, j)); } } } } else { int col = item.second; int color = cols[col].begin()->first; if (color == 0) { continue; } res.push_back(make_pair(false, make_pair(col, color))); for (int i = 0; i < n; ++i) { board[i][col] = 0; --rows[i][color]; if (rows[i][color] == 0) { rows[i].erase(color); if (rows[i].size() == 1) { q.push(make_pair(true, i)); } } } } } printf("%d\n", res.size()); for (vector<pair<bool, pair<int, char> > >::reverse_iterator it = res.rbegin(); it != res.rend(); it++) { printf("%c %d %c\n", it->first ? 'R' : 'K', it->second.first + 1, it->second.second); } return 0; } |