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
#include <cstdio>
#include <queue>

int colors[2010][2010];
int column_color[2010][30];
int row_color[2010][30];
int column_cnt[2010];
int row_cnt[2010];
std::queue<int> queue;
std::vector<std::pair<int, int> > result;

int main() {
    int N,M;
    scanf("%d %d\n", &N, &M);

    for (int i=0; i<N; ++i) {
        char str[2100];
        scanf("%s\n", str);

        for (int j=0; j<M; ++j) {
            colors[i][j] = str[j] - 'A';
        }
    }

    for (int i=0; i<N; ++i) {
        for (int j=0; j<M; ++j) {
            row_color[i][colors[i][j]]++;
            column_color[j][colors[i][j]]++;
        }
    }

    for (int i=0; i<N; ++i) {
        for (int c=0; c<30; ++c) {
            if (row_color[i][c] > 0) {
                row_cnt[i]++;
            }
        }
        if (row_cnt[i] == 1) {
            queue.emplace(i);
        }
    }

    for (int j=0; j<M; ++j) {
        for (int c=0; c<30; ++c) {
            if (column_color[j][c] > 0) {
                column_cnt[j]++;
            }
        }
        if (column_cnt[j] == 1) {
            queue.emplace(10000 + j);
        }
    }

    while (!queue.empty()) {
        auto item = queue.front();
        queue.pop();

        //row
        if (item < 10000) {
            int r = item;
            int col = 0;
            for (int c=0; c<30; c++) {
                if (row_color[r][c] > 0) {
                    col = c;
                }
            }

            for (int j=0; j<M; ++j) {
                column_color[j][col]--;
                if (column_color[j][col] == 0) {
                    column_cnt[j]--;
                    if (column_cnt[j] == 1) {
                        queue.emplace(10000 + j);
                    }
                }
            }

            result.emplace_back(item, col);
        } else {
            int x = item - 10000;
            int col = 0;
            for (int c=0; c<30; c++) {
                if (column_color[x][c] > 0) {
                    col = c;
                }
            }

            for (int i=0; i<N; ++i) {
                row_color[i][col]--;
                if (row_color[i][col] == 0) {
                    row_cnt[i]--;
                    if (row_cnt[i] == 1) {
                        queue.emplace(i);
                    }
                }
            }

            result.emplace_back(item, col);
        }
    }

    printf("%d\n", (int)result.size());
    for (int i=result.size()-1; i>=0; --i) {
        if (result[i].first < 10000) {
            printf("R %d %c\n", result[i].first+1, 'A'+result[i].second);
        } else {
            printf("K %d %c\n", result[i].first-10000+1, 'A'+result[i].second);
        }
    }

    return 0;
}