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
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>

using namespace std;
int noColor=100;
struct movement
{
    char rork;
    int n;
    char color;
};

void uncolorRow(vector<int> & vec)
{
    for(auto & v:vec){ v=noColor;}
}
void uncolorColumn(vector<vector<int>> & pic, int column)
{
    for(int i =0;i<pic.size();++i) pic[i][column] = noColor;
}

bool isUniqueRow(vector<int> & vec, int &col)
{
    int i =0;
    int c = noColor;
    while(i<vec.size() and vec[i]==noColor) ++i;
    if( i==vec.size()) {  return true;}
    c = vec[i];
    for(int z=0;z<vec.size(); ++z ) {if((vec[z]!= c) and (vec[z]!= noColor))return  false;}
    col =c;
    return true;
}

bool isUniqueColumn(vector<vector<int>> & pic,int column, int &col)
{
    int i =0;
    int c = noColor;
    while(i<pic.size() and pic[i][column]==noColor) ++i;
    if( i==pic.size()) { return true;}
    c = pic[i][column];
    for(int z=0;z<pic.size(); ++z ) {if(((pic[z][column])!= c) and ((pic[z][column])!= noColor))return  false;}
    col =c;

    return true;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    vector<vector<int>> pic(n, vector<int>(m, noColor));
    for(int i=0;i<n;++i) {
        string s; cin>>s; 
        for(int j=0;j<m;++j)
        {
            pic[i][j] = s[j]-'A';
        }
    }

    vector<movement> moves;
    vector<bool> row(n,false), column(m,false);

    while( moves.size()<m+n)
    {
        for(int i=0;i<n;++i)
        {
           int c=0;
           if(not row[i] and isUniqueRow(pic[i], c)) 
           {
               row[i] =true;
               moves.push_back({'R', i+1, static_cast<char>('A'+c )});
               uncolorRow(pic[i]);
           }
        }
        for(int i=0;i<m;++i)
        {
           int c=0;
           if(not column[i] and isUniqueColumn(pic,i, c)) 
           {
               column[i] =true;
               moves.push_back({'K', i+1, static_cast<char>('A'+c )});
               uncolorColumn(pic, i);
           }
        }
    }

    int k =moves.size();
    cout<< k<<endl;
    for(int i=0;i<moves.size();++i)
    {
        cout<< moves[k-1-i].rork <<" " <<moves[k-1-i].n << " "<<moves[k-1-i].color<<endl;
    }
    return 0;
}