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
#include <bits/stdc++.h>

using namespace std;

struct Ruch {
	char typ; // 'R' lub 'K'
	int nr;
	char kolor;
};

const int MAX = 2007;
int ile[MAX][30][2]; // numer, kolor, RZAD/KOLUMNA
bool zrobione[MAX][2];

pair<bool,char> czyok(bool czyK, int x)
{
	char niezero = -1;
	for (int i = 0; i < 26; i++) {
		if (ile[x][i][czyK] <= 0)
			continue;
		if (niezero != -1)
			return {false, -1};
		niezero = i;
	}
	return {(niezero!=-1), niezero};
}

void cofnij(Ruch r, int n, int m)
{
	bool czyK = (r.typ == 'K');
	int maks = (czyK ? n : m);
	for (int i = 1; i <= maks; i++) {
		int x = (czyK ? i : r.nr);
		int y = (czyK ? r.nr : i);
		ile[x][(int)r.kolor][0]--;
		ile[y][(int)r.kolor][1]--;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			char c;
			cin >> c;
			ile[i][c-'A'][0]++;
			ile[j][c-'A'][1]++;
		}

	queue<Ruch> Q;
	for (int i = 1; i <= n; i++) {
		pair<bool,char> tmp = czyok(false, i);
		if (tmp.first)
			Q.push({'R', i, tmp.second});
	}
	for (int i = 1; i <= m; i++) {
		pair<bool,char> tmp = czyok(true, i);
		if (tmp.first)
			Q.push({'K', i, tmp.second});
	}

	vector<Ruch> ruchy;
	while (!Q.empty()) {
		Ruch r = Q.front();
		Q.pop();
		// nie rob tego ruchu jak nie trzeba
		if (!czyok(r.typ=='K', r.nr).first)
			continue;
		zrobione[r.nr][r.typ=='K'] = true;
		ruchy.push_back(r);

		// cofnij ten ruch
		cofnij(r, n, m);

		// sprobuj dodac nowe ruchy
		int maks = (r.typ=='K' ? n : m);
		char typ = (r.typ=='K' ? 'R' : 'K');
		for (int i = 1; i <= maks; i++) {
			if (zrobione[i][typ=='K'])
				continue;
			pair<bool,char> tmp = czyok(typ=='K', i);
			if (tmp.first) {
				zrobione[i][typ=='K'] = true;
				Q.push({typ, i, tmp.second});
			}
		}
	}

	reverse(ruchy.begin(), ruchy.end());
	cout << ruchy.size() << '\n';
	for (Ruch r : ruchy)
		cout << r.typ << ' ' << r.nr << ' ' << char('A'+r.kolor) << '\n';

	return 0;
}

/*#include <bits/stdc++.h>

using namespace std;

struct Ruch {
	char typ; // 'R' lub 'K'
	int nr;
	char kolor;
};

const int MAX = 2007;
int ile[MAX][30][2]; // numer, kolor, RZAD/KOLUMNA

pair<bool,char> czyok(bool czyK, int x)
{
	char niezero = -1;
	for (int i = 0; i < 26; i++) {
		if (ile[x][i][czyK] <= 0)
			continue;
		if (niezero != -1)
			return {false, -1};
		niezero = i;
	}
	return {(niezero!=-1), niezero};
}

void cofnij(Ruch r, int n, int m)
{
	bool czyK = (r.typ == 'K');
	int maks = (czyK ? n : m);
	for (int i = 1; i <= maks; i++) {
		int x = (czyK ? i : r.nr);
		int y = (czyK ? r.nr : i);
		ile[x][(int)r.kolor][0]--;
		ile[y][(int)r.kolor][1]--;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			char c;
			cin >> c;
			ile[i][c-'A'][0]++;
			ile[j][c-'A'][1]++;
		}

	vector<Ruch> ruchy;
	for (int i = 0; i < n+m; i++) {
		Ruch teraz;
		for (int idx = 1; idx <= n; idx++) {
			pair<bool,char> tmp = czyok(false, idx);
			if (tmp.first) {
				teraz = {'R', idx, tmp.second};
				goto zrob;
			}
		}
		for (int idx = 1; idx <= m; idx++) {
			pair<bool,char> tmp = czyok(true, idx);
			if (tmp.first) {
				teraz = {'K', idx, tmp.second};
				goto zrob;
			}
		}
		break; //jesli nie ma dobrego ruchu to skoncz
		zrob:
		ruchy.push_back(teraz);
		cofnij(teraz, n, m);
	}

	reverse(ruchy.begin(), ruchy.end());
	cout << ruchy.size() << '\n';
	for (Ruch r : ruchy)
		cout << r.typ << ' ' << r.nr << ' ' << char('A'+r.kolor) << '\n';

	return 0;
}*/