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
#include <bits/stdc++.h>
using namespace std;

struct Line {
    bool horiz = false;
    map<char, int> buckets;
};

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int h, w;
    cin >> h >> w;
    vector<string> board(h);
    vector<Line> hlines(h);
    vector<Line> vlines(w);
    vector<Line*> flats;
    for (int i = 0; i < h; i++) {
        hlines[i].horiz = true;
        cin >> board[i];
        for (int j = 0; j < w; j++) {
            hlines[i].buckets[board[i][j]]++;
            vlines[j].buckets[board[i][j]]++;
        }
    }
    for (auto &line : hlines) if (line.buckets.size() == 1) flats.push_back(&line);
    for (auto &line : vlines) if (line.buckets.size() == 1) flats.push_back(&line);
    vector<string> results;
    while (flats.size() > 0) {
        vector<Line *> nflats;
        for (Line *line : flats) {
            ostringstream cmd;
            if (line->buckets.size() == 0) continue;
            if (line->horiz) {
                int y = line-&hlines[0];
                cmd << "R " << y+1 << " " << line->buckets.begin()->first << endl;
                for (int x = 0; x < w; x++) if (board[y][x] > 0) {
                    if (--vlines[x].buckets[board[y][x]]==0) {
                        vlines[x].buckets.erase(board[y][x]);
                        if (vlines[x].buckets.size() == 1) nflats.push_back(&vlines[x]);
                    }
                }
            } else {
                int x = line-&vlines[0];                
                cmd << "K " << x+1 << " " << line->buckets.begin()->first << endl;
                for (int y = 0; y < h; y++) if (board[y][x] > 0) {
                    if (--hlines[y].buckets[board[y][x]]==0) {
                        hlines[y].buckets.erase(board[y][x]);
                        if (hlines[y].buckets.size() == 1) nflats.push_back(&hlines[y]);
                    }
                }
            }
            line->buckets.clear();
            results.push_back(cmd.str());
        }
        flats = nflats;
    }
    reverse(results.begin(), results.end());
    cout << results.size() << endl;
    copy(results.begin(), results.end(), ostream_iterator<string>(cout));
}