//#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char spk(...); template <class c> auto spk(c *a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if<sizeof spk<c>(0) != 1, stream &>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if<sizeof spk<c>(0) == 1, stream &>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream &operator<<(pair<a, b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream &operator<<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream &_dbg(const string &s, int i, int b) { return *this; } template <class c, class... cs> stream &_dbg(const string &s, int i, int b, c arg, cs... args) { if (i == (int)(s.size())) return (*this << ": " << arg); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') - (s[i] == ']') - (s[i] == '}'); return (s[i] == ',' && b == 0) ? (*this << ": " << arg << " ")._dbg(s, i + 1, b, args...) : (s[i] == ' ' ? *this : *this << s[i]) ._dbg(s, i + 1, b, arg, args...); } }; } // namespace debug #ifdef DEBUG #define dout debug::stream() #define dbg(...) \ ((dout << "line:" << __LINE__ << " >> ") \ ._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) #else #define dout #define dbg(...) #endif // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ const int N = 2e3 + 7; int row_letter_cnt[N][26]; int col_letter_cnt[N][26]; int row_diff_cnt[N]; int col_diff_cnt[N]; bool row_vis[N], col_vis[N]; struct Op { int nr; char dir; char letter; friend ostream &operator<<(ostream &os, const Op &op) { os << op.dir << " " << op.nr + 1 << " " << op.letter; return os; } }; int picture[N][N]; class Visualiser { public: Visualiser(int n, int m) : n(n), m(m) { picture.resize(n, vector<char>(m, '.')); } void Paint(const Op &op) { if (op.dir == 'K') { for (int i = 0; i < n; i++) { picture[i][op.nr] = op.letter; } } else { for (int i = 0; i < m; i++) { picture[op.nr][i] = op.letter; } } } void Show() { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << picture[i][j]; } cout << '\n'; } } private: int n, m; vector<vector<char>> picture; }; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { int l = s[j] - 'A'; picture[i][j] = l; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { row_diff_cnt[i] += row_letter_cnt[i][picture[i][j]] == 0; row_letter_cnt[i][picture[i][j]]++; } } for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { col_diff_cnt[j] += col_letter_cnt[j][picture[i][j]] == 0; col_letter_cnt[j][picture[i][j]]++; } } queue<pair<int, int>> Q; for (int i = 0; i < n; i++) { if (row_diff_cnt[i] == 1) { Q.push({i, picture[i][0]}); row_vis[i] = true; } } for (int j = 0; j < m; j++) { if (col_diff_cnt[j] == 1) { Q.push({j + n, picture[0][j]}); col_vis[j] = true; } } vector<Op> solution; while (!Q.empty()) { int v = Q.front().first; int l = Q.front().second; Q.pop(); if (v >= n) { col_letter_cnt[v - n][l] = 0; col_diff_cnt[v - n] = 0; solution.push_back({v - n, 'K', (char)(l + 'A')}); for (int i = 0; i < n; i++) { row_letter_cnt[i][l]--; row_diff_cnt[i] -= row_letter_cnt[i][l] == 0; if (row_vis[i] || row_diff_cnt[i] != 1) continue; for (int j = 0; j < 26; j++) { if (row_letter_cnt[i][j]) { Q.push({i, j}); break; } } row_vis[i] = true; } } else { solution.push_back({v, 'R', (char)(l + 'A')}); row_letter_cnt[v][l] = 0; row_diff_cnt[v] = 0; for (int i = 0; i < m; i++) { col_letter_cnt[i][l]--; col_diff_cnt[i] -= col_letter_cnt[i][l] == 0; if (col_vis[i] || col_diff_cnt[i] != 1) continue; for (int j = 0; j < 26; j++) { if (col_letter_cnt[i][j]) { Q.push({i + n, j}); break; } } col_vis[i] = true; } } } reverse(solution.begin(), solution.end()); cout << solution.size() << '\n'; for (const Op &op : solution) { cout << op << "\n"; } // Check // Visualiser visualiser(n, m); // for (const Op &op : solution) { // visualiser.Paint(op); // visualiser.Show(); // cout << '\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 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | //#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char spk(...); template <class c> auto spk(c *a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if<sizeof spk<c>(0) != 1, stream &>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if<sizeof spk<c>(0) == 1, stream &>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream &operator<<(pair<a, b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream &operator<<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream &_dbg(const string &s, int i, int b) { return *this; } template <class c, class... cs> stream &_dbg(const string &s, int i, int b, c arg, cs... args) { if (i == (int)(s.size())) return (*this << ": " << arg); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') - (s[i] == ']') - (s[i] == '}'); return (s[i] == ',' && b == 0) ? (*this << ": " << arg << " ")._dbg(s, i + 1, b, args...) : (s[i] == ' ' ? *this : *this << s[i]) ._dbg(s, i + 1, b, arg, args...); } }; } // namespace debug #ifdef DEBUG #define dout debug::stream() #define dbg(...) \ ((dout << "line:" << __LINE__ << " >> ") \ ._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) #else #define dout #define dbg(...) #endif // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ const int N = 2e3 + 7; int row_letter_cnt[N][26]; int col_letter_cnt[N][26]; int row_diff_cnt[N]; int col_diff_cnt[N]; bool row_vis[N], col_vis[N]; struct Op { int nr; char dir; char letter; friend ostream &operator<<(ostream &os, const Op &op) { os << op.dir << " " << op.nr + 1 << " " << op.letter; return os; } }; int picture[N][N]; class Visualiser { public: Visualiser(int n, int m) : n(n), m(m) { picture.resize(n, vector<char>(m, '.')); } void Paint(const Op &op) { if (op.dir == 'K') { for (int i = 0; i < n; i++) { picture[i][op.nr] = op.letter; } } else { for (int i = 0; i < m; i++) { picture[op.nr][i] = op.letter; } } } void Show() { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << picture[i][j]; } cout << '\n'; } } private: int n, m; vector<vector<char>> picture; }; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { int l = s[j] - 'A'; picture[i][j] = l; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { row_diff_cnt[i] += row_letter_cnt[i][picture[i][j]] == 0; row_letter_cnt[i][picture[i][j]]++; } } for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { col_diff_cnt[j] += col_letter_cnt[j][picture[i][j]] == 0; col_letter_cnt[j][picture[i][j]]++; } } queue<pair<int, int>> Q; for (int i = 0; i < n; i++) { if (row_diff_cnt[i] == 1) { Q.push({i, picture[i][0]}); row_vis[i] = true; } } for (int j = 0; j < m; j++) { if (col_diff_cnt[j] == 1) { Q.push({j + n, picture[0][j]}); col_vis[j] = true; } } vector<Op> solution; while (!Q.empty()) { int v = Q.front().first; int l = Q.front().second; Q.pop(); if (v >= n) { col_letter_cnt[v - n][l] = 0; col_diff_cnt[v - n] = 0; solution.push_back({v - n, 'K', (char)(l + 'A')}); for (int i = 0; i < n; i++) { row_letter_cnt[i][l]--; row_diff_cnt[i] -= row_letter_cnt[i][l] == 0; if (row_vis[i] || row_diff_cnt[i] != 1) continue; for (int j = 0; j < 26; j++) { if (row_letter_cnt[i][j]) { Q.push({i, j}); break; } } row_vis[i] = true; } } else { solution.push_back({v, 'R', (char)(l + 'A')}); row_letter_cnt[v][l] = 0; row_diff_cnt[v] = 0; for (int i = 0; i < m; i++) { col_letter_cnt[i][l]--; col_diff_cnt[i] -= col_letter_cnt[i][l] == 0; if (col_vis[i] || col_diff_cnt[i] != 1) continue; for (int j = 0; j < 26; j++) { if (col_letter_cnt[i][j]) { Q.push({i + n, j}); break; } } col_vis[i] = true; } } } reverse(solution.begin(), solution.end()); cout << solution.size() << '\n'; for (const Op &op : solution) { cout << op << "\n"; } // Check // Visualiser visualiser(n, m); // for (const Op &op : solution) { // visualiser.Paint(op); // visualiser.Show(); // cout << '\n'; // } return 0; } |