#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define PB push_back
typedef long long ll;
typedef long double ld;
#define REP(x,y) for(int x=0;x<(y);x++)
#define ROF(x,y) for(int x=(y);x>0;x--)
#define FOR(x,z,y) for(int x=(z);x<(y);x++)
#define INT(x) int x;scanf("%d ",&x)
#define LL(x) ll x;scanf("%lld",&x)
#define CZ(x) char x;scanf(" %c",&x)
typedef pair<int,bool> pib;
char t[2005][2005];
set<int> cols,rows;
vector<pair<char,pib>> vec;
short row[2005],col[2005];
queue<pib> q;
int main(){
INT(n); INT(m);
REP(i,n){
fgets(t[i],m+2,stdin);
}
REP(i,n){
char color = t[i][0];
FOR(j,1,m){
if(t[i][j]!=color){
row[i]++;
color = t[i][j];
}
}
if(row[i]==0) q.push({i,0});
rows.insert(i);
}
REP(i,m){
char color = t[0][i];
FOR(j,1,n){
if(t[j][i]!=color){
col[i]++;
color = t[j][i];
}
}
if(col[i]==0) q.push({i,1});
cols.insert(i);
}
while(!q.empty()){
pib p = q.front();
q.pop();
if(p.s){
auto it = cols.find(p.f);
int _prev = p.f==*cols.begin() ? p.f: *prev(it,1);
int _next = p.f==*cols.rbegin() ? p.f: *next(it,1);
vec.PB({t[*(rows.begin())][p.f],{p.f,1}});
for(int i:rows){
if((t[i][p.f]!=t[i][_next] && t[i][p.f]!=t[i][_prev]) || (p.f==_next && t[i][p.f]!=t[i][_prev]) || (p.f==_prev && t[i][p.f]!=t[i][_next])){
if(t[i][_prev]==t[i][_next])row[i]-=2;
else row[i]--;
if(row[i]==0) q.push({i,0});
}
}
cols.erase(p.f);
}else{
auto it = rows.find(p.f);
int _prev = p.f==*rows.begin() ? p.f: *prev(it,1);
int _next = p.f==*rows.rbegin() ? p.f: *next(it,1);
vec.PB({t[p.f][*(cols.begin())],{p.f,0}});
for(int i:cols){
if((t[p.f][i]!=t[_next][i] && t[p.f][i]!=t[_prev][i]) || (p.f==_next && t[p.f][i]!=t[_prev][i]) || (p.f==_prev && t[p.f][i]!=t[_next][i])){
if(t[_prev][i]==t[_next][i])col[i]-=2;
else col[i]--;
if(col[i]==0) q.push({i,1});
}
}
rows.erase(p.f);
}
if(rows.size()==0 || cols.size()==0) break;
}
reverse(vec.begin(),vec.end());
printf("%d\n",vec.size());
for(auto p:vec){
printf("%c %d %c\n",p.s.s ? 'K' : 'R' ,p.s.f+1,p.f);
}
}
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 | #include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x,y) for(int x=0;x<(y);x++) #define ROF(x,y) for(int x=(y);x>0;x--) #define FOR(x,z,y) for(int x=(z);x<(y);x++) #define INT(x) int x;scanf("%d ",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int,bool> pib; char t[2005][2005]; set<int> cols,rows; vector<pair<char,pib>> vec; short row[2005],col[2005]; queue<pib> q; int main(){ INT(n); INT(m); REP(i,n){ fgets(t[i],m+2,stdin); } REP(i,n){ char color = t[i][0]; FOR(j,1,m){ if(t[i][j]!=color){ row[i]++; color = t[i][j]; } } if(row[i]==0) q.push({i,0}); rows.insert(i); } REP(i,m){ char color = t[0][i]; FOR(j,1,n){ if(t[j][i]!=color){ col[i]++; color = t[j][i]; } } if(col[i]==0) q.push({i,1}); cols.insert(i); } while(!q.empty()){ pib p = q.front(); q.pop(); if(p.s){ auto it = cols.find(p.f); int _prev = p.f==*cols.begin() ? p.f: *prev(it,1); int _next = p.f==*cols.rbegin() ? p.f: *next(it,1); vec.PB({t[*(rows.begin())][p.f],{p.f,1}}); for(int i:rows){ if((t[i][p.f]!=t[i][_next] && t[i][p.f]!=t[i][_prev]) || (p.f==_next && t[i][p.f]!=t[i][_prev]) || (p.f==_prev && t[i][p.f]!=t[i][_next])){ if(t[i][_prev]==t[i][_next])row[i]-=2; else row[i]--; if(row[i]==0) q.push({i,0}); } } cols.erase(p.f); }else{ auto it = rows.find(p.f); int _prev = p.f==*rows.begin() ? p.f: *prev(it,1); int _next = p.f==*rows.rbegin() ? p.f: *next(it,1); vec.PB({t[p.f][*(cols.begin())],{p.f,0}}); for(int i:cols){ if((t[p.f][i]!=t[_next][i] && t[p.f][i]!=t[_prev][i]) || (p.f==_next && t[p.f][i]!=t[_prev][i]) || (p.f==_prev && t[p.f][i]!=t[_next][i])){ if(t[_prev][i]==t[_next][i])col[i]-=2; else col[i]--; if(col[i]==0) q.push({i,1}); } } rows.erase(p.f); } if(rows.size()==0 || cols.size()==0) break; } reverse(vec.begin(),vec.end()); printf("%d\n",vec.size()); for(auto p:vec){ printf("%c %d %c\n",p.s.s ? 'K' : 'R' ,p.s.f+1,p.f); } } |
English