// Online C++ compiler to run C++ program online
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <queue>
using namespace std;
struct pomalowanie {
bool is_wiersz;
int numer;
char kolor;
};
int main() {
int n, m;
cin >> n >> m;
unordered_set<char> wszystkie_kolory;
vector<unordered_map<char,int>> kolory_per_wiersz(n, unordered_map<char, int>());
vector<unordered_map<char,int>> kolory_per_kolumna(m, unordered_map<char, int>());
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
char c; cin >> c;
wszystkie_kolory.insert(c);
kolory_per_wiersz[i][c]++;
kolory_per_kolumna[j][c]++;
}
}
vector<pomalowanie> result;
queue<pair<bool,int>> kolejka; // is_wiersz, number
vector<bool> visited_wiersze(n, false);
vector<bool> visited_kolumny(m, false);
for(int i = 0; i < n; i++) {
if(kolory_per_wiersz[i].size() == 1) {
kolejka.push({true, i});
visited_wiersze[i] = true;
}
}
for(int i = 0; i < m; i++) {
if(kolory_per_kolumna[i].size() == 1) {
kolejka.push({false, i});
visited_kolumny[i] = true;
}
}
while(!kolejka.empty()) {
pair<bool,int> nowy = kolejka.front();
kolejka.pop();
bool is_wiersz = nowy.first;
int numer = nowy.second;
pomalowanie pomal;
pomal.is_wiersz = is_wiersz;
pomal.numer = numer;
pomal.kolor = *wszystkie_kolory.begin();
unordered_map<char,int>& kolory_tego_czegos = is_wiersz ? kolory_per_wiersz[numer] : kolory_per_kolumna[numer];
for(auto& tmp : kolory_tego_czegos) {
pomal.kolor = tmp.first;
}
result.push_back(pomal);
// update kolory innych wierszy/kolumn i wstaw do kolejki
char kolor = pomal.kolor;
if(is_wiersz) {
for(int i = 0; i < m; i++) if(!visited_kolumny[i]) {
unordered_map<char,int>& kolory = kolory_per_kolumna[i];
kolory[kolor]--;
if(kolory[kolor] == 0) {
kolory.erase(kolor);
}
if(kolory.size() <= 1) {
kolejka.push({false, i});
visited_kolumny[i] = true;
}
}
} else {
for(int i = 0; i < n; i++) if(!visited_wiersze[i]) {
unordered_map<char,int>& kolory = kolory_per_wiersz[i];
kolory[kolor]--;
if(kolory[kolor] == 0) {
kolory.erase(kolor);
}
if(kolory.size() <= 1) {
kolejka.push({true, i});
visited_wiersze[i] = true;
}
}
}
}
cout << result.size() << endl;
for(int i = result.size() - 1; i >= 0; i--) {
if(result[i].is_wiersz) {
cout << "R ";
} else cout << "K ";
cout << result[i].numer + 1 << " " << result[i].kolor << endl;
}
}
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 | // Online C++ compiler to run C++ program online #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> #include <queue> using namespace std; struct pomalowanie { bool is_wiersz; int numer; char kolor; }; int main() { int n, m; cin >> n >> m; unordered_set<char> wszystkie_kolory; vector<unordered_map<char,int>> kolory_per_wiersz(n, unordered_map<char, int>()); vector<unordered_map<char,int>> kolory_per_kolumna(m, unordered_map<char, int>()); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { char c; cin >> c; wszystkie_kolory.insert(c); kolory_per_wiersz[i][c]++; kolory_per_kolumna[j][c]++; } } vector<pomalowanie> result; queue<pair<bool,int>> kolejka; // is_wiersz, number vector<bool> visited_wiersze(n, false); vector<bool> visited_kolumny(m, false); for(int i = 0; i < n; i++) { if(kolory_per_wiersz[i].size() == 1) { kolejka.push({true, i}); visited_wiersze[i] = true; } } for(int i = 0; i < m; i++) { if(kolory_per_kolumna[i].size() == 1) { kolejka.push({false, i}); visited_kolumny[i] = true; } } while(!kolejka.empty()) { pair<bool,int> nowy = kolejka.front(); kolejka.pop(); bool is_wiersz = nowy.first; int numer = nowy.second; pomalowanie pomal; pomal.is_wiersz = is_wiersz; pomal.numer = numer; pomal.kolor = *wszystkie_kolory.begin(); unordered_map<char,int>& kolory_tego_czegos = is_wiersz ? kolory_per_wiersz[numer] : kolory_per_kolumna[numer]; for(auto& tmp : kolory_tego_czegos) { pomal.kolor = tmp.first; } result.push_back(pomal); // update kolory innych wierszy/kolumn i wstaw do kolejki char kolor = pomal.kolor; if(is_wiersz) { for(int i = 0; i < m; i++) if(!visited_kolumny[i]) { unordered_map<char,int>& kolory = kolory_per_kolumna[i]; kolory[kolor]--; if(kolory[kolor] == 0) { kolory.erase(kolor); } if(kolory.size() <= 1) { kolejka.push({false, i}); visited_kolumny[i] = true; } } } else { for(int i = 0; i < n; i++) if(!visited_wiersze[i]) { unordered_map<char,int>& kolory = kolory_per_wiersz[i]; kolory[kolor]--; if(kolory[kolor] == 0) { kolory.erase(kolor); } if(kolory.size() <= 1) { kolejka.push({true, i}); visited_wiersze[i] = true; } } } } cout << result.size() << endl; for(int i = result.size() - 1; i >= 0; i--) { if(result[i].is_wiersz) { cout << "R "; } else cout << "K "; cout << result[i].numer + 1 << " " << result[i].kolor << endl; } } |
English