#include <bits/stdc++.h> #define ft first #define sc second #define pb push_back #define FOR(i,n) for(int i=0; i<n; i++) #define FORX(i,a,b) for(int i=(a); i<=(b); i++) #define watch(x) cerr << (#x) << " == " << (x) << endl typedef long long ll; typedef long double ld; using namespace std; char tab[2005][2005]; int col_cnt[2005][30], row_cnt[2005][30]; vector<tuple<char, int, char>> ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; FOR(y,n){ FOR(x,m){ cin>>tab[y][x]; col_cnt[x][tab[y][x]-'A']++; row_cnt[y][tab[y][x]-'A']++; } } int moves; do { moves = 0; FOR(y,n) { int cnt = 0; char col; FOR(i,'Z'-'A'+1){ if (row_cnt[y][i]) { cnt++; col = 'A'+i; if (cnt > 1) {break;} } } if (cnt == 1) { moves++; ans.pb({'R', y+1, col}); FOR(x,m) { if ('A' <= tab[y][x] && tab[y][x] <= 'Z') { row_cnt[y][tab[y][x]-'A']--; col_cnt[x][tab[y][x]-'A']--; tab[y][x] = 0; } } } } FOR(x,m) { int cnt = 0; char col; FOR(i,'Z'-'A'+1){ if (col_cnt[x][i]) { cnt++; col = 'A'+i; if (cnt > 1) {break;} } } if (cnt == 1) { moves++; ans.pb({'K', x+1, col}); FOR(y,n) { if ('A' <= tab[y][x] && tab[y][x] <= 'Z') { row_cnt[y][tab[y][x]-'A']--; col_cnt[x][tab[y][x]-'A']--; tab[y][x] = 0; } } } } } while (moves); cout<<ans.size()<<"\n"; reverse(ans.begin(), ans.end()); for (auto &[a, b, c] : ans) { cout<<a<<" "<<b<<" "<<c<<"\n"; } 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 | #include <bits/stdc++.h> #define ft first #define sc second #define pb push_back #define FOR(i,n) for(int i=0; i<n; i++) #define FORX(i,a,b) for(int i=(a); i<=(b); i++) #define watch(x) cerr << (#x) << " == " << (x) << endl typedef long long ll; typedef long double ld; using namespace std; char tab[2005][2005]; int col_cnt[2005][30], row_cnt[2005][30]; vector<tuple<char, int, char>> ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; FOR(y,n){ FOR(x,m){ cin>>tab[y][x]; col_cnt[x][tab[y][x]-'A']++; row_cnt[y][tab[y][x]-'A']++; } } int moves; do { moves = 0; FOR(y,n) { int cnt = 0; char col; FOR(i,'Z'-'A'+1){ if (row_cnt[y][i]) { cnt++; col = 'A'+i; if (cnt > 1) {break;} } } if (cnt == 1) { moves++; ans.pb({'R', y+1, col}); FOR(x,m) { if ('A' <= tab[y][x] && tab[y][x] <= 'Z') { row_cnt[y][tab[y][x]-'A']--; col_cnt[x][tab[y][x]-'A']--; tab[y][x] = 0; } } } } FOR(x,m) { int cnt = 0; char col; FOR(i,'Z'-'A'+1){ if (col_cnt[x][i]) { cnt++; col = 'A'+i; if (cnt > 1) {break;} } } if (cnt == 1) { moves++; ans.pb({'K', x+1, col}); FOR(y,n) { if ('A' <= tab[y][x] && tab[y][x] <= 'Z') { row_cnt[y][tab[y][x]-'A']--; col_cnt[x][tab[y][x]-'A']--; tab[y][x] = 0; } } } } } while (moves); cout<<ans.size()<<"\n"; reverse(ans.begin(), ans.end()); for (auto &[a, b, c] : ans) { cout<<a<<" "<<b<<" "<<c<<"\n"; } return 0; } |