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
#include <iostream>
#include <limits>
#include <unordered_map>
#include <vector>
using namespace std;

struct Move {
    char dir;
    int num;
    char color;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<string> board;
    board.resize(n);
    for (auto &r : board) {
        cin >> r;
    }
    vector<unordered_map<char, int>> rowCounts, columnCounts;
    rowCounts.resize(n);
    columnCounts.resize(m);
    for (int r = 0; r < n; ++r) {
        for (int c = 0; c < m; ++c) {
            ++rowCounts[r][board[r][c]];
            ++columnCounts[c][board[r][c]];
        }
    }
    vector<Move> moves;
    for (;;) {
        bool moveFound = false;
        for (int r = 0; r < n; ++r) {
            if (rowCounts[r].size() == 1) {
                moveFound = true;
                char color = begin(rowCounts[r])->first;
                moves.push_back({'R', r, color});
                rowCounts[r].clear();
                for (auto &column : columnCounts) {
                    auto iter = column.find(color);
                    if (iter != end(column)) {
                        if (--iter->second == 0) {
                            column.erase(iter);
                        }
                    }
                }
                break;
            }
        }
        for (int c = 0; c < m; ++c) {
            if (columnCounts[c].size() == 1) {
                moveFound = true;
                char color = begin(columnCounts[c])->first;
                moves.push_back({'K', c, color});
                columnCounts[c].clear();
                for (auto &row : rowCounts) {
                    auto iter = row.find(color);
                    if (iter != end(row)) {
                        if (--iter->second == 0) {
                            row.erase(iter);
                        }
                    }
                }
                break;
            }
        }
        if (!moveFound) {
            break;
        }
    }
    cout << size(moves) << '\n';
    for (auto move = rbegin(moves); move != rend(moves); ++move) {
        cout << move->dir << ' ' << move->num + 1 << ' ' << move->color << '\n';
    }
}