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
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

typedef long long ll;

const ll MAX = 2e3 + 1;

char tab[MAX][MAX];
map<char, ll> row[MAX], col[MAX];

struct Move {
    char opt;
    int num;
    char color;

    Move(char o, int n, char c): opt(o), num(n), color(c) {}
};

ostream& operator<<(ostream& os, const Move& mov)
{
    os << mov.opt << ' ' << mov.num << ' ' << mov.color;
    return os;
}

int main()
{
    ios_base::sync_with_stdio(0);

    ll n, m;
    cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> tab[i][j];
            row[i][tab[i][j]]++;
            col[j][tab[i][j]]++;
        }
    }

    vector<Move> result;

    for (int i = 0; i < n + m; ++i) {
        char opt = 'R';
        int id = -1;
        for (int i2 = 0; i2 < n; ++i2) {
            if (row[i2].size() == 1) {
                id = i2;
                opt = 'R';
                break;
            }
        }
        for (int i2 = 0; i2 < m; ++i2) {
            if (col[i2].size() == 1) {
                id = i2;
                opt = 'K';
                break;
            }
        }

        if (id == -1) break;

        char c = opt == 'R' ? row[id].begin()->first : col[id].begin()->first;

        result.pb(Move(opt, (id + 1), c));

        if (opt == 'R') {
            row[id].clear();
            for (int i2 = 0; i2 < m; ++i2) {
                if (col[i2][c] > 0) {
                    col[i2][c]--;
                }
                if (col[i2][c] == 0) {
                    col[i2].erase(c);
                }
            }

        } else {
            col[id].clear();
            for (int i2 = 0; i2 < n; ++i2) {
                if (row[i2][c] > 0) {
                    row[i2][c]--;
                }
                if (row[i2][c] == 0) {
                    row[i2].erase(c);
                }
            }
        }
    }

    cout << result.size() << endl;
    reverse(result.begin(), result.end());
    for (auto m: result) {
        cout << m << endl;
    }
}