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

int ox, oy;
int h, w;
char field[2001][2001];

enum direction { VERTICAL, HORIZONTAL };

char check_line_monochromatic(int xy, direction dir) {
	char color = dir == VERTICAL ? field[oy][xy] : field[xy][ox];
	/*if(dir == VERTICAL) std::cerr << "V "; else std::cerr << "H ";
	std::cerr << w << ' ' << h << ' ' << ox << ' ' << oy << '(' << color << " " << xy << ')' << '\n';
	for(int y = oy; y < h; ++y) {
		for(int x = ox; x < w; ++x)
			std::cout << field[y][x];
		std::cout << '\n';
	}*/

	if(dir == VERTICAL)
		for(int i = oy+1; i < h; ++i) {
			if(field[i][xy] != color)
				return 0;
		}
	else
		for(int i = ox+1; i < w; ++i) {
			if(field[xy][i] != color)
				return 0;
		}

	return color;
}

int main() {
	std::cin >> h >> w;

	for(int y = 0; y < h; ++y)
		for(int x = 0; x < w; ++x)
			std::cin >> field[y][x];

	struct line {
		direction dir;
		int pos;
		char color;
	};
	std::vector<line> lines;

	while((h-oy) && (w-ox)) {
		if(char c = check_line_monochromatic(ox, VERTICAL); c) {
			lines.emplace_back(VERTICAL, ox, c);
			ox += 1;
continue;
		}
		if(char c = check_line_monochromatic(w - 1, VERTICAL); c) {
			w -= 1;
			lines.emplace_back(VERTICAL, w, c);
continue;
		}
		if(char c = check_line_monochromatic(oy, HORIZONTAL); c) {
			lines.emplace_back(HORIZONTAL, oy, c);
			oy += 1;
continue;
		}
		if(char c = check_line_monochromatic(h-1, HORIZONTAL); c) {
			h -= 1;
			lines.emplace_back(HORIZONTAL, h, c);
continue;
		}
	}

	std::cout << lines.size() << '\n';
	for(auto [dir, pos, color] : lines | std::views::reverse) {
		if(dir == VERTICAL) std::cout << "K ";
		else std::cout << "R ";
		std::cout << pos + 1 << ' ' << color << '\n';
	}
}