#include <bits/stdc++.h> using namespace std; struct odp { char ruch; int nr; char kolor; }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector <vector <char>> v(n, vector <char>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> v[i][j]; } } vector <set<char>> rows(n), cols(m); vector <vector <int>> row(n, vector <int>(200, 0)), col(m, vector <int>(200, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { row[i][v[i][j]]++; col[j][v[i][j]]++; rows[i].insert(v[i][j]); cols[j].insert(v[i][j]); } } queue <odp> q; for (int i = 0; i < n; i++) if (rows[i].size() == 1) q.push({'R', i, *rows[i].begin()}); for (int i = 0; i < m; i++) if (cols[i].size() == 1) q.push({'K', i, *cols[i].begin()}); vector <odp> ans; while (!q.empty()) { auto cur = q.front(); q.pop(); if (cur.ruch == 'R') { if (rows[cur.nr].size() == 0) continue; rows[cur.nr].clear(); } else { if (cols[cur.nr].size() == 0) continue; cols[cur.nr].clear(); } ans.push_back(cur); if (cur.ruch == 'R') { for (int i = 0; i < m; i++) { if (cols[i].size() == 0) continue; col[i][cur.kolor]--; if (col[i][cur.kolor] == 0) cols[i].erase(cur.kolor); if (cols[i].size() == 1) q.push({'K', i, *cols[i].begin()}); } } else { for (int i = 0; i < n; i++) { if (rows[i].size() == 0) continue; row[i][cur.kolor]--; if (row[i][cur.kolor] == 0) rows[i].erase(cur.kolor); if (rows[i].size() == 1) q.push({'R', i, *rows[i].begin()}); } } } cout << ans.size() << '\n'; for (int i = (int)ans.size() - 1; i >= 0; i--) cout << ans[i].ruch << ' ' << ans[i].nr + 1 << ' ' << ans[i].kolor << '\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 | #include <bits/stdc++.h> using namespace std; struct odp { char ruch; int nr; char kolor; }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector <vector <char>> v(n, vector <char>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> v[i][j]; } } vector <set<char>> rows(n), cols(m); vector <vector <int>> row(n, vector <int>(200, 0)), col(m, vector <int>(200, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { row[i][v[i][j]]++; col[j][v[i][j]]++; rows[i].insert(v[i][j]); cols[j].insert(v[i][j]); } } queue <odp> q; for (int i = 0; i < n; i++) if (rows[i].size() == 1) q.push({'R', i, *rows[i].begin()}); for (int i = 0; i < m; i++) if (cols[i].size() == 1) q.push({'K', i, *cols[i].begin()}); vector <odp> ans; while (!q.empty()) { auto cur = q.front(); q.pop(); if (cur.ruch == 'R') { if (rows[cur.nr].size() == 0) continue; rows[cur.nr].clear(); } else { if (cols[cur.nr].size() == 0) continue; cols[cur.nr].clear(); } ans.push_back(cur); if (cur.ruch == 'R') { for (int i = 0; i < m; i++) { if (cols[i].size() == 0) continue; col[i][cur.kolor]--; if (col[i][cur.kolor] == 0) cols[i].erase(cur.kolor); if (cols[i].size() == 1) q.push({'K', i, *cols[i].begin()}); } } else { for (int i = 0; i < n; i++) { if (rows[i].size() == 0) continue; row[i][cur.kolor]--; if (row[i][cur.kolor] == 0) rows[i].erase(cur.kolor); if (rows[i].size() == 1) q.push({'R', i, *rows[i].begin()}); } } } cout << ans.size() << '\n'; for (int i = (int)ans.size() - 1; i >= 0; i--) cout << ans[i].ruch << ' ' << ans[i].nr + 1 << ' ' << ans[i].kolor << '\n'; } |