#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <string>
#include <queue>
static std::string nl{"\n"};
struct Ruch {
char kierunek;
int wspolrzedna;
char kolor;
};
static std::vector<std::vector<int8_t>> plansza{};
static std::vector<std::unordered_map<int8_t,int>> wiersze{};
static std::vector<std::unordered_map<int8_t,int>> kolumny{};
static std::queue<int> jednobarwne_wiersze{};
static std::queue<int> jednobarwne_kolumny{};
static std::vector<Ruch> ruchy = {};
static int wysokosc = 0;
static int szerokosc = 0;
void zerwij_wiersz(int y) {
int8_t kolor = 0;
wiersze[y].clear();
for (int x=0; x<szerokosc; ++x){
if (plansza[y][x]) {
kolor |= plansza[y][x];
--kolumny[x][kolor];
if (kolumny[x][kolor] == 0) {
kolumny[x].erase(kolor);
if (kolumny[x].size() == 1) {
jednobarwne_kolumny.push(x);
}
}
}
plansza[y][x] = 0;
}
if (kolor) {
ruchy.push_back({'R', y+1, (char)(kolor-1+'A')});
}
}
void zerwij_kolumne(int x) {
int8_t kolor = 0;
kolumny[x].clear();
for (int y=0; y<wysokosc; ++y){
if (plansza[y][x]) {
kolor |= plansza[y][x];
--wiersze[y][kolor];
// std::cerr << "w" << y << " " << (wiersze[y][kolor]) << " " << wiersze[y].size() << nl;
if (wiersze[y][kolor] == 0) {
wiersze[y].erase(kolor);
if (wiersze[y].size() == 1) {
jednobarwne_wiersze.push(y);
}
}
}
plansza[y][x] = 0;
}
if (kolor) {
ruchy.push_back({'K', x+1, (char)(kolor-1+'A')});
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> wysokosc >> szerokosc;
plansza.resize(wysokosc);
kolumny.resize(szerokosc);
wiersze.resize(wysokosc);
for (int y=0; y<wysokosc; ++y) {
std::string wiersz;
std::cin >> wiersz;
for (int x=0; x<szerokosc; ++x) {
int8_t kolor = wiersz[x] - 'A' + 1;
plansza[y].push_back(kolor);
++wiersze[y][kolor];
if (wiersze[y][kolor] == szerokosc) { jednobarwne_wiersze.push(y); }
++kolumny[x][kolor];
if (kolumny[x][kolor] == wysokosc) { jednobarwne_kolumny.push(x); }
}
}
while (jednobarwne_kolumny.size() || jednobarwne_wiersze.size()) {
while (jednobarwne_kolumny.size()) {
zerwij_kolumne(jednobarwne_kolumny.front());
jednobarwne_kolumny.pop();
}
while (jednobarwne_wiersze.size()) {
zerwij_wiersz(jednobarwne_wiersze.front());
jednobarwne_wiersze.pop();
}
}
std::cout << ruchy.size() << nl;
for (auto ruch=ruchy.rbegin(); ruch!=ruchy.rend(); ++ruch) {
std::cout << ruch->kierunek << " " << ruch->wspolrzedna << " " << ruch->kolor << nl;
}
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <string> #include <queue> static std::string nl{"\n"}; struct Ruch { char kierunek; int wspolrzedna; char kolor; }; static std::vector<std::vector<int8_t>> plansza{}; static std::vector<std::unordered_map<int8_t,int>> wiersze{}; static std::vector<std::unordered_map<int8_t,int>> kolumny{}; static std::queue<int> jednobarwne_wiersze{}; static std::queue<int> jednobarwne_kolumny{}; static std::vector<Ruch> ruchy = {}; static int wysokosc = 0; static int szerokosc = 0; void zerwij_wiersz(int y) { int8_t kolor = 0; wiersze[y].clear(); for (int x=0; x<szerokosc; ++x){ if (plansza[y][x]) { kolor |= plansza[y][x]; --kolumny[x][kolor]; if (kolumny[x][kolor] == 0) { kolumny[x].erase(kolor); if (kolumny[x].size() == 1) { jednobarwne_kolumny.push(x); } } } plansza[y][x] = 0; } if (kolor) { ruchy.push_back({'R', y+1, (char)(kolor-1+'A')}); } } void zerwij_kolumne(int x) { int8_t kolor = 0; kolumny[x].clear(); for (int y=0; y<wysokosc; ++y){ if (plansza[y][x]) { kolor |= plansza[y][x]; --wiersze[y][kolor]; // std::cerr << "w" << y << " " << (wiersze[y][kolor]) << " " << wiersze[y].size() << nl; if (wiersze[y][kolor] == 0) { wiersze[y].erase(kolor); if (wiersze[y].size() == 1) { jednobarwne_wiersze.push(y); } } } plansza[y][x] = 0; } if (kolor) { ruchy.push_back({'K', x+1, (char)(kolor-1+'A')}); } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> wysokosc >> szerokosc; plansza.resize(wysokosc); kolumny.resize(szerokosc); wiersze.resize(wysokosc); for (int y=0; y<wysokosc; ++y) { std::string wiersz; std::cin >> wiersz; for (int x=0; x<szerokosc; ++x) { int8_t kolor = wiersz[x] - 'A' + 1; plansza[y].push_back(kolor); ++wiersze[y][kolor]; if (wiersze[y][kolor] == szerokosc) { jednobarwne_wiersze.push(y); } ++kolumny[x][kolor]; if (kolumny[x][kolor] == wysokosc) { jednobarwne_kolumny.push(x); } } } while (jednobarwne_kolumny.size() || jednobarwne_wiersze.size()) { while (jednobarwne_kolumny.size()) { zerwij_kolumne(jednobarwne_kolumny.front()); jednobarwne_kolumny.pop(); } while (jednobarwne_wiersze.size()) { zerwij_wiersz(jednobarwne_wiersze.front()); jednobarwne_wiersze.pop(); } } std::cout << ruchy.size() << nl; for (auto ruch=ruchy.rbegin(); ruch!=ruchy.rend(); ++ruch) { std::cout << ruch->kierunek << " " << ruch->wspolrzedna << " " << ruch->kolor << nl; } } |
English