#include <bits/stdc++.h> #ifdef dbg #define D(...) fprintf(stderr, __VA_ARGS__) #define DD(...) D("[Debug] "#__VA_ARGS__ " = "), \ debug_helper::debug(__VA_ARGS__), D("\n") #include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp" #else #define D(...) ((void)0) #define DD(...) ((void)0) #endif #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; template<class T> void chkmn(T &x, T y) { if (y < x) x = y; } template<class T> void chkmx(T &x, T y) { if (y > x) x = y; } using namespace std; const int maxn = 2010; int n, m, a[maxn][maxn], c1[maxn][30], c2[maxn][30]; int v1[maxn], v2[maxn], r1[maxn], r2[maxn]; char s[maxn]; vector<tuple<char, int, char>> ans; void del(int x, int y) { if (!a[x][y]) return; c1[x][a[x][y]]--; if (!c1[x][a[x][y]]) r1[x]--; c2[y][a[x][y]]--; if (!c2[y][a[x][y]]) r2[y]--; a[x][y] = 0; } int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n >> m; rep (i, 1, n) { cin >> s; rep (j, 1, m) a[i][j] = s[j - 1] - 'A' + 1; } rep (i, 1, n) rep (j, 1, m) c1[i][a[i][j]]++; rep (i, 1, n) rep (j, 1, m) c2[j][a[i][j]]++; rep (i, 1, n) rep (j, 1, 26) if (c1[i][j]) r1[i]++; rep (i, 1, m) rep (j, 1, 26) if (c2[i][j]) r2[i]++; rep (_, 1, n + m) { int id = 0, ty = 0; rep (i, 1, n) if (!v1[i] && r1[i] == 1) id = i, ty = 1; rep (i, 1, m) if (!v2[i] && r2[i] == 1) id = i, ty = 2; if (!id) { int ok = 1; rep (i, 1, n) if (r1[i]) ok = 0; rep (i, 1, m) if (r2[i]) ok = 0; if (!ok) return cout << "shz sb\n", 0; break; } if (ty == 1) { int qwq = 0; rep (j, 1, 26) if (c1[id][j]) qwq = j; ans.emplace_back('R', id, 'A' + qwq - 1); v1[id] = 1; rep (j, 1, m) del(id, j); } else { int qwq = 0; rep (j, 1, 26) if (c2[id][j]) qwq = j; ans.emplace_back('K', id, 'A' + qwq - 1); v2[id] = 1; rep (j, 1, n) del(j, id); } } reverse(ALL(ans)); cout << SZ(ans) << '\n'; for (auto [x, y, z] : ans) cout << x << " " << y << " " << z << '\n'; }
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 | #include <bits/stdc++.h> #ifdef dbg #define D(...) fprintf(stderr, __VA_ARGS__) #define DD(...) D("[Debug] "#__VA_ARGS__ " = "), \ debug_helper::debug(__VA_ARGS__), D("\n") #include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp" #else #define D(...) ((void)0) #define DD(...) ((void)0) #endif #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; template<class T> void chkmn(T &x, T y) { if (y < x) x = y; } template<class T> void chkmx(T &x, T y) { if (y > x) x = y; } using namespace std; const int maxn = 2010; int n, m, a[maxn][maxn], c1[maxn][30], c2[maxn][30]; int v1[maxn], v2[maxn], r1[maxn], r2[maxn]; char s[maxn]; vector<tuple<char, int, char>> ans; void del(int x, int y) { if (!a[x][y]) return; c1[x][a[x][y]]--; if (!c1[x][a[x][y]]) r1[x]--; c2[y][a[x][y]]--; if (!c2[y][a[x][y]]) r2[y]--; a[x][y] = 0; } int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n >> m; rep (i, 1, n) { cin >> s; rep (j, 1, m) a[i][j] = s[j - 1] - 'A' + 1; } rep (i, 1, n) rep (j, 1, m) c1[i][a[i][j]]++; rep (i, 1, n) rep (j, 1, m) c2[j][a[i][j]]++; rep (i, 1, n) rep (j, 1, 26) if (c1[i][j]) r1[i]++; rep (i, 1, m) rep (j, 1, 26) if (c2[i][j]) r2[i]++; rep (_, 1, n + m) { int id = 0, ty = 0; rep (i, 1, n) if (!v1[i] && r1[i] == 1) id = i, ty = 1; rep (i, 1, m) if (!v2[i] && r2[i] == 1) id = i, ty = 2; if (!id) { int ok = 1; rep (i, 1, n) if (r1[i]) ok = 0; rep (i, 1, m) if (r2[i]) ok = 0; if (!ok) return cout << "shz sb\n", 0; break; } if (ty == 1) { int qwq = 0; rep (j, 1, 26) if (c1[id][j]) qwq = j; ans.emplace_back('R', id, 'A' + qwq - 1); v1[id] = 1; rep (j, 1, m) del(id, j); } else { int qwq = 0; rep (j, 1, 26) if (c2[id][j]) qwq = j; ans.emplace_back('K', id, 'A' + qwq - 1); v2[id] = 1; rep (j, 1, n) del(j, id); } } reverse(ALL(ans)); cout << SZ(ans) << '\n'; for (auto [x, y, z] : ans) cout << x << " " << y << " " << z << '\n'; } |