#include<cstdio> #include<algorithm> #include<queue> #include<set> #define S 2007 using namespace std; int T[S][S]; int rowsLetters[S][S]; int rowColors[S]; int columnLetters[S][S]; int columnColors[S]; bool visRow[S]; bool visColumn[S]; set<int> existingRows; set<int> existingColumns; int main(void){ int n,m; scanf("%d %d",&n,&m); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ char c; scanf(" %c", &c); T[i][j] = c - 'A' + 1; } } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ rowsLetters[i][T[i][j]]++; if(rowsLetters[i][T[i][j]] == 1){ rowColors[i]++; } } } for(int j = 1; j <= m; j++){ for(int i = 1; i <= n; i++){ columnLetters[j][T[i][j]]++; if(columnLetters[j][T[i][j]] == 1){ columnColors[j]++; } } } queue<pair<int,int>> q; for(int i = 1; i <= n; i++){ if(rowColors[i] == 1){ q.push({1, i}); } } for(int j = 1; j <= m; j++){ if(columnColors[j] == 1){ q.push({2, j}); } } vector<pair<char,pair<int,char>>> ans; for(int i = 1; i <= n; i++){ existingRows.insert(i); } for(int j = 1; j <= m; j++){ existingColumns.insert(j); } while(!q.empty()){ int t = q.front().first; int x = q.front().second; q.pop(); if(t == 1){ char c = 'A'; for(int i = 1; i <= 26; i++){ if(rowsLetters[x][i] != 0) c = 'A' + i - 1; } ans.push_back({'R', {x, c}}); for(auto &v: existingColumns){ columnLetters[v][T[x][v]]--; if(columnLetters[v][T[x][v]] == 0){ columnColors[v]--; if(columnColors[v] == 1) q.push({2,v}); } } existingRows.erase(x); }else{ char c = 'A'; for(int i = 1; i <= 26; i++){ if(columnLetters[x][i] != 0) c = 'A' + i - 1; } ans.push_back({'K', {x, c}}); for(auto &v: existingRows){ rowsLetters[v][T[v][x]]--; if(rowsLetters[v][T[v][x]] == 0){ rowColors[v]--; if(rowColors[v] == 1) q.push({1,v}); } } existingColumns.erase(x); } } reverse(ans.begin(), ans.end()); printf("%d\n",ans.size()); for(int i = 0; i < ans.size(); i++){ printf("%c %d %c\n",ans[i].first, ans[i].second.first, ans[i].second.second); } 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 118 119 120 121 122 123 124 125 126 | #include<cstdio> #include<algorithm> #include<queue> #include<set> #define S 2007 using namespace std; int T[S][S]; int rowsLetters[S][S]; int rowColors[S]; int columnLetters[S][S]; int columnColors[S]; bool visRow[S]; bool visColumn[S]; set<int> existingRows; set<int> existingColumns; int main(void){ int n,m; scanf("%d %d",&n,&m); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ char c; scanf(" %c", &c); T[i][j] = c - 'A' + 1; } } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ rowsLetters[i][T[i][j]]++; if(rowsLetters[i][T[i][j]] == 1){ rowColors[i]++; } } } for(int j = 1; j <= m; j++){ for(int i = 1; i <= n; i++){ columnLetters[j][T[i][j]]++; if(columnLetters[j][T[i][j]] == 1){ columnColors[j]++; } } } queue<pair<int,int>> q; for(int i = 1; i <= n; i++){ if(rowColors[i] == 1){ q.push({1, i}); } } for(int j = 1; j <= m; j++){ if(columnColors[j] == 1){ q.push({2, j}); } } vector<pair<char,pair<int,char>>> ans; for(int i = 1; i <= n; i++){ existingRows.insert(i); } for(int j = 1; j <= m; j++){ existingColumns.insert(j); } while(!q.empty()){ int t = q.front().first; int x = q.front().second; q.pop(); if(t == 1){ char c = 'A'; for(int i = 1; i <= 26; i++){ if(rowsLetters[x][i] != 0) c = 'A' + i - 1; } ans.push_back({'R', {x, c}}); for(auto &v: existingColumns){ columnLetters[v][T[x][v]]--; if(columnLetters[v][T[x][v]] == 0){ columnColors[v]--; if(columnColors[v] == 1) q.push({2,v}); } } existingRows.erase(x); }else{ char c = 'A'; for(int i = 1; i <= 26; i++){ if(columnLetters[x][i] != 0) c = 'A' + i - 1; } ans.push_back({'K', {x, c}}); for(auto &v: existingRows){ rowsLetters[v][T[v][x]]--; if(rowsLetters[v][T[v][x]] == 0){ rowColors[v]--; if(rowColors[v] == 1) q.push({1,v}); } } existingColumns.erase(x); } } reverse(ans.begin(), ans.end()); printf("%d\n",ans.size()); for(int i = 0; i < ans.size(); i++){ printf("%c %d %c\n",ans[i].first, ans[i].second.first, ans[i].second.second); } return 0; } |