#include<bits/stdc++.h> #define MAXN 2011 using namespace std; struct ans{ char rowCol; int num; char col; }; ans newAns(char rowCol, int num, char col) { ans e; e.rowCol = rowCol; e.num = num; e.col = col; return e; } int n, m; bool o; string tab[MAXN]; map<char, int> col[MAXN]; map<char, int> row[MAXN]; stack<ans> buff; void debug() { for (int i = 0; i < n; i++) { for (pair<char, int> e : row[i]) { cout<<"("<<e.first<<" "<<e.second<<") "; } cout<<"\n"; } cout<<"\n"; for (int j = 0; j < m; j++) { for (pair<char, int> e : col[j]) { cout<<"("<<e.first<<" "<<e.second<<") "; } cout<<"\n"; } cout<<"---\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> tab[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { row[i][tab[i][j]]++; col[j][tab[i][j]]++; } } //debug(); for (int k = 0; k < n + m; k++) { o = false; for (int i = 0; i < n; i++) { if (row[i].size() == 1) { char color = row[i].begin()->first; //cout<<"BUFF: "<<"R "<<i + 1<<" "<<color<<"\n"; buff.push(newAns('R', i + 1, color)); row[i].erase(color); for (int j = 0; j < m; j++) { if (col[j][color] <= 1) { col[j].erase(color); } else { col[j][color]--; } } o = true; break; } } if (o == true) { //debug(); continue; } for (int j = 0; j < m; j++) { if (col[j].size() == 1) { char color = col[j].begin()->first; //cout<<"BUFF: "<<"K "<<j + 1<<" "<<color<<"\n"; buff.push(newAns('K', j + 1, color)); col[j].erase(color); for (int i = 0; i < n; i++) { if (row[i][color] <= 1) { row[i].erase(color); } else { row[i][color]--; } } break; } } //debug(); } cout<<buff.size()<<"\n"; while (!buff.empty()) { ans e = buff.top(); buff.pop(); cout<<e.rowCol<<" "<<e.num<<" "<<e.col<<"\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 | #include<bits/stdc++.h> #define MAXN 2011 using namespace std; struct ans{ char rowCol; int num; char col; }; ans newAns(char rowCol, int num, char col) { ans e; e.rowCol = rowCol; e.num = num; e.col = col; return e; } int n, m; bool o; string tab[MAXN]; map<char, int> col[MAXN]; map<char, int> row[MAXN]; stack<ans> buff; void debug() { for (int i = 0; i < n; i++) { for (pair<char, int> e : row[i]) { cout<<"("<<e.first<<" "<<e.second<<") "; } cout<<"\n"; } cout<<"\n"; for (int j = 0; j < m; j++) { for (pair<char, int> e : col[j]) { cout<<"("<<e.first<<" "<<e.second<<") "; } cout<<"\n"; } cout<<"---\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> tab[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { row[i][tab[i][j]]++; col[j][tab[i][j]]++; } } //debug(); for (int k = 0; k < n + m; k++) { o = false; for (int i = 0; i < n; i++) { if (row[i].size() == 1) { char color = row[i].begin()->first; //cout<<"BUFF: "<<"R "<<i + 1<<" "<<color<<"\n"; buff.push(newAns('R', i + 1, color)); row[i].erase(color); for (int j = 0; j < m; j++) { if (col[j][color] <= 1) { col[j].erase(color); } else { col[j][color]--; } } o = true; break; } } if (o == true) { //debug(); continue; } for (int j = 0; j < m; j++) { if (col[j].size() == 1) { char color = col[j].begin()->first; //cout<<"BUFF: "<<"K "<<j + 1<<" "<<color<<"\n"; buff.push(newAns('K', j + 1, color)); col[j].erase(color); for (int i = 0; i < n; i++) { if (row[i][color] <= 1) { row[i].erase(color); } else { row[i][color]--; } } break; } } //debug(); } cout<<buff.size()<<"\n"; while (!buff.empty()) { ans e = buff.top(); buff.pop(); cout<<e.rowCol<<" "<<e.num<<" "<<e.col<<"\n"; } return 0; } |