#include <bits/stdc++.h> using namespace std; const int MAXN = 2e3 + 7; map<char, int> cntRow[MAXN]; map<char, int> cntCol[MAXN]; int diffRow[MAXN]; int diffCol[MAXN]; char tab[MAXN][MAXN]; int n, m; vector<pair<char, pair<int, char>>> op; bool inCol[MAXN]; bool inRow[MAXN]; void solve(){ queue<pair<int, int>> q; for(int i = 1; i <= n; i++){ if(diffRow[i] == 1){ q.push({1, i}); //cout << 1 << ' ' << i << '\n'; inRow[i] = true; } } for(int i = 1; i <= m; i++){ if(diffCol[i] == 1){ q.push({2, i}); //cout << 2 << ' ' << i << '\n'; inCol[i] = true; } } while(q.size()){ pair<int, int> akt = q.front(); q.pop(); int i = akt.second; if(akt.first == 1){ //row char nasz = '?'; for(int j = 1; j <= m; j++){ if(tab[i][j] != '?'){ nasz = tab[i][j]; cntCol[j][tab[i][j]]--; if(cntCol[j][tab[i][j]] == 0){ diffCol[j]--; } if(diffCol[j] == 1 && !inCol[j]){ q.push({2, j}); inCol[j] = true; } tab[i][j] = '?'; } } if(nasz == '?'){ continue; } op.push_back({'R', {i, nasz}}); }else{ //column char nasz = '?'; for(int j = 1; j <= n; j++){ if(tab[j][i] != '?'){ nasz = tab[j][i]; cntRow[j][tab[j][i]]--; if(cntRow[j][tab[j][i]] == 0){ diffRow[j]--; } if(diffRow[j] == 1 && !inRow[j]){ q.push({1, j}); inRow[j] = true; } tab[j][i] = '?'; } } if(nasz == '?'){ continue; } op.push_back({'K', {i, nasz}}); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; string s; for(int i = 1; i <= n; i++){ cin >> s; for(int j = 0; j < m; j++){ tab[i][j + 1] = s[j]; cntRow[i][s[j]]++; if(cntRow[i][s[j]] == 1){ diffRow[i]++; } cntCol[j + 1][s[j]]++; if(cntCol[j + 1][s[j]] == 1){ diffCol[j + 1]++; } } } solve(); reverse(op.begin(), op.end()); cout << (int)op.size() << '\n'; for(auto ele : op){ cout << ele.first << ' ' << ele.second.first << ' ' << ele.second.second << '\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 112 113 114 115 116 117 | #include <bits/stdc++.h> using namespace std; const int MAXN = 2e3 + 7; map<char, int> cntRow[MAXN]; map<char, int> cntCol[MAXN]; int diffRow[MAXN]; int diffCol[MAXN]; char tab[MAXN][MAXN]; int n, m; vector<pair<char, pair<int, char>>> op; bool inCol[MAXN]; bool inRow[MAXN]; void solve(){ queue<pair<int, int>> q; for(int i = 1; i <= n; i++){ if(diffRow[i] == 1){ q.push({1, i}); //cout << 1 << ' ' << i << '\n'; inRow[i] = true; } } for(int i = 1; i <= m; i++){ if(diffCol[i] == 1){ q.push({2, i}); //cout << 2 << ' ' << i << '\n'; inCol[i] = true; } } while(q.size()){ pair<int, int> akt = q.front(); q.pop(); int i = akt.second; if(akt.first == 1){ //row char nasz = '?'; for(int j = 1; j <= m; j++){ if(tab[i][j] != '?'){ nasz = tab[i][j]; cntCol[j][tab[i][j]]--; if(cntCol[j][tab[i][j]] == 0){ diffCol[j]--; } if(diffCol[j] == 1 && !inCol[j]){ q.push({2, j}); inCol[j] = true; } tab[i][j] = '?'; } } if(nasz == '?'){ continue; } op.push_back({'R', {i, nasz}}); }else{ //column char nasz = '?'; for(int j = 1; j <= n; j++){ if(tab[j][i] != '?'){ nasz = tab[j][i]; cntRow[j][tab[j][i]]--; if(cntRow[j][tab[j][i]] == 0){ diffRow[j]--; } if(diffRow[j] == 1 && !inRow[j]){ q.push({1, j}); inRow[j] = true; } tab[j][i] = '?'; } } if(nasz == '?'){ continue; } op.push_back({'K', {i, nasz}}); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; string s; for(int i = 1; i <= n; i++){ cin >> s; for(int j = 0; j < m; j++){ tab[i][j + 1] = s[j]; cntRow[i][s[j]]++; if(cntRow[i][s[j]] == 1){ diffRow[i]++; } cntCol[j + 1][s[j]]++; if(cntCol[j + 1][s[j]] == 1){ diffCol[j + 1]++; } } } solve(); reverse(op.begin(), op.end()); cout << (int)op.size() << '\n'; for(auto ele : op){ cout << ele.first << ' ' << ele.second.first << ' ' << ele.second.second << '\n'; } return 0; } |