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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <vector>
#include <map>
// #include <set>
#include <algorithm>

typedef struct move
{
    char row_col;
    int number;
    char color;
} move;

static std::vector<std::map<char, int>> row_stats;
static std::vector<std::map<char, int>> col_stats;
// static std::vector<std::vector<char>> data;
static char data[2000][2000];
static std::vector<move> res;
static std::vector<int> rows_todo;
static std::vector<int> cols_todo;
static std::vector<int> rows_chk;
static std::vector<int> cols_chk;

static int rows, cols;

void print_stats()
{
    std::cout << "### ROWS ###" << std::endl;
    for (int r = 0; r < rows; r++)
    {
        std::cout << r << " : ";
        for (auto &[k, v] : row_stats[r])
            std::cout << k << "->" << v << "; ";
        std::cout << std::endl;
    }
    std::cout << "### COLS ###" << std::endl;
    for (int c = 0; c < cols; c++)
    {
        std::cout << c << " : ";
        for (auto &[k, v] : col_stats[c])
            std::cout << k << "->" << v << "; ";
        std::cout << std::endl;
    }
    std::cout << "---" << std::endl;
}

void print_board()
{
    for (auto &row : data)
    {
        for (auto &ch : row)
        {
            std::cout << ch;
        }
        std::cout << std::endl;
    }
    std::cout << "---" << std::endl;
}

void print_chk()
{
    for (auto &chk : rows_chk)
        std::cout << chk << "; ";
    std::cout << std::endl;
    for (auto &chk : cols_chk)
        std::cout << chk << "; ";
    std::cout << std::endl
              << "---" << std::endl;
}

void print(std::vector<move> data)
{
    std::cout << data.size() << '\n';
    for (auto &row : data)
    {
        std::cout << row.row_col << ' ' << row.number + 1 << ' ' << row.color << '\n';
    }
}

void print_todo()
{
    for (auto &chk : rows_todo)
        std::cout << chk << "; ";
    std::cout << std::endl;
    for (auto &chk : cols_todo)
        std::cout << chk << "; ";
    std::cout << std::endl
              << "---" << std::endl;
}

int row_fill(int row, char ch)
{
    move tmp;
    int val = 0;
    // for (int col = 0; col < cols; col++)
    for (int &col : cols_chk)
    {
        if (data[row][col] != '#')
        {
            row_stats[row][ch]--;
            if (row_stats[row][ch] == 0)
                row_stats[row].erase(ch);
            col_stats[col][ch]--;
            if (col_stats[col][ch] == 0)
                col_stats[col].erase(ch);
            if (col_stats[col].size() == 1 && std::find(cols_todo.begin(), cols_todo.end(), col) == cols_todo.end())
                cols_todo.push_back(col);
            data[row][col] = '#';
            val++;
        }
    }
    // rows_chk.erase(std::remove(rows_chk.begin(), rows_chk.end(), row), rows_chk.end());
    rows_chk.erase(std::find(rows_chk.begin(), rows_chk.end(), row));
    tmp.row_col = 'R';
    tmp.number = row;
    tmp.color = ch;
    res.insert(res.begin(), tmp);
    return val;
}

int col_fill(int col, char ch)
{
    move tmp;
    int val = 0;
    // for (int row = 0; row < rows; row++)
    for (int &row : rows_chk)
    {
        if (data[row][col] != '#')
        {
            row_stats[row][ch]--;
            if (row_stats[row][ch] == 0)
                row_stats[row].erase(ch);
            if (row_stats[row].size() == 1 && std::find(rows_todo.begin(), rows_todo.end(), row) == rows_todo.end())
                rows_todo.push_back(row);
            col_stats[col][ch]--;
            if (col_stats[col][ch] == 0)
                col_stats[col].erase(ch);
            data[row][col] = '#';
            val++;
        }
    }
    // cols_chk.erase(std::remove(cols_chk.begin(), cols_chk.end(), col), cols_chk.end());
    cols_chk.erase(std::find(cols_chk.begin(), cols_chk.end(), col));
    tmp.row_col = 'K';
    tmp.number = col;
    tmp.color = ch;
    res.insert(res.begin(), tmp);
    return val;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int done = 0;
    std::string line;

    std::cin >> rows >> cols;
    // data.resize(rows);
    // for (auto &x : data)
    //     x.resize(cols);
    row_stats.resize(rows);
    col_stats.resize(cols);
    rows_chk.reserve(rows);
    cols_chk.reserve(cols);
    for (int row = 0; row < rows; row++)
        rows_chk.push_back(row);
    for (int col = 0; col < cols; col++)
        cols_chk.push_back(col);

    for (int r = 0; r < rows; r++)
    {
        std::cin >> line;
        for (int ch = 0; ch < cols; ch++)
        {
            data[r][ch] = line[ch];
            row_stats[r][line[ch]]++;
            col_stats[ch][line[ch]]++;
        }
    }

    for (int row = 0; row < rows; row++)
        if (row_stats[row].size() == 1)
            rows_todo.push_back(row);
    for (int col = 0; col < cols; col++)
        if (col_stats[col].size() == 1)
            cols_todo.push_back(col);

    while (done < rows * cols)
    {
        for (int &row : rows_todo)
        {
            done += row_fill(row, row_stats[row].begin()->first);
        }
        rows_todo.clear();

        if (done == rows * cols)
            break;

        for (int &col : cols_todo)
        {
            done += col_fill(col, col_stats[col].begin()->first);
        }
        cols_todo.clear();
    }
    print(res);
}