#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define mt make_tuple #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using pll = pair<ll, ll>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x) const int nax = 2005; int n, m; char a[nax][nax]; int row[nax][26]; int col[nax][26]; int cntrow[nax]; int cntcol[nax]; bool vis[nax][nax]; queue<tuple<int, int, char>> todo; vector<tuple<int, int, char>> ans; void zabijrow(int r){ rep(i, 1, m){ if(vis[r][i]) continue; vis[r][i] = true; col[i][a[r][i] - 'A'] -= 1; if(col[i][a[r][i] - 'A'] == 0){ cntcol[i] -= 1; if(cntcol[i] == 1){ char cur = 'A'; while(col[i][cur - 'A'] == 0) cur += 1; todo.push(mt(1, i, cur)); } } } } void zabijcol(int c){ rep(i, 1, n){ if(vis[i][c]) continue; vis[i][c] = true; row[i][a[i][c] - 'A'] -= 1; if(row[i][a[i][c] - 'A'] == 0){ cntrow[i] -= 1; if(cntrow[i] == 1){ char cur = 'A'; while(row[i][cur - 'A'] == 0) cur += 1; todo.push(mt(0, i, cur)); } } } } void solve(){ cin >> n >> m; rep(i, 1, n){ string s; cin >> s; rep(j, 1, m) a[i][j] = s[j - 1]; } rep(i, 1, n){ rep(j, 1, m){ row[i][a[i][j] - 'A'] += 1; if(row[i][a[i][j] - 'A'] == 1) cntrow[i] += 1; } } rep(j, 1, m){ rep(i, 1, n){ col[j][a[i][j] - 'A'] += 1; if(col[j][a[i][j] - 'A'] == 1) cntcol[j] += 1; } } // 0 wiersze 1 kolumny rep(i, 1, n){ if(cntrow[i] == 1){ char cur = 'A'; while(row[i][cur - 'A'] == 0) cur += 1; todo.push(mt(0, i, cur)); } } rep(i, 1, m){ if(cntcol[i] == 1){ char cur = 'A'; while(col[i][cur - 'A'] == 0) cur += 1; todo.push(mt(1, i, cur)); } } while(!todo.empty()){ auto [x, y, z] = todo.front(); todo.pop(); ans.pb(mt(x, y, z)); if(x == 0){ zabijrow(y); } else zabijcol(y); } assert(SZ(ans) == n + m); reverse(all(ans)); cout << n + m << "\n"; for(auto [tp, id, w] : ans){ if(tp == 0) cout << "R "; else cout << "K "; cout << id << " "; cout << w << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; rep(te, 1, tt) solve(); 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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define mt make_tuple #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using pll = pair<ll, ll>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x) const int nax = 2005; int n, m; char a[nax][nax]; int row[nax][26]; int col[nax][26]; int cntrow[nax]; int cntcol[nax]; bool vis[nax][nax]; queue<tuple<int, int, char>> todo; vector<tuple<int, int, char>> ans; void zabijrow(int r){ rep(i, 1, m){ if(vis[r][i]) continue; vis[r][i] = true; col[i][a[r][i] - 'A'] -= 1; if(col[i][a[r][i] - 'A'] == 0){ cntcol[i] -= 1; if(cntcol[i] == 1){ char cur = 'A'; while(col[i][cur - 'A'] == 0) cur += 1; todo.push(mt(1, i, cur)); } } } } void zabijcol(int c){ rep(i, 1, n){ if(vis[i][c]) continue; vis[i][c] = true; row[i][a[i][c] - 'A'] -= 1; if(row[i][a[i][c] - 'A'] == 0){ cntrow[i] -= 1; if(cntrow[i] == 1){ char cur = 'A'; while(row[i][cur - 'A'] == 0) cur += 1; todo.push(mt(0, i, cur)); } } } } void solve(){ cin >> n >> m; rep(i, 1, n){ string s; cin >> s; rep(j, 1, m) a[i][j] = s[j - 1]; } rep(i, 1, n){ rep(j, 1, m){ row[i][a[i][j] - 'A'] += 1; if(row[i][a[i][j] - 'A'] == 1) cntrow[i] += 1; } } rep(j, 1, m){ rep(i, 1, n){ col[j][a[i][j] - 'A'] += 1; if(col[j][a[i][j] - 'A'] == 1) cntcol[j] += 1; } } // 0 wiersze 1 kolumny rep(i, 1, n){ if(cntrow[i] == 1){ char cur = 'A'; while(row[i][cur - 'A'] == 0) cur += 1; todo.push(mt(0, i, cur)); } } rep(i, 1, m){ if(cntcol[i] == 1){ char cur = 'A'; while(col[i][cur - 'A'] == 0) cur += 1; todo.push(mt(1, i, cur)); } } while(!todo.empty()){ auto [x, y, z] = todo.front(); todo.pop(); ans.pb(mt(x, y, z)); if(x == 0){ zabijrow(y); } else zabijcol(y); } assert(SZ(ans) == n + m); reverse(all(ans)); cout << n + m << "\n"; for(auto [tp, id, w] : ans){ if(tp == 0) cout << "R "; else cout << "K "; cout << id << " "; cout << w << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; rep(te, 1, tt) solve(); return 0; } |