#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define i128 __int128 #define mem(x) memset(x,0,sizeof(x)) #define endl "\n" #define printYes cout << "Yes\n" #define printYES cout << "YES\n" #define printNo cout << "No\n" #define printNO cout << "NO\n" #define lowbit(x) ((x)&(-(x))) #define pb push_back #define mkp make_pair #define pii pair<int,int> #define fi first #define se second #define SZ(x) ((int)(x).size()) #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 pcnt(x) __builtin_popcount(x) mt19937 rnd(time(0)); template<class T>void chkmin(T&x,T y){x=min(x,y);} template<class T>void chkmax(T&x,T y){x=max(x,y);} const ll inf=1000000000000000000; //const ll inf=1000000000; //const ll mod=998244353; //const ll mod=1000000007; const int N=2005; int n,m; char a[N][N]; int b[N][26],c[N][26],B[N],C[N]; queue<pii>q; bool visb[N],visc[N]; pair<pii,int> ans[N*2]; int cnt; int query(pii z) { int tp=z.fi,x=z.se; if (tp==0) { rep(i,0,25) if (b[x][i]) return i; } else { rep(i,0,25) if (c[x][i]) return i; } return 0; } void del(int x,int y) { b[x][a[x][y]-'A']--; if (b[x][a[x][y]-'A']==0) { B[x]--; if (B[x]==1&&visb[x]==0) q.push(mkp(0,x)); } c[y][a[x][y]-'A']--; if (c[y][a[x][y]-'A']==0) { C[y]--; if (C[y]==1&&visc[y]==0) q.push(mkp(1,y)); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin >> n >> m; rep(i,1,n) rep(j,1,m) cin >> a[i][j]; rep(i,1,n) rep(j,1,m) { b[i][a[i][j]-'A']++; c[j][a[i][j]-'A']++; } rep(i,1,n) rep(j,0,25) if (b[i][j]) B[i]++; rep(i,1,m) rep(j,0,25) if (c[i][j]) C[i]++; rep(i,1,n) if (B[i]==1) q.push(mkp(0,i)); rep(i,1,m) if (C[i]==1) q.push(mkp(1,i)); while (!q.empty()) { ans[++cnt]=mkp(q.front(),query(q.front())); int tp=q.front().fi,x=q.front().se; q.pop(); if (tp==0) { visb[x]=1; rep(i,1,m) if (visc[i]==0) del(x,i); } else { visc[x]=1; rep(i,1,n) if (visb[i]==0) del(i,x); } } cout << cnt << endl; per(i,cnt,1) { if (ans[i].fi.fi==0) cout << "R " << ans[i].fi.se << " " << (char)(ans[i].se+'A') << endl; else cout << "K " << ans[i].fi.se << " " << (char)(ans[i].se+'A') << 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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define i128 __int128 #define mem(x) memset(x,0,sizeof(x)) #define endl "\n" #define printYes cout << "Yes\n" #define printYES cout << "YES\n" #define printNo cout << "No\n" #define printNO cout << "NO\n" #define lowbit(x) ((x)&(-(x))) #define pb push_back #define mkp make_pair #define pii pair<int,int> #define fi first #define se second #define SZ(x) ((int)(x).size()) #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 pcnt(x) __builtin_popcount(x) mt19937 rnd(time(0)); template<class T>void chkmin(T&x,T y){x=min(x,y);} template<class T>void chkmax(T&x,T y){x=max(x,y);} const ll inf=1000000000000000000; //const ll inf=1000000000; //const ll mod=998244353; //const ll mod=1000000007; const int N=2005; int n,m; char a[N][N]; int b[N][26],c[N][26],B[N],C[N]; queue<pii>q; bool visb[N],visc[N]; pair<pii,int> ans[N*2]; int cnt; int query(pii z) { int tp=z.fi,x=z.se; if (tp==0) { rep(i,0,25) if (b[x][i]) return i; } else { rep(i,0,25) if (c[x][i]) return i; } return 0; } void del(int x,int y) { b[x][a[x][y]-'A']--; if (b[x][a[x][y]-'A']==0) { B[x]--; if (B[x]==1&&visb[x]==0) q.push(mkp(0,x)); } c[y][a[x][y]-'A']--; if (c[y][a[x][y]-'A']==0) { C[y]--; if (C[y]==1&&visc[y]==0) q.push(mkp(1,y)); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin >> n >> m; rep(i,1,n) rep(j,1,m) cin >> a[i][j]; rep(i,1,n) rep(j,1,m) { b[i][a[i][j]-'A']++; c[j][a[i][j]-'A']++; } rep(i,1,n) rep(j,0,25) if (b[i][j]) B[i]++; rep(i,1,m) rep(j,0,25) if (c[i][j]) C[i]++; rep(i,1,n) if (B[i]==1) q.push(mkp(0,i)); rep(i,1,m) if (C[i]==1) q.push(mkp(1,i)); while (!q.empty()) { ans[++cnt]=mkp(q.front(),query(q.front())); int tp=q.front().fi,x=q.front().se; q.pop(); if (tp==0) { visb[x]=1; rep(i,1,m) if (visc[i]==0) del(x,i); } else { visc[x]=1; rep(i,1,n) if (visb[i]==0) del(i,x); } } cout << cnt << endl; per(i,cnt,1) { if (ans[i].fi.fi==0) cout << "R " << ans[i].fi.se << " " << (char)(ans[i].se+'A') << endl; else cout << "K " << ans[i].fi.se << " " << (char)(ans[i].se+'A') << endl; } return 0; } |