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

using namespace std;

int height, width;

int columns[2000][26];
int columnsDifferent[2000];
int rows[2000][26];
int rowsDifferent[2000];

int board[2000][2000];

queue<int> current;

vector<pair<int, int>> steps;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> height >> width;

    for (int i = 0; i < height; ++i)
    {
        for (int j = 0; j < width; ++j)
        {
            char color;
            cin >> color;
            int colorIndex = color - 'A';
            board[i][j] = colorIndex;
            if (rows[i][colorIndex] == 0) ++rowsDifferent[i];
            if (columns[j][colorIndex] == 0) ++columnsDifferent[j];
            rows[i][colorIndex] += 1;
            columns[j][colorIndex] += 1;
        }
    }
    for (int i = 0; i < height; ++i)
    {
        if (rowsDifferent[i] == 1)
        {
            current.emplace(i);
        }
    }
    for (int i = 0; i < width; ++i)
    {
        if (columnsDifferent[i] == 1)
        {
            current.emplace(i + 2000);
        }
    }
    while (!current.empty())
    {
        int element = current.front();
        current.pop();

        if (element < 2000)
        {
            int painted = -1;
            for (int p = 0; p < 26; ++p)
            {
                if (rows[element][p] != 0)
                {
                    painted = p;
                    break;
                }
            }
            if (painted == -1) continue;
            steps.emplace_back(element, painted);
            for (int i = 0; i < width; ++i)
            {
                int color = board[element][i];
                if (color != -1)
                {
                    --columns[i][color];
                    if (columns[i][color] == 0)
                    {
                        --columnsDifferent[i];
                        if (columnsDifferent[i] == 1) current.push(i + 2000);
                    }
                    board[element][i] = -1;
                }
            }
        }
        else
        {
            int painted = -1;
            for (int p = 0; p < 26; ++p)
            {
                if (columns[element - 2000][p] != 0)
                {
                    painted = p;
                    break;
                }
            }
            if (painted == -1) continue;
            steps.emplace_back(element, painted);
            for (int i = 0; i < height; ++i)
            {
                int color = board[i][element - 2000];
                if (color != -1)
                {
                    --rows[i][color];
                    // cout << i << ' ' << (element - 2000) << ' ' << color << ' ' << rows[i][color] << '\n';
                    if (rows[i][color] == 0)
                    {
                        --rowsDifferent[i];
                        if (rowsDifferent[i] == 1) current.push(i);
                    }
                    board[i][element - 2000] = -1;
                }
            }
        }
    }
    cout << steps.size() << '\n';
    for (int i = steps.size() - 1; i >= 0; --i)
    {
        auto step = steps[i];
        if (step.first < 2000)
        {
            cout << "R " << (step.first + 1) << ' ' << (char) (step.second + 'A') << '\n';
        }
        else
        {
            cout << "K " << (step.first - 2000 + 1) << ' ' << (char) (step.second + 'A') << '\n';
        }
    }
}