#include <cstdio>
#include <queue>
using namespace std;
int n, m, dn, dm;
int gra[2010][2010];
int kol[2010][30];
int wie[2010][30];
queue<pair<int, int> > q_kol;
queue<pair<int, int> > q_wie;
vector<pair<int, int> > res;
vector<bool> leniwy;
bool b_kol[2010];
bool b_wie[2010];
int main()
{
scanf("%d %d", &n, &m);
dn = n;
dm = m;
for(int i = 0; i < n; i++)
{
char linia[m];
scanf("%s", linia);
for(int j = 0; j < m; j++)
{
int ele = linia[j] - 'A';
gra[i][j] = ele;
wie[i][ele]++;
kol[j][ele]++;
}
}
while(true)
{
while(!q_wie.empty())
{
pair<int, int> w_i = q_wie.front();
q_wie.pop();
res.push_back(w_i);
leniwy.push_back(0);
b_wie[w_i.first] = 1;
dn--;
for(int j = 0; j < m; j++) if(!b_kol[j]) kol[j][w_i.second]--;
}
if(dn == 0) break;
for(int j = 0; j < m; j++)
{
if(b_kol[j]) continue;
for(int c = 0; c < 'Z' - 'A' + 1; c++) if(kol[j][c] == dn) q_kol.push(make_pair(j, c));
}
while(!q_kol.empty())
{
pair<int, char> k_j = q_kol.front();
q_kol.pop();
res.push_back(k_j);
leniwy.push_back(1);
b_kol[k_j.first] = 1;
dm--;
for(int i = 0; i < n; i++) if(!b_wie[i]) wie[i][k_j.second]--;
}
if(dm == 0) break;
for(int i = 0; i < n; i++)
{
if(b_wie[i]) continue;
for(int c = 0; c < 'Z' - 'A' + 1; c++) if(wie[i][c] == dm) q_wie.push(make_pair(i, c));
}
}
printf("%d\n", res.size());
for(int i = res.size() - 1; i >= 0; i--)
{
if(leniwy[i]) printf("K %d %c\n", res[i].first + 1, res[i].second + 'A');
else printf("R %d %c\n", res[i].first + 1, res[i].second + 'A');
}
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 | #include <cstdio> #include <queue> using namespace std; int n, m, dn, dm; int gra[2010][2010]; int kol[2010][30]; int wie[2010][30]; queue<pair<int, int> > q_kol; queue<pair<int, int> > q_wie; vector<pair<int, int> > res; vector<bool> leniwy; bool b_kol[2010]; bool b_wie[2010]; int main() { scanf("%d %d", &n, &m); dn = n; dm = m; for(int i = 0; i < n; i++) { char linia[m]; scanf("%s", linia); for(int j = 0; j < m; j++) { int ele = linia[j] - 'A'; gra[i][j] = ele; wie[i][ele]++; kol[j][ele]++; } } while(true) { while(!q_wie.empty()) { pair<int, int> w_i = q_wie.front(); q_wie.pop(); res.push_back(w_i); leniwy.push_back(0); b_wie[w_i.first] = 1; dn--; for(int j = 0; j < m; j++) if(!b_kol[j]) kol[j][w_i.second]--; } if(dn == 0) break; for(int j = 0; j < m; j++) { if(b_kol[j]) continue; for(int c = 0; c < 'Z' - 'A' + 1; c++) if(kol[j][c] == dn) q_kol.push(make_pair(j, c)); } while(!q_kol.empty()) { pair<int, char> k_j = q_kol.front(); q_kol.pop(); res.push_back(k_j); leniwy.push_back(1); b_kol[k_j.first] = 1; dm--; for(int i = 0; i < n; i++) if(!b_wie[i]) wie[i][k_j.second]--; } if(dm == 0) break; for(int i = 0; i < n; i++) { if(b_wie[i]) continue; for(int c = 0; c < 'Z' - 'A' + 1; c++) if(wie[i][c] == dm) q_wie.push(make_pair(i, c)); } } printf("%d\n", res.size()); for(int i = res.size() - 1; i >= 0; i--) { if(leniwy[i]) printf("K %d %c\n", res[i].first + 1, res[i].second + 'A'); else printf("R %d %c\n", res[i].first + 1, res[i].second + 'A'); } return 0; } |
English