#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define pb push_back
#define fi first
#define se second
#define rep(i,b,e) for(int i=(b); i<(e); i++)
#define each(a,x) for(auto &a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; }
template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) {
out << "{";
bool fi = 1;
for(auto b : a) {
if(fi) {out<<b; fi=0;}
else out<<", "<<b;
}
return out << "}";
}
void dump(){
cerr << "\e"<<"\n";
}
template <class T, class... Ts> void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
#define debug(...) cerr << "\e"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
//#define debug(...) ;
const int Z = 30;
const int N = 2005;
int cntX[N][Z],cntY[N][Z];
char grid[N][N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int Y,X;
cin >> Y >> X;
rep(y,1,Y+1) rep(x,1,X+1){
cin >> grid[x][y];
++cntX[x][grid[x][y]-'A'];
++cntY[y][grid[x][y]-'A'];
}
bool go=1;
vector<pair<char,pair<int,char>>> wynik;
while(go){
go = 0;
// Kolumny
for(int x=1; x<=X; ++x){
int cntDiff=0;
char let='?';
for(char c='A'; c<='Z'; ++c){
if(cntX[x][c-'A']>0){
let = c;
++cntDiff;
}
}
if(cntDiff==1){
go = 1;
wynik.pb({'K',{x,let}});
for(int y=1; y<=Y; ++y){
char &toRem = grid[x][y];
if(toRem=='.') continue;
--cntX[x][toRem-'A'];
--cntY[y][toRem-'A'];
grid[x][y]='.';
}
}
}
// Wierszy
for(int y=1; y<=Y; ++y){
int cntDiff=0;
char let='?';
for(char c='A'; c<='Z'; ++c){
if(cntY[y][c-'A']>0){
let = c;
++cntDiff;
}
}
if(cntDiff==1){
go = 1;
wynik.pb({'R',{y,let}});
for(int x=1; x<=X; ++x){
char &toRem = grid[x][y];
if(toRem=='.') continue;
--cntX[x][toRem-'A'];
--cntY[y][toRem-'A'];
grid[x][y]='.';
}
}
}
}
reverse(all(wynik));
cout << sz(wynik) << "\n";
for(auto &[a,bc] : wynik){
auto &[b,c] = bc;
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double db; typedef pair<int,int> pii; typedef vector<int> vi; #define pb push_back #define fi first #define se second #define rep(i,b,e) for(int i=(b); i<(e); i++) #define each(a,x) for(auto &a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } void dump(){ cerr << "\e"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) //#define debug(...) ; const int Z = 30; const int N = 2005; int cntX[N][Z],cntY[N][Z]; char grid[N][N]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int Y,X; cin >> Y >> X; rep(y,1,Y+1) rep(x,1,X+1){ cin >> grid[x][y]; ++cntX[x][grid[x][y]-'A']; ++cntY[y][grid[x][y]-'A']; } bool go=1; vector<pair<char,pair<int,char>>> wynik; while(go){ go = 0; // Kolumny for(int x=1; x<=X; ++x){ int cntDiff=0; char let='?'; for(char c='A'; c<='Z'; ++c){ if(cntX[x][c-'A']>0){ let = c; ++cntDiff; } } if(cntDiff==1){ go = 1; wynik.pb({'K',{x,let}}); for(int y=1; y<=Y; ++y){ char &toRem = grid[x][y]; if(toRem=='.') continue; --cntX[x][toRem-'A']; --cntY[y][toRem-'A']; grid[x][y]='.'; } } } // Wierszy for(int y=1; y<=Y; ++y){ int cntDiff=0; char let='?'; for(char c='A'; c<='Z'; ++c){ if(cntY[y][c-'A']>0){ let = c; ++cntDiff; } } if(cntDiff==1){ go = 1; wynik.pb({'R',{y,let}}); for(int x=1; x<=X; ++x){ char &toRem = grid[x][y]; if(toRem=='.') continue; --cntX[x][toRem-'A']; --cntY[y][toRem-'A']; grid[x][y]='.'; } } } } reverse(all(wynik)); cout << sz(wynik) << "\n"; for(auto &[a,bc] : wynik){ auto &[b,c] = bc; cout << a << " " << b << " " << c << "\n"; } return 0; } |
English