#include <iostream> #include <vector> #include <list> #include <algorithm> #define LIMIT 2001 using namespace std; int n, m; char dia[LIMIT][LIMIT]; class Move { public: bool isRow; int id; char color; Move(bool isRow2, int id2, char color2) : isRow(isRow2), id(id2), color(color2) {} }; class Line { public: int empty = 0; int counts[26] = {0}; char leader; bool isRow; int id; Line(bool isRow, int minx, int maxx, int miny, int maxy) { this->isRow = isRow; id = isRow ? miny + 1 : minx + 1; int max = 0; for (int y = miny; y <= maxy; y++) { for (int x = minx; x <= maxx; x++) { counts[dia[y][x] - 'A']++; if (counts[dia[y][x] - 'A'] > max) { max = counts[dia[y][x] - 'A']; leader = dia[y][x]; } } } } void remove(char color) { counts[color - 'A']--; empty++; int max = 0; for (char c = 'A'; c <= 'Z'; c++) { if (counts[c - 'A'] > max) { max = counts[c - 'A']; leader = c; } } } bool isReady() { return empty + counts[leader - 'A'] == getCount(); } bool isEmpty() { return empty == getCount(); } int getCount() const { return isRow ? m : n; } bool operator<(const Line &other) const { return counts[leader-'A'] > other.counts[other.leader-'A']; } }; vector<Line> lines; list<Line *> items; list<Move> moves; void remove(bool isRow, int id1) { for (auto it = items.begin(); it != items.end(); ++it) { if (isRow == (*it)->isRow) { continue; } if (isRow) { char color = dia[id1][(*it)->id - 1]; (*it)->remove(color); } else { char color = dia[(*it)->id - 1][id1]; (*it)->remove(color); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { string string; cin >> string; int j = 0; for (char &c: string) { dia[i][j++] = c; } } for (int i = 0; i < n; i++) { lines.push_back(Line(true, 0, m - 1, i, i)); } for (int j = 0; j < m; j++) { lines.push_back(Line(false, j, j, 0, n - 1)); } sort(lines.begin(), lines.end()); for (Line &line: lines) { items.push_back(&line); } int res = 0; while (!items.empty()) { for (auto it = items.begin(); it != items.end(); ++it) { if ((*it)->isReady()) { if (!(*it)->isEmpty()) { res++; moves.push_front(Move((*it)->isRow, (*it)->id, (*it)->leader)); remove((*it)->isRow, (*it)->id - 1); } items.erase(it); break; } } } cout << res << endl; for (Move &move: moves) { cout << (move.isRow ? 'R' : 'K') << " " << move.id << " " << move.color << endl; } return 0; }
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 | #include <iostream> #include <vector> #include <list> #include <algorithm> #define LIMIT 2001 using namespace std; int n, m; char dia[LIMIT][LIMIT]; class Move { public: bool isRow; int id; char color; Move(bool isRow2, int id2, char color2) : isRow(isRow2), id(id2), color(color2) {} }; class Line { public: int empty = 0; int counts[26] = {0}; char leader; bool isRow; int id; Line(bool isRow, int minx, int maxx, int miny, int maxy) { this->isRow = isRow; id = isRow ? miny + 1 : minx + 1; int max = 0; for (int y = miny; y <= maxy; y++) { for (int x = minx; x <= maxx; x++) { counts[dia[y][x] - 'A']++; if (counts[dia[y][x] - 'A'] > max) { max = counts[dia[y][x] - 'A']; leader = dia[y][x]; } } } } void remove(char color) { counts[color - 'A']--; empty++; int max = 0; for (char c = 'A'; c <= 'Z'; c++) { if (counts[c - 'A'] > max) { max = counts[c - 'A']; leader = c; } } } bool isReady() { return empty + counts[leader - 'A'] == getCount(); } bool isEmpty() { return empty == getCount(); } int getCount() const { return isRow ? m : n; } bool operator<(const Line &other) const { return counts[leader-'A'] > other.counts[other.leader-'A']; } }; vector<Line> lines; list<Line *> items; list<Move> moves; void remove(bool isRow, int id1) { for (auto it = items.begin(); it != items.end(); ++it) { if (isRow == (*it)->isRow) { continue; } if (isRow) { char color = dia[id1][(*it)->id - 1]; (*it)->remove(color); } else { char color = dia[(*it)->id - 1][id1]; (*it)->remove(color); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { string string; cin >> string; int j = 0; for (char &c: string) { dia[i][j++] = c; } } for (int i = 0; i < n; i++) { lines.push_back(Line(true, 0, m - 1, i, i)); } for (int j = 0; j < m; j++) { lines.push_back(Line(false, j, j, 0, n - 1)); } sort(lines.begin(), lines.end()); for (Line &line: lines) { items.push_back(&line); } int res = 0; while (!items.empty()) { for (auto it = items.begin(); it != items.end(); ++it) { if ((*it)->isReady()) { if (!(*it)->isEmpty()) { res++; moves.push_front(Move((*it)->isRow, (*it)->id, (*it)->leader)); remove((*it)->isRow, (*it)->id - 1); } items.erase(it); break; } } } cout << res << endl; for (Move &move: moves) { cout << (move.isRow ? 'R' : 'K') << " " << move.id << " " << move.color << endl; } return 0; } |