#include "bits/stdc++.h" using namespace std; using ll = long long; int const N = 2002; int n, m, row[N][26], col[N][26]; bitset <26> r[N], c[N]; vector <pair<char, pair<char, int>>> v; char sign, a[N][N]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cin >> sign; a[i][j] = sign; col[j][sign - 'A'] += 1; row[i][sign - 'A'] += 1; c[j][sign - 'A'] = 1; r[i][sign - 'A'] = 1; } } while(true){ vector <int> columns, rows; for(int i = 1; i <= m; ++i) if(c[i].count() == 1) columns.push_back(i); for(auto x : columns){ char act = ' '; for(int i = 1; i <= n; ++i){ if(a[i][x] == 'x') continue; act = a[i][x]; a[i][x] = 'x'; col[x][act - 'A'] -= 1; row[i][act - 'A'] -= 1; if(col[x][act - 'A'] == 0) c[x][act - 'A'] = 0; if(row[i][act - 'A'] == 0) r[i][act - 'A'] = 0; } v.push_back({'K', {act, x}}); } for(int i = 1; i <= n; ++i) if(r[i].count() == 1) rows.push_back(i); if(columns.size() + rows.size() == 0) break; for(auto x : rows){ char act = ' '; for(int i = 1; i <= m; ++i){ if(a[x][i] == 'x') continue; act = a[x][i]; a[x][i] = 'x'; row[x][act - 'A'] -= 1; col[i][act - 'A'] -= 1; if(row[x][act - 'A'] == 0) r[x][act - 'A'] = 0; if(col[i][act - 'A'] == 0) c[i][act - 'A'] = 0; } v.push_back({'R', {act, x}}); } } reverse(v.begin(), v.end()); cout << v.size() << '\n'; for(auto x : v) cout << x.first << ' ' << x.second.second << ' ' << x.second.first << '\n'; }
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 | #include "bits/stdc++.h" using namespace std; using ll = long long; int const N = 2002; int n, m, row[N][26], col[N][26]; bitset <26> r[N], c[N]; vector <pair<char, pair<char, int>>> v; char sign, a[N][N]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cin >> sign; a[i][j] = sign; col[j][sign - 'A'] += 1; row[i][sign - 'A'] += 1; c[j][sign - 'A'] = 1; r[i][sign - 'A'] = 1; } } while(true){ vector <int> columns, rows; for(int i = 1; i <= m; ++i) if(c[i].count() == 1) columns.push_back(i); for(auto x : columns){ char act = ' '; for(int i = 1; i <= n; ++i){ if(a[i][x] == 'x') continue; act = a[i][x]; a[i][x] = 'x'; col[x][act - 'A'] -= 1; row[i][act - 'A'] -= 1; if(col[x][act - 'A'] == 0) c[x][act - 'A'] = 0; if(row[i][act - 'A'] == 0) r[i][act - 'A'] = 0; } v.push_back({'K', {act, x}}); } for(int i = 1; i <= n; ++i) if(r[i].count() == 1) rows.push_back(i); if(columns.size() + rows.size() == 0) break; for(auto x : rows){ char act = ' '; for(int i = 1; i <= m; ++i){ if(a[x][i] == 'x') continue; act = a[x][i]; a[x][i] = 'x'; row[x][act - 'A'] -= 1; col[i][act - 'A'] -= 1; if(row[x][act - 'A'] == 0) r[x][act - 'A'] = 0; if(col[i][act - 'A'] == 0) c[i][act - 'A'] = 0; } v.push_back({'R', {act, x}}); } } reverse(v.begin(), v.end()); cout << v.size() << '\n'; for(auto x : v) cout << x.first << ' ' << x.second.second << ' ' << x.second.first << '\n'; } |