#include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; const int N = 2007; struct Ruch { char typ; int nr; char kolor; Ruch(char t, int nr, char kol) { this->typ = t; this->nr = nr; this->kolor = kol; } }; int n, m; string rys[N]; vector<Ruch> out; queue<Ruch> q; int rkolory[N][30], kkolory[N][30]; int radded[N], kadded[N]; char checkrow(int r) { int ile = 0; char kol = 0; for (int i = 'A'; i <= 'Z'; i++) { if (rkolory[r][i - 'A'] > 0) { ile++; kol = i; } } return ile == 1 ? kol : 0; } char checkkol(int k) { int ile = 0; char kol = 0; for (int i = 'A'; i <= 'Z'; i++) { if (kkolory[k][i - 'A'] > 0) { ile++; kol = i; } } return ile == 1 ? kol : 0; } void processrow(int r, char kol) { for (int k = 0; k < m; k++) { if (rys[r][k] == kol) { rys[r][k] = '?'; rkolory[r][kol - 'A']--; kkolory[k][kol - 'A']--; if (!kadded[k]) { char c = checkkol(k); if (c != 0) { kadded[k] = true; q.emplace('K', k, c); } } } } } void processcol(int k, char kol) { for (int r = 0; r < n; r++) { if (rys[r][k] == kol) { rys[r][k] = '?'; rkolory[r][kol - 'A']--; kkolory[k][kol - 'A']--; if (!radded[r]) { char c = checkrow(r); if (c != 0) { radded[r] = true; q.emplace('R', r, c); } } } } } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> rys[i]; } for (int r = 0; r < n; r++) { for (int k = 0; k < m; k++) { int kol = rys[r][k] - 'A'; rkolory[r][kol]++; kkolory[k][kol]++; } } for (int r = 0; r < n; r++) { char kol = checkrow(r); if (kol != 0) { radded[r] = true; q.emplace('R', r, kol); } } for (int k = 0; k < m; k++) { char kol = checkkol(k); if (kol != 0) { kadded[k] = true; q.emplace('K', k, kol); } } while (!q.empty()) { Ruch ruch = q.front(); q.pop(); out.emplace_back(ruch.typ, ruch.nr + 1, ruch.kolor); if (ruch.typ == 'R') { processrow(ruch.nr, ruch.kolor); } else { processcol(ruch.nr, ruch.kolor); } } reverse(out.begin(), out.end()); // cout << "OK"; // return 0; cout << out.size() << endl; for (auto it : out) { cout << it.typ << " " << it.nr << " " << it.kolor << "\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 | #include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; const int N = 2007; struct Ruch { char typ; int nr; char kolor; Ruch(char t, int nr, char kol) { this->typ = t; this->nr = nr; this->kolor = kol; } }; int n, m; string rys[N]; vector<Ruch> out; queue<Ruch> q; int rkolory[N][30], kkolory[N][30]; int radded[N], kadded[N]; char checkrow(int r) { int ile = 0; char kol = 0; for (int i = 'A'; i <= 'Z'; i++) { if (rkolory[r][i - 'A'] > 0) { ile++; kol = i; } } return ile == 1 ? kol : 0; } char checkkol(int k) { int ile = 0; char kol = 0; for (int i = 'A'; i <= 'Z'; i++) { if (kkolory[k][i - 'A'] > 0) { ile++; kol = i; } } return ile == 1 ? kol : 0; } void processrow(int r, char kol) { for (int k = 0; k < m; k++) { if (rys[r][k] == kol) { rys[r][k] = '?'; rkolory[r][kol - 'A']--; kkolory[k][kol - 'A']--; if (!kadded[k]) { char c = checkkol(k); if (c != 0) { kadded[k] = true; q.emplace('K', k, c); } } } } } void processcol(int k, char kol) { for (int r = 0; r < n; r++) { if (rys[r][k] == kol) { rys[r][k] = '?'; rkolory[r][kol - 'A']--; kkolory[k][kol - 'A']--; if (!radded[r]) { char c = checkrow(r); if (c != 0) { radded[r] = true; q.emplace('R', r, c); } } } } } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> rys[i]; } for (int r = 0; r < n; r++) { for (int k = 0; k < m; k++) { int kol = rys[r][k] - 'A'; rkolory[r][kol]++; kkolory[k][kol]++; } } for (int r = 0; r < n; r++) { char kol = checkrow(r); if (kol != 0) { radded[r] = true; q.emplace('R', r, kol); } } for (int k = 0; k < m; k++) { char kol = checkkol(k); if (kol != 0) { kadded[k] = true; q.emplace('K', k, kol); } } while (!q.empty()) { Ruch ruch = q.front(); q.pop(); out.emplace_back(ruch.typ, ruch.nr + 1, ruch.kolor); if (ruch.typ == 'R') { processrow(ruch.nr, ruch.kolor); } else { processcol(ruch.nr, ruch.kolor); } } reverse(out.begin(), out.end()); // cout << "OK"; // return 0; cout << out.size() << endl; for (auto it : out) { cout << it.typ << " " << it.nr << " " << it.kolor << "\n"; } return 0; } |