#include <bits/stdc++.h>
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define f first
#define s second
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
struct akcja{
int color,idx;
bool type;
};
const int N = 2e3+1;
char tab[N][N];
int cnt[N][26][2],dlugosc[N][2],n,m,ile[N][2];
bool tagged[N][2];
char xd[] = {'R','K'};
unordered_set<int>literki[N][2];
int main(){
fast
cin >> n >> m;
for(int i = 1;i <= n;i++){
dlugosc[i][0] = m;
for(int j = 1;j <= m;j++){
dlugosc[i][1] = n;
cin >> tab[i][j];
cnt[i][tab[i][j] - 'A'][0]++;
cnt[j][tab[i][j] - 'A'][1]++;
if(cnt[i][tab[i][j] - 'A'][0] == 1){
ile[i][0]++;
literki[i][0].insert(tab[i][j] - 'A');
}
if(cnt[j][tab[i][j] - 'A'][1] == 1){
ile[j][1]++;
literki[j][1].insert(tab[i][j] - 'A');
}
}
}
queue<akcja> paski;
for(int i = 1;i <= n;i++)
if(ile[i][0] == 1)
for(auto a: literki[i][0]){
paski.push({a,i,0});
break;
}
if(paski.empty())
for(int i = 1;i <= m;i++)
if(ile[i][1] == 1)
for(auto a: literki[i][1]){
paski.push({a,i,1});
}
vector<akcja> res;
while(!paski.empty()){
akcja p = paski.front();
paski.pop();
tagged[p.idx][p.type] = 1;
res.pb(p);
int x;
if(!p.type)x = m;
else x = n;
for(int i = 1;i <= x;i++){
if(tagged[i][!p.type])continue;
if(!p.type){
cnt[i][tab[p.idx][i] - 'A'][!p.type]--;
if(cnt[i][tab[p.idx][i] - 'A'][!p.type] == 0){
ile[i][!p.type]--;
literki[i][!p.type].erase(tab[p.idx][i] - 'A');
}
}
else{
cnt[i][tab[i][p.idx] - 'A'][!p.type]--;
if(cnt[i][tab[i][p.idx] - 'A'][!p.type] == 0){
ile[i][!p.type]--;
literki[i][!p.type].erase(tab[i][p.idx] - 'A');
}
}
if(ile[i][!p.type] == 1){
for(auto a:literki[i][!p.type]){
paski.push({a,i,!p.type});
tagged[i][!p.type] = 1;
break;
}
}
}
}
reverse(res.begin(),res.end());
cout << res.size() << "\n";
for(auto p: res)
cout << xd[p.type] << " " << p.idx << " " << (char)((char)p.color + 'A') << "\n";
}
/*
4 6
FAXAZA
FFXXZZ
FXXXZA
FXXXZZ
*/
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 | #include <bits/stdc++.h> #define ll long long #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pb push_back #define f first #define s second #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; struct akcja{ int color,idx; bool type; }; const int N = 2e3+1; char tab[N][N]; int cnt[N][26][2],dlugosc[N][2],n,m,ile[N][2]; bool tagged[N][2]; char xd[] = {'R','K'}; unordered_set<int>literki[N][2]; int main(){ fast cin >> n >> m; for(int i = 1;i <= n;i++){ dlugosc[i][0] = m; for(int j = 1;j <= m;j++){ dlugosc[i][1] = n; cin >> tab[i][j]; cnt[i][tab[i][j] - 'A'][0]++; cnt[j][tab[i][j] - 'A'][1]++; if(cnt[i][tab[i][j] - 'A'][0] == 1){ ile[i][0]++; literki[i][0].insert(tab[i][j] - 'A'); } if(cnt[j][tab[i][j] - 'A'][1] == 1){ ile[j][1]++; literki[j][1].insert(tab[i][j] - 'A'); } } } queue<akcja> paski; for(int i = 1;i <= n;i++) if(ile[i][0] == 1) for(auto a: literki[i][0]){ paski.push({a,i,0}); break; } if(paski.empty()) for(int i = 1;i <= m;i++) if(ile[i][1] == 1) for(auto a: literki[i][1]){ paski.push({a,i,1}); } vector<akcja> res; while(!paski.empty()){ akcja p = paski.front(); paski.pop(); tagged[p.idx][p.type] = 1; res.pb(p); int x; if(!p.type)x = m; else x = n; for(int i = 1;i <= x;i++){ if(tagged[i][!p.type])continue; if(!p.type){ cnt[i][tab[p.idx][i] - 'A'][!p.type]--; if(cnt[i][tab[p.idx][i] - 'A'][!p.type] == 0){ ile[i][!p.type]--; literki[i][!p.type].erase(tab[p.idx][i] - 'A'); } } else{ cnt[i][tab[i][p.idx] - 'A'][!p.type]--; if(cnt[i][tab[i][p.idx] - 'A'][!p.type] == 0){ ile[i][!p.type]--; literki[i][!p.type].erase(tab[i][p.idx] - 'A'); } } if(ile[i][!p.type] == 1){ for(auto a:literki[i][!p.type]){ paski.push({a,i,!p.type}); tagged[i][!p.type] = 1; break; } } } } reverse(res.begin(),res.end()); cout << res.size() << "\n"; for(auto p: res) cout << xd[p.type] << " " << p.idx << " " << (char)((char)p.color + 'A') << "\n"; } /* 4 6 FAXAZA FFXXZZ FXXXZA FXXXZZ */ |
English