#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
const bool DBG = false;
struct cnt {
bool painted;
int a[26];
int& operator[](char c) {
return a[c - 'A'];
}
};
void print(cnt &c) {
for (char ch = 'A'; ch <= 'Z'; ++ch) {
cout << setw(4) << c[ch];
}
cout << '\n';
}
struct ruch {
char c;
int idx;
char color;
};
vector<ruch> response;
void solve();
int main() {
solve();
cout << response.size() << '\n';
reverse(response.begin(), response.end());
for (auto& r : response) {
cout << r.c << ' ' << r.idx << ' ' << r.color << '\n';
}
return 0;
}
void solve() {
int n, m;
cin >> n >> m;
vector<string> p;
p.resize(n);
for (int i = 0; i < n; ++i) {
cin >> p[i];
}
vector<cnt> kcnt;
kcnt.resize(m);
vector<cnt> wcnt;
wcnt.resize(n);
for (int w = 0; w < n; ++w) {
for (int k = 0; k < m; ++k) {
if (DBG) {
cout << w << ' ' << k << endl;
}
char c = p[w][k];
kcnt[k][c]++;
wcnt[w][c]++;
}
}
if (DBG) {
for (int w = 0; w < n; ++w) {
cout << "wiersz " << w << ":\n";
print(wcnt[w]);
}
}
int nc = n;
int mc = m;
while (true) {
for (int w = 0; w < n; ++w) {
cnt& c = wcnt[w];
if (c.painted) continue;
for (char ch = 'A'; ch <= 'Z'; ++ch) {
if (c[ch] == mc) {
// paint row
response.emplace_back(ruch{'R', w+1, ch});
//TODO
c.painted = true;
--nc;
if (nc == 0) return;
for (int k = 0; k < m; ++k) {
cnt& ck = kcnt[k];
ck[ch]--;
}
break;
}
}
}
for (int k = 0; k < m; ++k) {
cnt& c = kcnt[k];
if (c.painted) continue;
for (char ch = 'A'; ch <= 'Z'; ++ch) {
if (c[ch] == nc) {
// paint col
response.emplace_back(ruch{'K', k+1, ch});
c.painted = true;
--mc;
if (mc == 0) return;
for (int k = 0; k < n; ++k) {
cnt& ck = wcnt[k];
ck[ch]--;
}
break;
}
}
}
}
}
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 | #include <iostream> #include <vector> #include <string> #include <iomanip> #include <algorithm> using namespace std; const bool DBG = false; struct cnt { bool painted; int a[26]; int& operator[](char c) { return a[c - 'A']; } }; void print(cnt &c) { for (char ch = 'A'; ch <= 'Z'; ++ch) { cout << setw(4) << c[ch]; } cout << '\n'; } struct ruch { char c; int idx; char color; }; vector<ruch> response; void solve(); int main() { solve(); cout << response.size() << '\n'; reverse(response.begin(), response.end()); for (auto& r : response) { cout << r.c << ' ' << r.idx << ' ' << r.color << '\n'; } return 0; } void solve() { int n, m; cin >> n >> m; vector<string> p; p.resize(n); for (int i = 0; i < n; ++i) { cin >> p[i]; } vector<cnt> kcnt; kcnt.resize(m); vector<cnt> wcnt; wcnt.resize(n); for (int w = 0; w < n; ++w) { for (int k = 0; k < m; ++k) { if (DBG) { cout << w << ' ' << k << endl; } char c = p[w][k]; kcnt[k][c]++; wcnt[w][c]++; } } if (DBG) { for (int w = 0; w < n; ++w) { cout << "wiersz " << w << ":\n"; print(wcnt[w]); } } int nc = n; int mc = m; while (true) { for (int w = 0; w < n; ++w) { cnt& c = wcnt[w]; if (c.painted) continue; for (char ch = 'A'; ch <= 'Z'; ++ch) { if (c[ch] == mc) { // paint row response.emplace_back(ruch{'R', w+1, ch}); //TODO c.painted = true; --nc; if (nc == 0) return; for (int k = 0; k < m; ++k) { cnt& ck = kcnt[k]; ck[ch]--; } break; } } } for (int k = 0; k < m; ++k) { cnt& c = kcnt[k]; if (c.painted) continue; for (char ch = 'A'; ch <= 'Z'; ++ch) { if (c[ch] == nc) { // paint col response.emplace_back(ruch{'K', k+1, ch}); c.painted = true; --mc; if (mc == 0) return; for (int k = 0; k < n; ++k) { cnt& ck = wcnt[k]; ck[ch]--; } break; } } } } } |
English