#include<bits/stdc++.h> using namespace std; bool doneRow[2001]; bool doneCol[2001]; map<char, int> howManyInColorRow[2001]; map<char, int> howManyInColorCol[2001]; int colorsRemainingRow[2001]; int colorsRemainingCol[2001]; map<char, int> howManyToRemoveFromRows; map<char, int> howManyToRemoveFromCols; int rowsRemaining; int colsRemaining; int n,m; vector<int> rowsToBeEliminated; vector<int> colsToBeEliminated; string mapa[2001]; stack<string> result; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin>>n>>m; rowsRemaining=n; colsRemaining=m; for(int i=0;i<n;i++) { cin>>mapa[i]; for(int j=0;j<m;j++) { howManyInColorRow[i][mapa[i][j]]++; howManyInColorCol[j][mapa[i][j]]++; } } for(int i=0;i<n;i++) { colorsRemainingRow[i] = howManyInColorRow[i].size(); if(colorsRemainingRow[i]==1) rowsToBeEliminated.push_back(i); } for(int j=0;j<m;j++) { colorsRemainingCol[j] = howManyInColorCol[j].size(); if(colorsRemainingCol[j]==1) colsToBeEliminated.push_back(j); } while(colsRemaining>0 || rowsRemaining>0) { ////cerr<<"col remaining "<<colsRemaining<<"; rows remaining"<<rowsRemaining<<endl; //for(int i=0;i<n;i++) //{ // //cerr<<"row "<<i<<" remaining colors "<<colorsRemainingRow[i]<<endl;; // if(i==1) // { // //cerr<<"row 1 colors"<<endl; // for(auto kv :howManyInColorRow[i]) // { // //cerr<<kv.first<<" "<<kv.second<<endl; // } // } //} //for(int i=0;i<m;i++) //{ // //cerr<<"col "<<i<<" remaining colors "<<colorsRemainingCol[i]<<endl;; //} if(colsToBeEliminated.size()>0) { howManyToRemoveFromRows.clear(); for(auto col : colsToBeEliminated) { if(!doneCol[col]) { ////cerr<<"size: "<<howManyInColorCol[col].size()<<endl; char color = howManyInColorCol[col].begin()->first; howManyToRemoveFromRows[color]++; doneCol[col]=1; stringstream ss; ss<<"K"<<" "<<col+1<<" "<<color; result.push(ss.str()); colsRemaining--; } } colsToBeEliminated.clear(); //colsRemaining-=colsToBeEliminated.size(); for(int row=0;row<n;row++) { if(!doneRow[row]) { for(auto kv : howManyToRemoveFromRows) { howManyInColorRow[row][kv.first]-=kv.second; if(howManyInColorRow[row][kv.first]==0) { colorsRemainingRow[row]--; howManyInColorRow[row].erase(kv.first); if(colorsRemainingRow[row]==1) { //cerr<<"row to be eliminated: "<<row<<endl; rowsToBeEliminated.push_back(row); } else if(colorsRemainingRow[row]==0) { //cerr<<"row "<<row<<" has no colors left"<<endl; doneRow[row]=true; rowsRemaining--; } } } } } } if(rowsToBeEliminated.size()>0) { howManyToRemoveFromCols.clear(); for(auto row : rowsToBeEliminated) { if(!doneRow[row]) { ////cerr<<"size: "<<howManyInColorRow[row].size()<<endl; char color = howManyInColorRow[row].begin()->first; howManyToRemoveFromCols[color]++; doneRow[row]=1; stringstream ss; ss<<"R"<<" "<<row+1<<" "<<color; result.push(ss.str()); rowsRemaining--; } } rowsToBeEliminated.clear(); for(int col=0;col<m;col++) { if(!doneCol[col]) { for(auto kv : howManyToRemoveFromCols) { howManyInColorCol[col][kv.first]-=kv.second; if(howManyInColorCol[col][kv.first]==0) { colorsRemainingCol[col]--; howManyInColorCol[col].erase(kv.first); if(colorsRemainingCol[col]==1) { //cerr<<"col to be eliminated: "<<col<<endl; colsToBeEliminated.push_back(col); } else if(colorsRemainingCol[col]==0) { //cerr<<"column "<<col<<" has no colors left"<<endl; doneCol[col]=true; colsRemaining--; } } } } } } } cout<< result.size()<<endl; while(!result.empty()) { cout<<result.top()<<endl; result.pop(); } }
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | #include<bits/stdc++.h> using namespace std; bool doneRow[2001]; bool doneCol[2001]; map<char, int> howManyInColorRow[2001]; map<char, int> howManyInColorCol[2001]; int colorsRemainingRow[2001]; int colorsRemainingCol[2001]; map<char, int> howManyToRemoveFromRows; map<char, int> howManyToRemoveFromCols; int rowsRemaining; int colsRemaining; int n,m; vector<int> rowsToBeEliminated; vector<int> colsToBeEliminated; string mapa[2001]; stack<string> result; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin>>n>>m; rowsRemaining=n; colsRemaining=m; for(int i=0;i<n;i++) { cin>>mapa[i]; for(int j=0;j<m;j++) { howManyInColorRow[i][mapa[i][j]]++; howManyInColorCol[j][mapa[i][j]]++; } } for(int i=0;i<n;i++) { colorsRemainingRow[i] = howManyInColorRow[i].size(); if(colorsRemainingRow[i]==1) rowsToBeEliminated.push_back(i); } for(int j=0;j<m;j++) { colorsRemainingCol[j] = howManyInColorCol[j].size(); if(colorsRemainingCol[j]==1) colsToBeEliminated.push_back(j); } while(colsRemaining>0 || rowsRemaining>0) { ////cerr<<"col remaining "<<colsRemaining<<"; rows remaining"<<rowsRemaining<<endl; //for(int i=0;i<n;i++) //{ // //cerr<<"row "<<i<<" remaining colors "<<colorsRemainingRow[i]<<endl;; // if(i==1) // { // //cerr<<"row 1 colors"<<endl; // for(auto kv :howManyInColorRow[i]) // { // //cerr<<kv.first<<" "<<kv.second<<endl; // } // } //} //for(int i=0;i<m;i++) //{ // //cerr<<"col "<<i<<" remaining colors "<<colorsRemainingCol[i]<<endl;; //} if(colsToBeEliminated.size()>0) { howManyToRemoveFromRows.clear(); for(auto col : colsToBeEliminated) { if(!doneCol[col]) { ////cerr<<"size: "<<howManyInColorCol[col].size()<<endl; char color = howManyInColorCol[col].begin()->first; howManyToRemoveFromRows[color]++; doneCol[col]=1; stringstream ss; ss<<"K"<<" "<<col+1<<" "<<color; result.push(ss.str()); colsRemaining--; } } colsToBeEliminated.clear(); //colsRemaining-=colsToBeEliminated.size(); for(int row=0;row<n;row++) { if(!doneRow[row]) { for(auto kv : howManyToRemoveFromRows) { howManyInColorRow[row][kv.first]-=kv.second; if(howManyInColorRow[row][kv.first]==0) { colorsRemainingRow[row]--; howManyInColorRow[row].erase(kv.first); if(colorsRemainingRow[row]==1) { //cerr<<"row to be eliminated: "<<row<<endl; rowsToBeEliminated.push_back(row); } else if(colorsRemainingRow[row]==0) { //cerr<<"row "<<row<<" has no colors left"<<endl; doneRow[row]=true; rowsRemaining--; } } } } } } if(rowsToBeEliminated.size()>0) { howManyToRemoveFromCols.clear(); for(auto row : rowsToBeEliminated) { if(!doneRow[row]) { ////cerr<<"size: "<<howManyInColorRow[row].size()<<endl; char color = howManyInColorRow[row].begin()->first; howManyToRemoveFromCols[color]++; doneRow[row]=1; stringstream ss; ss<<"R"<<" "<<row+1<<" "<<color; result.push(ss.str()); rowsRemaining--; } } rowsToBeEliminated.clear(); for(int col=0;col<m;col++) { if(!doneCol[col]) { for(auto kv : howManyToRemoveFromCols) { howManyInColorCol[col][kv.first]-=kv.second; if(howManyInColorCol[col][kv.first]==0) { colorsRemainingCol[col]--; howManyInColorCol[col].erase(kv.first); if(colorsRemainingCol[col]==1) { //cerr<<"col to be eliminated: "<<col<<endl; colsToBeEliminated.push_back(col); } else if(colorsRemainingCol[col]==0) { //cerr<<"column "<<col<<" has no colors left"<<endl; doneCol[col]=true; colsRemaining--; } } } } } } } cout<< result.size()<<endl; while(!result.empty()) { cout<<result.top()<<endl; result.pop(); } } |