#include <iostream> #include <vector> #include <set> using namespace std; char T[2000][2000]; vector<string> kolejnosc; bool tenSamKolorRow(int row,const set<int>& coll){ char color = T[row][*coll.begin()]; for(auto num : coll) if(T[row][num]!=color) return false; return true; } bool tenSamKolorCol(int col, const set<int>&row){ char color = T[*row.begin()][col]; for(auto num : row) if(T[num][col]!=color) return false; return true; } set<int> rows,colls; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++){ rows.insert(i); for (int j = 0; j < m; j++) cin >> T[i][j]; } for (int j = 0; j < m; j++) colls.insert(j); int l = 0; vector<int> trash; bool zmiana = true; while(zmiana && colls.size() != 0){ zmiana = false; for(auto row : rows){ if(tenSamKolorRow(row,colls)) { kolejnosc.push_back("R "+to_string(row+1)+" "+ (T[row][*colls.begin()])); trash.push_back(row); zmiana = true; } } for(int row : trash){ rows.erase(row); } trash.clear(); if(rows.size() == 0) break; for(auto col : colls){ if(tenSamKolorCol(col,rows)){ kolejnosc.push_back("K "+to_string(col+1)+" "+ (T[*rows.begin()][col])); trash.push_back(col); zmiana = true; } } for(int col : trash) colls.erase(col); trash.clear(); } cout<<kolejnosc.size()<<"\n"; for (auto it = kolejnosc.rbegin(); it != kolejnosc.rend(); ++it) { cout << *it << "\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 | #include <iostream> #include <vector> #include <set> using namespace std; char T[2000][2000]; vector<string> kolejnosc; bool tenSamKolorRow(int row,const set<int>& coll){ char color = T[row][*coll.begin()]; for(auto num : coll) if(T[row][num]!=color) return false; return true; } bool tenSamKolorCol(int col, const set<int>&row){ char color = T[*row.begin()][col]; for(auto num : row) if(T[num][col]!=color) return false; return true; } set<int> rows,colls; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++){ rows.insert(i); for (int j = 0; j < m; j++) cin >> T[i][j]; } for (int j = 0; j < m; j++) colls.insert(j); int l = 0; vector<int> trash; bool zmiana = true; while(zmiana && colls.size() != 0){ zmiana = false; for(auto row : rows){ if(tenSamKolorRow(row,colls)) { kolejnosc.push_back("R "+to_string(row+1)+" "+ (T[row][*colls.begin()])); trash.push_back(row); zmiana = true; } } for(int row : trash){ rows.erase(row); } trash.clear(); if(rows.size() == 0) break; for(auto col : colls){ if(tenSamKolorCol(col,rows)){ kolejnosc.push_back("K "+to_string(col+1)+" "+ (T[*rows.begin()][col])); trash.push_back(col); zmiana = true; } } for(int col : trash) colls.erase(col); trash.clear(); } cout<<kolejnosc.size()<<"\n"; for (auto it = kolejnosc.rbegin(); it != kolejnosc.rend(); ++it) { cout << *it << "\n"; } return 0; } |