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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

#define MAX_SIZE 2005
const char ast = '*';


struct Move {
public:
        char d;
        int row;
        char c;
        Move( char dd, int rr, char cc) : d(dd), row(rr), c(cc) {}
};

int n, m;
int all;

char tab[MAX_SIZE][MAX_SIZE];
stack<Move> res;
bool col_painted[MAX_SIZE];
bool row_painted[MAX_SIZE];

void print_tab()
{
        for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= m; ++j) {
                        printf("%c", tab[i][j]);
                }
                printf("\n");
        }
}

void print_res()
{
//        printf("%d %d\n", n, m);

        printf("%lu\n", res.size());
        while(!res.empty()) {
                Move m = res.top();
                res.pop();
                printf("%c %d %c\n", m.d, m.row, m.c);
        }
}

// tab[i][*] - wiersz i
// tab[*][j] - kolumna j

int find_first_col_in_row(int row)
{
        for(int j = 1; j <= m; ++j) {
                if (tab[row][j] != ast) {
                        return j;
                }
        }
        return -1;
}

bool can_paint_row(int row, char& c)
{
        int first_col;

        first_col = find_first_col_in_row(row);
        if (first_col == -1) {
                row_painted[row] = true;
                return false;
        }
        c = tab[row][first_col];
        for(int j = first_col+1; j <= m; ++j) {
                if (tab[row][j] != c && tab[row][j] != ast) {
                        return false;
                }
        }
        return true;
}

void paint_row(int row)
{
        char c;
        if (can_paint_row(row, c)) {
                res.push( Move( 'R', row, c ) );
                row_painted[row] = true;
                for(int j = 1; j <= m; ++j) {
                        if (tab[row][j] != ast) {
                                tab[row][j] = ast;
                                --all;
                        }
                }
        }
}

int find_first_row_in_col(int col)
{
        for(int i = 1; i <= n; ++i) {
                if (tab[i][col] != ast) {
                        return i;
                }
        }
        return -1;
}

bool can_paint_col(int col, char& c)
{
        int first_row;

        first_row = find_first_row_in_col(col);
        if (first_row == -1) {
                col_painted[col] = true;
                return false;
        }
        c = tab[first_row][col];
        for(int i = first_row+1; i <= n; ++i) {
                if (tab[i][col] != c && tab[i][col] != ast) {
                        return false;
                }
        }
        return true;
}

void paint_col(int col)
{
        char c;
        if (can_paint_col(col, c)) {
                res.push( Move( 'K', col, c ) );
                col_painted[col] = true;
                for(int i = 1; i <= n; ++i) {
                        if (tab[i][col] != ast) {
                                tab[i][col] = ast;
                                --all;
                        }
                }

        }
}

// cr == 0 -> paint cols
// cr == 1 -> paint rows

void paint(int cr)
{
        if (cr == 0) {
                for(int j = 1; j <= m; ++j) {
                        if (!col_painted[j]) {
                                paint_col(j);
                        }
                        if (all == 0) {
                                return;
                        }
                }
        } else {
                for(int i = 1; i <= n; ++i) {
                        if (!row_painted[i]) {
                                paint_row(i);
                        }
                        if (all == 0) {
                                return;
                        }
                }
        }
}

int main()
{
        int cr;

        scanf("%d %d\n", &n, &m);
        all = n*m;
        for(int i = 1; i <= n; ++i) {
                char line[MAX_SIZE];
                scanf("%s[^\n]", line);
                for(int j = 1; j <= m; ++j) {
                        tab[i][j] = line[j-1];
                }
        }

        cr = 0;
        while(all > 0) {
                paint(cr);
                cr = 1 - cr;
        }

        print_res();
        return 0;
}