#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : ", "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- struct Paint { bool is_row; int number; int color; Paint(bool is_row_, int number_, int color_) : is_row(is_row_), number(number_), color(color_) {} }; ostream& operator << (ostream &os, Paint p) { return os << (p.is_row ? 'R' : 'K') << ' ' << p.number + 1 << ' ' << char(p.color + 'A'); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; constexpr int C = 26; vector<array<int, C + 1>> row_colors(n), col_colors(m); REP (c, C + 1) { REP (i, n) row_colors[i][c] = 0; REP (j, m) col_colors[j][c] = 0; } REP (i, n) REP (j, m) { char c; cin >> c; c -= 'A'; ++row_colors[i][c]; ++col_colors[j][c]; } deque<Paint> ready_to_paint; vector<bool> row_done(n, false), col_done(m, false); REP (c, C) { REP (i, n) { if (row_colors[i][c] == m) { ready_to_paint.emplace_back(true, i, c); row_done[i] = true; } } REP (j, m) { if (col_colors[j][c] == n) { ready_to_paint.emplace_back(false, j, c); col_done[j] = true; } } } debug(ready_to_paint); vector<Paint> answer; auto paint = [&](Paint p) { auto [is_row, number, color] = p; answer.emplace_back(is_row, number, color); if (is_row) { REP (j, m) { --col_colors[j][color]; ++col_colors[j].back(); } REP (j, m) if (!col_done[j]) { REP (c, C) { if (col_colors[j][c] + col_colors[j].back() == n) { ready_to_paint.emplace_back(false, j, c); col_done[j] = true; } } } } else { REP (i, n) { --row_colors[i][color]; ++row_colors[i].back(); } REP (i, n) if (!row_done[i]) { REP (c, C) { if (row_colors[i][c] + row_colors[i].back() == m) { ready_to_paint.emplace_back(true, i, c); row_done[i] = true; } } } } }; while (!ready_to_paint.empty()) { paint(ready_to_paint.front()); ready_to_paint.pop_front(); } reverse(answer.begin(), answer.end()); cout << ssize(answer) << '\n'; for (Paint p : answer) cout << p << '\n'; }
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : ", "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- struct Paint { bool is_row; int number; int color; Paint(bool is_row_, int number_, int color_) : is_row(is_row_), number(number_), color(color_) {} }; ostream& operator << (ostream &os, Paint p) { return os << (p.is_row ? 'R' : 'K') << ' ' << p.number + 1 << ' ' << char(p.color + 'A'); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; constexpr int C = 26; vector<array<int, C + 1>> row_colors(n), col_colors(m); REP (c, C + 1) { REP (i, n) row_colors[i][c] = 0; REP (j, m) col_colors[j][c] = 0; } REP (i, n) REP (j, m) { char c; cin >> c; c -= 'A'; ++row_colors[i][c]; ++col_colors[j][c]; } deque<Paint> ready_to_paint; vector<bool> row_done(n, false), col_done(m, false); REP (c, C) { REP (i, n) { if (row_colors[i][c] == m) { ready_to_paint.emplace_back(true, i, c); row_done[i] = true; } } REP (j, m) { if (col_colors[j][c] == n) { ready_to_paint.emplace_back(false, j, c); col_done[j] = true; } } } debug(ready_to_paint); vector<Paint> answer; auto paint = [&](Paint p) { auto [is_row, number, color] = p; answer.emplace_back(is_row, number, color); if (is_row) { REP (j, m) { --col_colors[j][color]; ++col_colors[j].back(); } REP (j, m) if (!col_done[j]) { REP (c, C) { if (col_colors[j][c] + col_colors[j].back() == n) { ready_to_paint.emplace_back(false, j, c); col_done[j] = true; } } } } else { REP (i, n) { --row_colors[i][color]; ++row_colors[i].back(); } REP (i, n) if (!row_done[i]) { REP (c, C) { if (row_colors[i][c] + row_colors[i].back() == m) { ready_to_paint.emplace_back(true, i, c); row_done[i] = true; } } } } }; while (!ready_to_paint.empty()) { paint(ready_to_paint.front()); ready_to_paint.pop_front(); } reverse(answer.begin(), answer.end()); cout << ssize(answer) << '\n'; for (Paint p : answer) cout << p << '\n'; } |