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
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <set>


struct Entry {
    char letter;
    int num;
    char rc;
};

char checkSingleLetterInColumn(int rows, int column, std::vector<std::vector<char>> *tab){
    char letter = '0';

    for (int row = 0; row < rows; row++){
        char currentLetter = tab->at(row).at(column);
        if(currentLetter != '0' && letter == '0'){//first letter save it
            letter = currentLetter;
        } else if(currentLetter != '0' && letter != '0' && currentLetter != letter){//consecutive other letter
            return '0';
        }
    }

    return letter;    
}

void fillColumn(int rows, int col, std::vector<std::vector<char>> *tab, char letter){
    for (int row = 0; row < rows; row++){
        tab->at(row).at(col) = letter;
    }
}

void goThroughColumns(int rows, int cols, std::vector<std::vector<char>> *tab, std::set<int> *columnsCompleted, std::vector<Entry> *solution){
    if(columnsCompleted->size() == cols - 1)
        return;
    for(int col = 0; col < cols; col++){
        if(!columnsCompleted->contains(col)){
            char letter = checkSingleLetterInColumn(rows, col, tab);
            if(letter != '0'){
                fillColumn(rows, col, tab, '0');
                columnsCompleted->insert(col);
                Entry entry;
                entry.letter = letter;
                entry.rc = 'K';
                entry.num = col+1;
                solution->push_back(entry);
            }
        }
    }    
}

char checkSingleLetterInRow(int row, int cols, std::vector<std::vector<char>> *tab){
    char letter = '0';

    for (int col = 0; col < cols; col++){
        char currentLetter = tab->at(row).at(col);
        // std::cout << "control " << currentLetter << " " << letter << "\n";
        if(currentLetter != '0' && letter == '0'){//first letter save it
            letter = currentLetter;
        } else if(currentLetter != '0' && letter != '0' && currentLetter != letter){//consecutive other letter
            return '0';
        }
    }

    return letter;    
}

void fillRow(int row, int cols, std::vector<std::vector<char>> *tab, char letter){
    std::fill(tab->at(row).begin(), tab->at(row).end(), letter);
    // for (int col = 0; col < cols; col++){                                       
    //     tab->at(row).at(col) = letter;                                          
    // } 
}

void goThroughRows(int rows, int cols, std::vector<std::vector<char>> *tab, std::set<int> *rowsCompleted, std::vector<Entry> *solution){
    if(rowsCompleted->size() == rows - 1)
        return;
    for(int row = 0; row < rows; row++){
        if(!rowsCompleted->contains(row)){
            char letter = checkSingleLetterInRow(row, cols, tab);
            if(letter != '0'){
                fillRow(row, cols, tab, '0');
                rowsCompleted->insert(row);
                Entry entry;
                entry.letter = letter;
                entry.rc = 'R';
                entry.num = row+1;
                solution->push_back(entry);
            }
        }
    }    
}

int main() {

    int rows;
    int cols;
    std::cin >> rows;
    std::cin >> cols;

    std::vector<std::vector<char>> tab(rows);
    for (int row = 0; row < rows; row++){
        for(int col = 0; col < cols; col++){
            char letter;
            std::cin >> letter;
            tab[row].push_back(letter);
        }
    }

    std::set<int> columnsCompleted;
    std::set<int> rowsCompleted;

    std::vector<Entry> solution;

    while(columnsCompleted.size() < cols && rowsCompleted.size() < rows){
        goThroughColumns(rows, cols, &tab, &columnsCompleted, &solution);
        goThroughRows(rows, cols, &tab, &rowsCompleted, &solution);

        // for (int row = 0; row < rows; row++){
        //     for(int col = 0; col < cols; col++){
        //         std::cout << tab.at(row).at(col) << " ";
        //     }
        //     std::cout << "\n";
        // }
    }

    std::cout << solution.size() << "\n";
    for (std::vector<Entry>::reverse_iterator riter = solution.rbegin(); riter != solution.rend(); ++riter) 
    { 
        std::cout << riter->rc << " " << riter->num << " " << riter->letter << "\n";
    } 

    return 0;
}