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
/* 2024
 * Maciej Szeptuch
 */
#include <cstdio>
#include <vector>

const int MAX_SIZE = 2001;

int width;
int height;
bool waiting_row[MAX_SIZE];
bool waiting_col[MAX_SIZE];
char row[MAX_SIZE][MAX_SIZE];
std::vector<std::pair<int, char>> action;
std::vector<int> que;

void scan_row(int h);
void scan_col(int w);
int abs(int a);

int main(void)
{
    scanf("%d %d", &height, &width);
    for(int h = 0; h < height; ++h)
        scanf("%s", row[h]);

    for(int h = 0; h < height; ++h)
        scan_row(h);

    for(int w = 0; w < width; ++w)
        scan_col(w);

    while(!que.empty())
    {
        int o = que.back();
        que.pop_back();
        if(o < 0) // row
        {
            waiting_row[-o - 1] = false;
            scan_row(-o - 1);
            continue;
        }

        // column
        waiting_col[o - 1] = false;
        scan_col(o - 1);
    }

    printf("%lu\n", action.size());
    for(int a = action.size() - 1; a >= 0; --a)
        printf("%c %d %c\n", action[a].first < 0 ? 'R' : 'K', abs(action[a].first), action[a].second);

    return 0;
}

inline void scan_row(int h)
{
    char color = '?';
    for(int w = 0; w < width; ++w)
    {
        if(color == '?')
            color = row[h][w];
        else if(row[h][w] != '?' && color != row[h][w])
            return;
    }

    if(color == '?')
        return;

    action.push_back({-h - 1, color});
    for(int w = 0; w < width; ++w)
        if(row[h][w] != '?')
        {
            row[h][w] = '?';
            if(!waiting_col[w])
            {
                waiting_col[w] = true;
                que.push_back(w + 1);
            }
        }
}

inline void scan_col(int w)
{
    char color = '?';
    for(int h = 0; h < height; ++h)
    {
        if(color == '?')
            color = row[h][w];
        else if(row[h][w] != '?' && color != row[h][w])
            return;
    }

    if(color == '?')
        return;

    action.push_back({w + 1, color});
    for(int h = 0; h < height; ++h)
        if(row[h][w] != '?')
        {
            row[h][w] = '?';
            if(!waiting_row[h])
            {
                waiting_row[h] = true;
                que.push_back(-h - 1);
            }
        }
}

inline int abs(int a)
{
    return a < 0 ? -a : a;
}