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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include <vector>
#include <set>
#include <stack>

using namespace std;

typedef int Kolor;

const int MAX_N = 2000;
const int N_COLORS = 26;
int n_rows, n_cols;
Kolor plansza[MAX_N+1][MAX_N+1];


Kolor letter_to_kolor(char c) {
    return c - 'A';
}

char kolor_to_letter(Kolor k) {
    return k + 'A';
}

struct Operacja {
    char kierunek;
    int numer;
    Kolor kolor;
};

struct KreskaInfo {
    vector<int> kolor_to_count;
    set<Kolor> present_colors;

    void clear() {
        kolor_to_count = vector<int>(N_COLORS);
        present_colors.clear();
    }

    void reset_from_rzad(int r) {
        clear();
        for (int c = 1; c <= n_cols; ++c) {
            Kolor k = plansza[r][c];
            kolor_to_count[k]++;
            present_colors.insert(k);
        }
    }

    void reset_from_kolumna(int c) {
        clear();
        for (int r = 1; r <= n_rows; ++r) {
            Kolor k = plansza[r][c];
            kolor_to_count[k]++;
            present_colors.insert(k);
        }
    }

    bool is_single_color() const {
        return present_colors.size() == 1;
    }

    Kolor get_one_present_kolor() const {
        return *present_colors.begin();
    }

    void remove_kolor(Kolor k) {
        kolor_to_count[k]--;
        if (kolor_to_count[k] == 0) {
            present_colors.erase(k);
        }
    }

};

vector<KreskaInfo> rzad_to_info;
vector<KreskaInfo> kolumna_to_info;
set<int> remaining_rows;
set<int> remaining_cols;
set<int> single_color_rows;
set<int> single_color_cols;



void read_data() {
    cin >> n_rows >> n_cols;
    string line;
    getline(cin, line);  // dangling \n
    for (int r = 1; r <= n_rows; ++r) {
        getline(cin, line);
        for (int i = 0; i < n_cols; ++i) {
            Kolor kolor = letter_to_kolor(line[i]);
            int c = i + 1;
            plansza[r][c] = kolor;
        }
    }
}

void print_plansza() {
    cout << n_rows << " x " << n_cols << endl;
    for (int r = 1; r <= n_rows; ++r) {
        for (int c = 1; c <= n_cols; ++c) {
            Kolor k = plansza[r][c];
            if (k >= 10) cout << k << " ";
            else cout << " " << k << " ";
        }
        cout << endl;
    }
}

void przygotuj_auxiliary() {
    rzad_to_info.push_back({});     // fill in index 0
    kolumna_to_info.push_back({});

    for (int r = 1; r <= n_rows; ++r) {
        remaining_rows.insert(r);
        KreskaInfo info;
        info.reset_from_rzad(r);
        rzad_to_info.push_back(info);
        if (info.is_single_color()) {
            single_color_rows.insert(r);
        }
    }
    for (int c = 1; c <= n_cols; ++c) {
        remaining_cols.insert(c);
        KreskaInfo info;
        info.reset_from_kolumna(c);
        kolumna_to_info.push_back(info);
        if (info.is_single_color()) {
            single_color_cols.insert(c);
        }
    }
    
}

Operacja remove_rzad() {
    int r = *single_color_rows.begin();
    //cout << "*** Usuwam rzad " << r << " ***" << endl;
    Kolor k = rzad_to_info[r].get_one_present_kolor();
    remaining_rows.erase(r);
    single_color_rows.erase(r);

    for (int c: remaining_cols) {
        kolumna_to_info[c].remove_kolor(k);
        if (kolumna_to_info[c].is_single_color()) {
            single_color_cols.insert(c);
        }
    }

    return { 'R', r, k };
}

Operacja remove_kolumna() {
    int c = *single_color_cols.begin();
    //cout << "*** Usuwam kolumne " << c << " ***" << endl;
    Kolor k = kolumna_to_info[c].get_one_present_kolor();
    remaining_cols.erase(c);
    single_color_cols.erase(c);

    for (int r: remaining_rows) {
        rzad_to_info[r].remove_kolor(k);
        if (rzad_to_info[r].is_single_color()) {
            single_color_rows.insert(r);
        }
    }

    return { 'K', c, k };
}

ostream& operator<<(ostream& out, set<int> s) {
    out << "{";
    for (int el: s) {
        cout << el << ", ";
    }
    out << "}";
    return out;
}

ostream& operator<<(ostream& out, const KreskaInfo& ki) {
    out << "[" << " present_colors: " << ki.present_colors << "; ";
    for (Kolor k = 0; k < N_COLORS; ++k) {
        if (ki.kolor_to_count[k] > 0) {
            out << k << " -> " << ki.kolor_to_count[k] << ", ";
        }
    }
    out << "]";
    return out;
}

void print_state() {
    cout << "remaining_rows: " << remaining_rows << endl;
    cout << "remaining_cols: " << remaining_cols << endl;
    cout << "single_color_rows: " << single_color_rows << endl;
    cout << "single_color_cols: " << single_color_cols << endl;

    cout << "Rzedy" << endl;
    for (int r = 1; r <= n_rows; ++r) {
        if (remaining_rows.find(r) == remaining_rows.end()) {
            continue;
        }
        cout << r << ": " << rzad_to_info[r] << endl;
    }

    cout << "Kolumny" << endl;
    for (int c = 1; c <= n_cols; ++c) {
        if (remaining_cols.find(c) == remaining_cols.end()) {
            continue;
        }
        cout << c << ": " << kolumna_to_info[c] << endl;
    }

    cout << "-------------------------------------------------------" << endl << endl;
}

stack<Operacja> solve() {
    stack<Operacja> operacje;
    while (!remaining_rows.empty() && !remaining_cols.empty()) {
        if (!single_color_rows.empty()) {
            operacje.push(remove_rzad());
            //print_state();
        }
        if (!single_color_cols.empty()) {
            operacje.push(remove_kolumna());
            //print_state();
        }
    }
    return operacje;
}


void wypisz_wynik(stack<Operacja>& operacje) {
    cout << operacje.size() << endl;
    while (!operacje.empty()) {
        Operacja op = operacje.top();
        operacje.pop();
        cout << op.kierunek << " " << op.numer << " " << kolor_to_letter(op.kolor) << endl;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    read_data();
    //print_plansza();
    przygotuj_auxiliary();
    //cout << "Auxiliary gotowe!!" << endl;
    //print_state();

    stack<Operacja> operacje = solve();
    wypisz_wynik(operacje);
    return 0;
}